getSource() == open

plammy

Bekanntes Mitglied
Hallo :)

Ich habe geschafft, ein Bild in meinem JFrame zu öffnen mithilfe von JFileChooser.. Ich will es aber in einem bestimmten JPanl öffnen...

derzeit sieht so aus:

Java:
public class GUI extends JFrame implements ActionListener{

    static GUI gui = new GUI();
        
    BufferedImage mImage;
   
    private Image img;
  
    public GUI() {
        initComponents();   

        setTitle("Layout Tool");
        setSize(900,900);
        setBackground(new Color(42, 38, 36));  
//        img = Toolkit.getDefaultToolkit().createImage("C:\\Images\\Background.jpg");

         ImageIcon icon = new ImageIcon("C:\\Images\\logo.gif");
        img=icon.getImage();
   
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
    }

  
     void initComponents() {


   .............

  menubar = new JMenuBar();
        menuFile = new JMenu();
        menuAds = new JMenu();
             
        neu = new JMenuItem();
        open = new JMenuItem();
............
  //Menue Datei
        menuFile.setText("Datei");

        neu.setMnemonic(KeyEvent.VK_N);
        neu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
        neu.setText("Neu");
        neu.addActionListener(this);
        menuFile.add(neu);
...........
 menubar.add(menuFile);


................

}
  public void actionPerformed(ActionEvent object) {
        
        //Neu
        if (object.getSource() == neu){    
      
            
             CreateFile n1 = new CreateFile();    
             setContentPane(n1);         
             revalidate();
             repaint();
       
        }
        
        //Oeffnen
        if (object.getSource() == open){
            
            JFileChooser fc = new JFileChooser();          
            fc.setFileFilter(new FileNameExtensionFilter("*.jpg;*.gif","jpg", "gif"));
            
            int state = fc.showOpenDialog(GUI.this);
            if ( state == JFileChooser.APPROVE_OPTION ){
                File file = fc.getSelectedFile();  
                BufferedImage bi;
                    try {
                    bi = ImageIO.read(file);
                            image_label.setIcon(new ImageIcon(bi));
                    } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                    }
                            // catch IOException
          }
        }


also kann ich in dem menü gehen öffnen ankliken und dasbild öffnet sich direkt auf dem jframe...

ich will aber dass wenn ich auf öffnen klicke sich Instanz der Klasse CreateFile erstellt und das Bild in editPanel öffnen und nur da



Java:
public class CreateFile extends JPanel implements ActionListener
{
 static CreateFile cf = new CreateFile();
   
    public CreateFile()
    {
        initComponents();     
    
    }
    

    public void initComponents() 
    {
.......
 tabbedPane = new JTabbedPane();
        
        editPanel = new EditPanel();
        editPanel.setPreferredSize(new Dimension(1000,1000));
       
        
        scrollPane=new JScrollPane(editPanel);
//        editPanel. setAutoscrolls(true);
        scrollPane.setPreferredSize(new Dimension(600,600));
        
   
        
        websitePanel = new JPanel();    
        layoutPanel = new JPanel();
        fixFormsPanel = new JPanel(); 
        individualFormsPanel = new JPanel();
.................

        editPanel.setBackground(lightGrayPanel);
       
//        Border tb_edit = BorderFactory.createEtchedBorder(1, lightGrayPanel,darkGrayPanel);       
//        editPanel.setBorder(tb_edit);

    
        GroupLayout editPanelLayout = new GroupLayout(editPanel);
        editPanel.setLayout(editPanelLayout);
        editPanelLayout.setHorizontalGroup(
            editPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)          
            .addGap(2,2,2)
          
        );
        editPanelLayout.setVerticalGroup(
            editPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)           
            .addGap(2,2,2)
        );
scrollPane.setViewportView(editPanel);
        tabbedPane.addTab("neu", scrollPane);
        
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        this.setBackground(darkGrayPanel);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
              
