gpt4 book ai didi

java - 将组件添加到 JFrame

转载 作者:行者123 更新时间:2023-12-02 12:16:37 25 4
gpt4 key购买 nike

在下面的程序中,我创建了一个按钮,单击该按钮时应显示另一个带有添加组件的 jframe。但是当我单击此按钮时,它不显示任何组件,仅出现一个带有标题的空白 jframe。我的问题是什么,谁能解释一下吗?

<小时/>
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);

}
});

最佳答案

您的情况可能与其他答案的地址存在拼写错误。

  1. 使用f1而不是f
  2. 添加完按钮后,将下面的 f1.setVisible(true); 行移至底部,即 f1.getContentPane().add(b16);

关于java - 将组件添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46126992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com