Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, everyone, please explain displaying JPanel, ON button click in the same Frame

i've displayed a new JFrame with respect to button click but not JPanel

here's my code but it didn't work!


Java
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class buttons extends JPanel{
    public static void main (String args []) {
    
        JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EX…
        frame.setTitle("BULLS EYE");
        JPanel panel = new JPanel();
        JButton button1 = new JButton("click me");
        JButton button2 = new JButton("second button");
        
        
        panel.add(button1);
        panel.add(button2);
        
        frame.add(panel);
        
        button1.addActionListener( new Action1());
        button2.addActionListener(new Action2());
    }
    static class Action1 implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            JFrame Frame1 = new JFrame();
            JPanel panel1 = new JPanel();
            JLabel label1 = new JLabel("OM NAMA SHIVAYA");
            Frame1.setVisible(true);
            Frame1.setSize(300,300);
            Frame1.setDefaultCloseOperation(JFrame.E…
            panel1.add(label1);
            Frame1.add(panel1);
        }
    
    }
        
    static class Action2 implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            JPanel panel2 = new JPanel();
            
            JLabel label2 = new JLabel(" NARAYANAYA GANAPATHI ");
            GridBagConstraints c = new GridBagConstraints();
            c.gridx=0;
            c.gridy=0;
            panel2.add(label2,c);
            panel2.setSize(50,50);
            
        }
    }
    
}
Posted
Updated 23-Sep-12 1:05am
v3

This code is total chaos.

- you are extending JPanel and then using JFrame to build your GUI.
delete the "extends JPanel" or use "extends JFrame" and refer to it via "this".

Java
public class MyFrame extends JFrame{

  public myFrame(){
    // create GUI
    this.setSize(500, 500);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EX…
    this.setTitle("BULLS EYE");

    JButton oButton = new JButton("Click me hard!");
    oButton.addActionListener(new ButtonAction());
    this.add(oButton);
  }

  public static void main (String args []) {
    // start action here by creating the JFrame
    MyFrame oFrame = new MyFrame();
    oFrame.setVisible(true);
  }

}


- you are creating static classes for the Actions because you are too lazy to move the JFrame out of the main method.
Change that.

Java
public class MyFrame extends JFrame{
  // ...

  public ButtonAction implements ActionListener{
    public void actionPerformed(ActionEvent e) {

    }
  }
}


For the Action:
You can refer to the original object (the JButton) via oEvent.getSource(). You will need to cast that given Object that to get the JButton back:

Java
public ButtonAction implements ActionListener{
  public void actionPerformed(ActionEvent oEvent) {
      if(oEvent.getSource instanceOf JButton){ // check if Action is triggered by JButton
        JButton oButton = (JButton)oEvent.getSource(); // get Button (if needed)
        
        MyFrame.this.doFunnyThings(); // call function of the JFrame
      }
    }
  }
}


You can also define a ActionCommand for the button to identify the trigger of the action.
Please do a little search on the web for that.
 
Share this answer
 
v2
You are creating new instances of objects within your action listeners. However, as soon as these methods terminate those objects go out of scope and are destroyed. You need to make the objects part of the main class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900