如何在 Java 中创建 2 个框架并将它们链接在一起?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18091172/
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
How do I create 2 frames in Java and Link them together?
提问by Troll Junior
I just created a GUI, now I want to create another GUI and link both together.
我刚刚创建了一个 GUI,现在我想创建另一个 GUI 并将两者链接在一起。
So on the first GUI when the user selects 'next' button, the second GUI is displayed.
因此,当用户选择“下一步”按钮时,会在第一个 GUI 上显示第二个 GUI。
For this, do I have to create a new class and just create a GUI again?
为此,我是否必须创建一个新类并再次创建一个 GUI?
Here is what I have now:
这是我现在所拥有的:
import java.awt.Color;
import javax.swing.*;
public class Wizard {
private JLabel lblPicture;
private JRadioButton btLdap, btKerbegos, btSpnego, btSaml2;
private JButton btNext;
private JPanel panel;
public static void main(String[] args) {
new Wizard();
}
public Wizard() {
JFrame frame = new JFrame("Wizard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,360);
frame.setVisible(true);
MyPanel();
RadioButtons();
Button();
Image();
groupButton();
frame.add(panel);
frame.setVisible(true);
}
public void MyPanel() {
panel = new JPanel();
panel.setLayout(null);}
public void RadioButtons() {
btLdap = new JRadioButton ("Ldap");
btLdap.setBounds(60,85,100,20);
panel.add(btLdap);
btKerbegos = new JRadioButton ("Kerbegos");
btKerbegos.setBounds(60,115,100,20);
panel.add(btKerbegos);
btSpnego =new JRadioButton("Spnego");
btSpnego.setBounds(60,145,100,20);
panel.add(btSpnego);
btSaml2 = new JRadioButton("Saml2");
btSaml2.setBounds(60,175,100,20);
panel.add(btSaml2);
}
public void Button() {
btNext = new JButton ("Next");
btNext.setBounds(400,260,100,20);
panel.add(btNext);
}
public void Image() {
ImageIcon image = new ImageIcon("image.jpg");
lblPicture = new JLabel(image);
lblPicture.setBounds(200,20, 330, 270);
panel.add(lblPicture);
}
private void groupButton() {
ButtonGroup bg1 = new ButtonGroup( );
bg1.add(btLdap);
bg1.add(btKerbegos);
bg1.add(btSpnego);
bg1.add(btSaml2);
}
}
回答by Hovercraft Full Of Eels
- To display another window, you would create the window, be it a JFrame, JDialog, or what have you, and call
setVisible(true)
just like you do for your first window. - You ask if your other "window" should be in another class, and likely that answer is yes. Since it will have a completely different set of behaviors and goals from the first class, better to separate out concerns.
- Having said that, what you plan to do, to show multiple windows is not always the best user interface design. Better often is to show multiple views using a container that uses a CardLayout.
- If you want to display another window in a modalfashion, that is, have the first window wait for the second window to be processed before allowing user interaction, the second window should be a modal JDialog or JOptionPane (a JDialog in disguise).
- 要显示另一个窗口,您需要创建该窗口,无论是 JFrame、JDialog 还是您拥有的任何窗口,并
setVisible(true)
像对第一个窗口所做的那样调用。 - 你问你的另一个“窗口”是否应该在另一个班级,答案很可能是肯定的。由于它将具有与第一类完全不同的一组行为和目标,因此最好将关注点分开。
- 话虽如此,你打算做什么,显示多个窗口并不总是最好的用户界面设计。更好的做法是使用使用 CardLayout 的容器来显示多个视图。
- 如果要以模态方式显示另一个窗口,即在允许用户交互之前让第一个窗口等待第二个窗口处理完毕,则第二个窗口应该是模态 JDialog 或 JOptionPane(伪装的 JDialog)。
回答by Mike M
Write the two GUI's in different classes. When you start your program, start the first GUI.
在不同的类中编写两个 GUI。启动程序时,启动第一个 GUI。
FirstGUI frame1 = new FirstGUI("Title text");
frame1.setVisible(true);
Then, in the action listener code for the button you call "next"...
然后,在按钮的动作侦听器代码中,您调用“下一步”...
frame1.setVisible(false); //if you want to save the frame
frame1.dispose(); //if you want to kill the frame
SecondGUI frame2 = new SecondGUI("Title text");
frame2.setVisible(true);
回答by Java Devil
I think for what you want to achieve, the use of a CardLayout
would be appropriate.
我认为对于您想要实现的目标,使用 aCardLayout
是合适的。
This enables you to have multiple panels within the one frame with only one panel visible at a time and has functionality to 'flip' through the panels like a 'deck of cards'. So on initialising your frame you create the panels you want, and specify which one to start at then your next button will go to the next panel in the list.
这使您可以在一个框架内拥有多个面板,一次只能看到一个面板,并且具有像“纸牌”一样“翻转”面板的功能。因此,在初始化框架时,您可以创建所需的面板,并指定从哪个面板开始,然后您的下一个按钮将转到列表中的下一个面板。
See the tutorial herethere are also some video tutorials available on youtube.
请参阅此处的教程,YouTube 上还提供了一些视频教程。