Java 如何在GUI中创建多个框架或窗口?
我不熟悉创建GUI。我想知道如何创建多个窗口。如果要单击按钮,我想显示另一帧。我已经搜索了如何,我看到一些人正在制作另一个GUI表单,只是调用另一个表单,我点击了一个按钮,但我不明白怎么做 有很多方法可以做到这一点。主要方法之一是创建一个具有自己属性的新Java类。这里有一个例子:Java 如何在GUI中创建多个框架或窗口?,java,swing,user-interface,intellij-idea,Java,Swing,User Interface,Intellij Idea,我不熟悉创建GUI。我想知道如何创建多个窗口。如果要单击按钮,我想显示另一帧。我已经搜索了如何,我看到一些人正在制作另一个GUI表单,只是调用另一个表单,我点击了一个按钮,但我不明白怎么做 有很多方法可以做到这一点。主要方法之一是创建一个具有自己属性的新Java类。这里有一个例子: JButton button = new JButton("Button_Leads_To_This_Window"); button.addActionListener( new ActionActionListe
JButton button = new JButton("Button_Leads_To_This_Window");
button.addActionListener( new ActionActionListener()
{
public void actionPerformed(ActionEvent e)
{
NewFrame();
}
});
这将允许按钮调用一个新窗口,类似于您调用“我的帝国”窗口的方式。例如,NewFrame()类将如下所示:
public static void newFrame()
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setOpaque(true);
JTextArea textArea = new JTextArea(15, 50);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setFont(Font.getFont(Font.SANS_SERIF));
JScrollPane scroller = new JScrollPane(textArea);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JPanel inputpanel = new JPanel();
inputpanel.setLayout(new FlowLayout());
JTextField input = new JTextField(20);
JButton button = new JButton("Enter");
DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
panel.add(scroller);
inputpanel.add(input);
inputpanel.add(button);
panel.add(inputpanel);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.setResizable(false);
input.requestFocus();
}
});
}
以下是有关该事项的更多信息:
祝你好运。有很多方法可以做到这一点。主要方法之一是创建一个具有自己属性的新Java类。这里有一个例子:
JButton button = new JButton("Button_Leads_To_This_Window");
button.addActionListener( new ActionActionListener()
{
public void actionPerformed(ActionEvent e)
{
NewFrame();
}
});
这将允许按钮调用一个新窗口,类似于您调用“我的帝国”窗口的方式。例如,NewFrame()类将如下所示:
public static void newFrame()
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setOpaque(true);
JTextArea textArea = new JTextArea(15, 50);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setFont(Font.getFont(Font.SANS_SERIF));
JScrollPane scroller = new JScrollPane(textArea);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JPanel inputpanel = new JPanel();
inputpanel.setLayout(new FlowLayout());
JTextField input = new JTextField(20);
JButton button = new JButton("Enter");
DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
panel.add(scroller);
inputpanel.add(input);
inputpanel.add(button);
panel.add(inputpanel);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.setResizable(false);
input.requestFocus();
}
});
}
以下是有关该事项的更多信息:
祝你好运。new JFrame()
。请参阅该答案,了解一些自由浮动选项和许多非自由浮动选项。new JFrame()
。请参阅该答案,了解一些自由浮动选项和许多非自由浮动选项。