In the below program I have created a button that when clicked should show another jframe with added components. But when I click on this button it does not show any components only a blank jframe appears with title. What's my problem can anyone explain it?
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JFrame f1 = new JFrame();
f1.setVisible(true);
f1.setSize(800, 700);
f1.setTitle("Calc");
f1.getContentPane().setLayout(new FlowLayout());
JTextField t1 = new JTextField(10);
JTextField t2 = new JTextField(10);
JTextField t3 = new JTextField(10);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b10 = new JButton("0");
JButton b11 = new JButton("Add");
JButton b12 = new JButton("Sub");
JButton b13 = new JButton("Mul");
JButton b14 = new JButton("Div");
JButton b15 = new JButton("=");
JButton b16 = new JButton("CLR");
f.getContentPane().add(t1);
f.getContentPane().add(t2);
f.getContentPane().add(t3);
f.getContentPane().add(b1);
f.getContentPane().add(b2);
f.getContentPane().add(b3);
f.getContentPane().add(b4);
f.getContentPane().add(b5);
f.getContentPane().add(b6);
f.getContentPane().add(b7);
f.getContentPane().add(b8);
f.getContentPane().add(b9);
f.getContentPane().add(b10);
f.getContentPane().add(b11);
f.getContentPane().add(b12);
f.getContentPane().add(b13);
f.getContentPane().add(b14);
f.getContentPane().add(b15);
f.getContentPane().add(b16);
}
});
Possibly typo error in your case as address by the other answers.
f1
instead of f
f1.setVisible(true);
below to the bottom when you are done with adding buttons to it, ie f1.getContentPane().add(b16);
In your code you have refrenced a JFrame as f1
and you are adding component to f
so it will not add anything to your frame so, try using to add in f1
instead of f
and also move f1.setVisible(true);
below after adding every component to f1
as i have shown below
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JFrame f1 = new JFrame();
f1.setSize(800, 700);
f1.setTitle("Calc");
f1.getContentPane().setLayout(new FlowLayout());
JTextField t1 = new JTextField(10);
JTextField t2 = new JTextField(10);
JTextField t3 = new JTextField(10);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b10 = new JButton("0");
JButton b11 = new JButton("Add");
JButton b12 = new JButton("Sub");
JButton b13 = new JButton("Mul");
JButton b14 = new JButton("Div");
JButton b15 = new JButton("=");
JButton b16 = new JButton("CLR");
f1.getContentPane().add(t1);
f1.getContentPane().add(t2);
f1.getContentPane().add(t3);
f1.getContentPane().add(b1);
f1.getContentPane().add(b2);
f1.getContentPane().add(b3);
f1.getContentPane().add(b4);
f1.getContentPane().add(b5);
f1.getContentPane().add(b6);
f1.getContentPane().add(b7);
f1.getContentPane().add(b8);
f1.getContentPane().add(b9);
f1.getContentPane().add(b10);
f1.getContentPane().add(b11);
f1.getContentPane().add(b12);
f1.getContentPane().add(b13);
f1.getContentPane().add(b14);
f1.getContentPane().add(b15);
f1.getContentPane().add(b16);
f1.setVisible(true);
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.