根据以上图表;我希望Frame1使用New(Jbutton)显示Frame2并使用Show(JButton)显示Frame3。 Frame3有这个默认的“Hello World”(JtextField),我想使用Frame2中的Yes(JButton)将其设置为空。
问题是我不知道Frame2的代码以及如何从frame3清空文本字段。
这是我的代码到目前为止:
Frame1.java
public class Frame1 extends JFrame implements ActionListener{
JButton b1 = new JButton("New");
JButton b2 = new JButton("Show");
Frame2 f2 = new Frame2();
Frame3 f3 = new Frame3();
public Frame1(){
setLayout(new FlowLayout());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public static void main(String args[]){
new Frame1();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
f2.setVisible(true);
}
else{
f3.setVisible(true);
}
}
}Frame2.java
public class Frame2 extends JFrame implements ActionListener{
JButton b1 = new JButton("Yes");
JButton b2 = new JButton("No");
public Frame2(){
setLayout(new FlowLayout());
setVisible(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,100);
add(b1);
add(b2);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
}else{
}
}
}Frame3.java
public class Frame3 extends JFrame{
JTextField t1 = new JTextField("Hello WOrld");
public Frame3(){
setLayout(new FlowLayout());
setVisible(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,200);
add(t1);
}
}