java 在多个 JFrame 之间切换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30369197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch between multiple JFrames
提问by Vivi Huber
I'm relatively new to Java and I'm trying to make some kind of quiz. I created 3 JFrames, all in the same package. On my main frame, there are two buttons (one for the english version and the other one for the german version). I want to switch JFrames after pressing these buttons (so that I can, by pressing "English", see and interact with my english quiz frame). Looking it up didn't help me the slightest, because I'm not really experienced with it. Is it even possible to do it like this? If not, how could I do it?
我对 Java 比较陌生,我正在尝试进行某种测验。我在同一个包中创建了 3 个 JFrame。在我的主机上,有两个按钮(一个用于英文版,另一个用于德文版)。我想在按下这些按钮后切换 JFrames(这样我就可以通过按下“英语”来查看我的英语测验框架并与之交互)。查找它对我没有丝毫帮助,因为我对它并没有真正的经验。甚至有可能这样做吗?如果没有,我该怎么做?
回答by npinti
The standard approach is to use the Card Layout, which allows you to use the same JFrame as you populate it with different things at different points in your application. So initially, your JFrame would show the loading screen, then the user presses a button and you load a new set of components without discarding the current JFrame you have. In some cases, you might also need to make some size adjustments.
标准方法是使用Card Layout,它允许您在应用程序的不同点使用相同的 JFrame 填充不同的内容。因此,最初,您的 JFrame 将显示加载屏幕,然后用户按下一个按钮,然后您加载一组新组件,而不会丢弃您拥有的当前 JFrame。在某些情况下,您可能还需要进行一些尺寸调整。
It is difficult to say without seeing any code, but usually, what is done is that you do something like so:
没有看到任何代码很难说,但通常,所做的是你做这样的事情:
new Frame(args);
this.dispose();
The code above assumes that the constructor of Frame
takes care of launching and making the components visible. The this.dispose();
disposes of the current JFrame
(assuming your class extends JFrame
).
上面的代码假设 的构造函数Frame
负责启动和使组件可见。this.dispose();
当前的处置JFrame
(假设您的类 extends JFrame
)。
回答by ShanD
You have two buttons in your frame 1 right? So first, double click the button which says "English". Lets say the variable name for that button is jButton1. Inside that button type this.
您的框架 1 中有两个按钮,对吗?首先,双击显示“English”的按钮。假设该按钮的变量名称是 jButton1。在那个按钮里面输入这个。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
EnglishFrame eng = new EnglishFrame();
eng.setVisible(true);
}
Then double click the other button which says "German" (jButton2). Inside that type this.
然后双击另一个按钮,上面写着“德语”(jButton2)。里面那个类型这个。
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
GermanFrame german = new GermanFrame();
german.setVisible(true);
}
this.dispose() - This will cause the jFrame window to close
this.dispose() - 这将导致 jFrame 窗口关闭
Then you create an object of the other two forms. (In your case the form for English and Germany)
然后创建其他两种形式的对象。(在你的情况下,英语和德国的表格)
.setVisible(true) - This will show you the form.
.setVisible(true) - 这将显示表格。
回答by Ajan
Create a single Jframe window. After that create JPanelswith all the compenents such as buttons, textfields and labels you want. Make sure the panel is the same size as your Jframe. Panel's work about the same as JFrame's, code wise.
创建单个 Jframe 窗口。之后,创建带有所有组件的JPanel,例如您想要的按钮、文本字段和标签。确保面板与 Jframe 的大小相同。Panel 的工作与 JFrame 大致相同,代码明智。
This code will stitch everything together for you:
此代码将为您拼接所有内容:
panel.setSize(Jframe.getSize()) //That wont
panel.add(button); //Just remember you need to add more code to position the buttons correctly.
//If you using netbeans builder:
//You just have to use this one line in the constructor/intialiser method
frame.add(panel); //This will add the panel to the Jframe/Window
//No need to add extra code for positioning.
If you want to swap between the panels. In the button press method, use this
如果要在面板之间交换。在按钮按下方法中,使用这个
frame.setContentPane(panel); //panel = panel you want to change too.
frame.repaint(); //Ensures that the frame swaps to the next panel and doesn't get stuck.
frame.revalidate(); //Ensures that the frame swaps to the next panel and doesn't get stuck.
When you first start the java application you have to set the content pane or else it will appear as a blank window.
当您第一次启动 Java 应用程序时,您必须设置内容窗格,否则它将显示为一个空白窗口。
frame.setContentPane(panel); //Starting Panel
frame.setVisible(true); //Make the frame visible
Sorry if the explanation is bad, I don't have enough time to explain it fully.
对不起,如果解释不好,我没有足够的时间来充分解释。