如何在java中用另一个JFrame类中的按钮关闭一个JFrame类?
我有两个如何在java中用另一个JFrame类中的按钮关闭一个JFrame类?,java,swing,jframe,Java,Swing,Jframe,我有两个JFrames(frameA和FrameB)框架B只能从frameA打开,当我打开frameB时,frameA必须保持打开状态框架B有一个按钮(关闭框架a)。我想在单击按钮时关闭frameA 如何操作?您可以使用以下两个类:TJFrame和OpenFrame,用另一个JFrame中的按钮关闭JFrame类 public class TJFrame { public static OpenFrame openWindow; public static void main(String[
JFrames
(frameA
和FrameB
)<代码>框架B只能从frameA
打开,当我打开frameB
时,frameA
必须保持打开状态<代码>框架B有一个按钮(关闭框架a
)。我想在单击按钮时关闭frameA
如何操作?您可以使用以下两个类:TJFrame和OpenFrame,用另一个JFrame中的按钮关闭JFrame类
public class TJFrame {
public static OpenFrame openWindow;
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Frame");
JButton button = new JButton("Open");
frame.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openWindow = new OpenFrame();
openWindow.setVisible(true);
}
});
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setSize(350, 200);
frame.setVisible(true);
}}
public class OpenFrame extends JFrame{
JPanel back_panel;
public JButton button = new JButton("Cross");
public OpenFrame() {
back_panel = new JPanel();
setContentPane(back_panel);
this.setSize(350, 200);
button.setBounds(380, 10, 20, 20);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
back_panel.add(button);
}}
首先,每个JFrame是由不同的类生成的(我假设是这样,因为我不知道生成两个帧的其他方法)。 可尝试的解决方案: 在框架A中,创建一个“静态变量”: 而不是做
JFrame frameA=new JFrame("Name of the frame");
在publicstaticvoidmain(String[]args)中。然后,在publicstaticvoidmain(String[]args)程序中,执行以下操作
//the static JFrame assigned before
frameA= new JFrmae("Nameof the frame");
这样,frameB中的程序就可以使用ClassB中的以下代码读取“frameA”(让我们调用生成frameB ClassB的类):
然后,仍然在B班,我们可以
frameA.dispose();
我希望你能理解(如果你不理解,请对你不理解的内容进行评论),我希望它能起作用
代码:
及
将
FrameA
的引用传递到FrameB
,然后单击“按钮”时,所有的dispose
方法…请参见
JFrame frameA= ClassA.frameA;
frameA.dispose();
import javax.swing.JFrame;
public class ClassA {
static JFrame frameA;
public ClassA(){
//a useless constructor because I am not adding any Listeners(don't worry about it)
}
public static void main(String[] args){
frameA=new JFrame("Name");
//your ordinary things(some peiople put these in the constructor)
frameA.setSize(300,300);
frameA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameA.setVisible(true);
//runs ClassB
new ClassB();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ClassB extends JFrame implements ActionListener{
static JButton close=new JButton("close");
public ClassB(){
//your ordinary thigns
add(close);
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
close.addActionListener(this);
System.out.println("what?");
}
public static void main(String[] args){
JFrame frameB=new JFrame("Clae Frame A");
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.equals("close")){
JFrame frameA=ClassA.frameA;
frameA.dispose();
}
}
}