                .addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addComponent(websitePanel, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(layoutPanel, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(fixFormsPanel, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(individualFormsPanel, GroupLayout.DEFAULT_SIZE,0, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20 , 20, 20)
                .addComponent(websitePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(layoutPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(fixFormsPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(individualFormsPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
                
            .addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                
                
        );
       
    }
  
......
}
 

plammy

Bekanntes Mitglied
hab schon iwie so versucht aber klappt nicht ... :(

Java:
if (object.getSource() == open){
            
            CreateFile create = new CreateFile();
            
            JFileChooser fc = new JFileChooser();
            setContentPane(create);
            getContentPane().add(fc);
                    
            fc.setFileFilter(new FileNameExtensionFilter("*.jpg;*.gif","jpg", "gif"));
            
            int state = fc.showOpenDialog(create.editPanel);
            if ( state == JFileChooser.APPROVE_OPTION ){
                File file = fc.getSelectedFile();  
                BufferedImage bi;
                    try {
                    bi = ImageIO.read(file);
                            image_label.setIcon(new ImageIcon(bi));
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    
          }
              setVisible(true);
             repaint();
        }
 

Michael...

Top Contributor
Grundsätzlich benötigt Deine Klasse CreateFile eine Methode der man das Icon übergeben kann und die das Icon dann irgendwo "platziert".
Ist im editPanel ein JLabel enthalten in dem das Icon angefügt werden kann?
 

plammy

Bekanntes Mitglied
nein.. :(

Java:
public class EditPanel extends JPanel
{
    
    Image img;
    private int xPos, yPos;
    boolean pressOut = false;
 
    Color grayRect = new Color(230,227,224);
    Color orangeRect = new Color(211,111,53);
    
   
    
    Rectangle bausteinRect;

    public  EditPanel()
    {
       
        /* ********************* Mouse Listener ***************************  */
        addMouseListener(new MouseAdapter() 
        {
           
            public void mousePressed(MouseEvent e) 
            {  
                xPos = e.getX();
                yPos = e.getY();
                
                

            }
            
            public void mouseReleased(MouseEvent e) 
            {
                xPos = e.getX();
                yPos = e.getY();
                
                bausteinRect = new Rectangle(bausteinRect.x, bausteinRect.y, bausteinRect.width,bausteinRect.height);
                repaint();
            }
            
        });

     
        /* ****************** Mouse Motion Listener ***********************  */
        addMouseMotionListener(new MouseMotionAdapter() 
        {
       
            public void mouseDragged(MouseEvent e) 
            {
                xPos = e.getX();
                yPos = e.getY();
    
              repaint();
           }
        });
        
   
    }

   

    protected void paintComponent(Graphics g) 
    {
        super.paintComponent(g);
   
            int centerX = getWidth()/2;     
            int beginPosition = (getHeight()-(CreateFile.cf.headerH + CreateFile.cf.contentH+ CreateFile.cf.footerH+CreateFile.cf.tile1H+CreateFile.cf.tile3H))/2;

            
            //header
            Rectangle headerRect = new Rectangle(centerX-CreateFile.cf.headerW/2,beginPosition,CreateFile.cf.headerW,CreateFile.cf.headerH);      
            g.setColor(grayRect);
            headerRect.translate(-CreateFile.cf.tile2W/2,CreateFile.cf.tile1H);
            g.fill3DRect(headerRect.x, headerRect.y, headerRect.width, headerRect.height,true);
            
//            MediaTracker mt = new MediaTracker(this);
//            img = Toolkit.getDefaultToolkit().getImage("C:\\Images\\szlogo.jpg");
//            mt.addImage(img,0);
//
//            g.drawImage(img,centerX-CreateFile.cf.headerW/2,beginPosition,this);
   
            //content
            Rectangle contentRect = new Rectangle(centerX-CreateFile.cf.contentW/2,beginPosition + CreateFile.cf.headerH,CreateFile.cf.contentW,CreateFile.cf.contentH);
            g.setColor(grayRect);
            contentRect.translate(-CreateFile.cf.tile2W/2,CreateFile.cf.tile1H);   
            g.fill3DRect(contentRect.x, contentRect.y, contentRect.width, contentRect.height,true);


            //footer
            Rectangle footerRect = new Rectangle(centerX-CreateFile.cf.footerW/2,beginPosition+CreateFile.cf.headerH+CreateFile.cf.contentH,CreateFile.cf.footerW,CreateFile.cf.footerH);
            g.setColor(grayRect);
            footerRect.translate(-CreateFile.cf.tile2W/2,CreateFile.cf.tile1H+CreateFile.cf.tile3H);   
            g.fill3DRect(footerRect.x, footerRect.y, footerRect.width, footerRect.height,true);


            //tile 1
            Rectangle tile1Rect = new Rectangle(centerX-CreateFile.cf.headerW/2-CreateFile.cf.tile2W/2,beginPosition,CreateFile.cf.tile1W,CreateFile.cf.tile1H);
            g.setColor(orangeRect);  
            g.fill3DRect(tile1Rect.x, tile1Rect.y, tile1Rect.width, tile1Rect.height,true);


            //tile 2    
            Rectangle tile2Rect = new Rectangle(centerX+CreateFile.cf.headerW/2-CreateFile.cf.tile2W/2,beginPosition+CreateFile.cf.tile1H,CreateFile.cf.tile2W,CreateFile.cf.tile2H);
            g.setColor(orangeRect);  
            g.fill3DRect(tile2Rect.x, tile2Rect.y, tile2Rect.width, tile2Rect.height,true);


            //tile3      
            contentRect.translate(0,CreateFile.cf.tile3H);
            Rectangle tile3Rect = new Rectangle(centerX-CreateFile.cf.headerW/2-CreateFile.cf.tile2W/2,beginPosition+CreateFile.cf.tile1H+CreateFile.cf.headerH+CreateFile.cf.contentH,CreateFile.cf.tile3W,CreateFile.cf.tile3H);
            g.setColor(orangeRect);  
            g.fill3DRect(tile3Rect.x, tile3Rect.y, tile3Rect.width, tile3Rect.height,true);


            //baustein
     
            bausteinRect = new Rectangle(0, 0,CreateFile.cf.bausteinW,CreateFile.cf.bausteinH);
            g.setColor(orangeRect);  
            g.fill3DRect(xPos, yPos, bausteinRect.width, bausteinRect.height,true);        
       
            
    }

  
    
}
 

Ähnliche Java Themen


Oben