Java 爪哇摇摆。从 JButton 打开一个新的 JPanel 并使按钮漂亮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24009159/
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
Java Swing. Opening a new JPanel from a JButton and making the buttons pretty
提问by Splunk
I am trying to build a little program that has a main GUI with 2 buttons. One button closes the program, the other I want to open a new JPanel that will have text fields etc.
我正在尝试构建一个带有 2 个按钮的主 GUI 的小程序。一个按钮关闭程序,另一个我想打开一个包含文本字段等的新 JPanel。
I would like to be able to make the buttons so they look like normal application buttons I guess, nice and square, equal size etc. etc., I am not sure how to do this though.
我希望能够使按钮看起来像我猜的普通应用程序按钮,漂亮和方形,大小相等等等,但我不知道如何做到这一点。
Also, I am unsure how to open a new JFrame from a button click.
另外,我不确定如何通过单击按钮打开一个新的 JFrame。
GUI Code:
图形用户界面代码:
package practice;
public class UserInterface extends JFrame {
private JButton openReportSelection = new JButton("Open new Window");
private JButton closeButton = new JButton("Close Program");
private JButton getCloseButton() {
return closeButton;
}
private JButton getOpenReportSelection() {
return openReportSelection;
}
public UserInterface() {
mainInterface();
}
private void mainInterface() {
setTitle("Program Information Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel centerPanel = new JPanel(new GridLayout(0, 3));
centerPanel.add(openReportSelection);
centerPanel.add(closeButton);
getCloseButton().addActionListener(new Listener());
add(centerPanel, BorderLayout.CENTER);
setSize(1000, 200);
setVisible(true);
}
private void addReportPanel() {
JPanel reportPanel = createNewPanel();
getContentPane().add(reportPanel, BorderLayout.CENTER);
}
private JPanel createNewPanel() {
JPanel localJPanel = new JPanel();
localJPanel.setLayout(new FlowLayout());
return localJPanel;
}
}
ActionListener Class code:
ActionListener 类代码:
package practice;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Listener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
}
EDIT: I think opening a new JPanel would be the way to go rather than a JFrame. What would be the best way to do this from a Jbutton click?
编辑:我认为打开一个新的 JPanel 将是要走的路,而不是 JFrame。通过 Jbutton 单击执行此操作的最佳方法是什么?
采纳答案by MadProgrammer
Start by using a different layout manager, FlowLayout
or GridBagLayout
might work better
从使用不同的布局管理器开始,FlowLayout
或者GridBagLayout
可能会更好
JPanel centerPanel = new JPanel(new FlowLayout());
centerPanel.add(openReportSelection);
centerPanel.add(closeButton);
These layouts will honour the preferred sizes of your buttons
这些布局将尊重您的按钮的首选大小
As for opening another window, well, you've already create one, so the process is pretty much the same. Having said that, you might consider having a look at The Use of Multiple JFrames: Good or Bad Practice?before you commit yourself to far.
至于打开另一个窗口,好吧,您已经创建了一个,因此过程几乎相同。话虽如此,您可能会考虑查看多个 JFrame 的使用:好的或坏的做法?在你承诺远方之前。
A better approach might be to use a JMenuBar
and JMenuItem
s to act as the "open" and "exit" actions. Take a look at How to Use Menusthen you could use a CardLayout
to switch between views instead, for example
更好的方法可能是使用 aJMenuBar
和JMenuItem
s 作为“打开”和“退出”操作。看看如何使用菜单,然后你可以使用 aCardLayout
在视图之间切换,例如
From a pure design perspective (I know it's only practice, but perfect practice makes perfect), I wouldn't extend anything from JFrame
and instead would rely on building your main GUIs around something like JPanel
instead.
从纯粹的设计角度来看(我知道这只是实践,但完美的实践使完美),我不会扩展任何东西JFrame
,而是依赖于围绕类似的东西构建您的主要 GUI JPanel
。
This affords you the flexibility to decide how to use these components, as you could add them to frames, applets or other components...
这使您可以灵活地决定如何使用这些组件,因为您可以将它们添加到框架、小程序或其他组件...
回答by David Yee
If you want your buttons to have the native Look and Feel (L&F), add the following to your program:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
如果您希望您的按钮具有本机外观 (L&F),请将以下内容添加到您的程序中:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Instead of opening another JFrame
, you'll want to instead use a JDialog
, typically with modality set.
而不是打开另一个JFrame
,你会想要使用 a JDialog
,通常带有模式集。
In Java, you can only extend one class and therefore you should consider carefully whether it is appropriate or not to extend another class. You could ask yourself, "Am I actually extending the functionality of JFrame?" If the answer is no, then you actually want to use an instance variable.
在 Java 中,您只能扩展一个类,因此您应该仔细考虑扩展另一个类是否合适。您可以问自己,“我真的扩展了 JFrame 的功能吗?” 如果答案是否定的,那么您实际上想要使用实例变量。
Below is an example program from the above recommendations:
以下是上述建议的示例程序:
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class MyApplication {
private JFrame myframe; // instance variable of a JFrame
private JDialog mydialog;
public MyApplication() {
super();
myframe = new JFrame(); // instantiation
myframe.setSize(new Dimension(400, 75));
myframe.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JButton btnNewWindow = new JButton("Open New Window");
btnNewWindow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mydialog = new JDialog();
mydialog.setSize(new Dimension(400,100));
mydialog.setTitle("I got you! You can't click on your JFrame now!");
mydialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); // prevent user from doing something else
mydialog.setVisible(true);
}
});
myframe.getContentPane().add(btnNewWindow);
JButton btnCloseProgram = new JButton("Close Program :(");
btnCloseProgram.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myframe.dispose();
}
});
myframe.getContentPane().add(btnCloseProgram);
myframe.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new MyApplication();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
回答by krisg
I am not sure from your question what do wou want to do. Do you want to open a new JFrame or do you want to add a JPanel to the existing frame.
我不确定你的问题想做什么。您是要打开一个新的 JFrame 还是要向现有框架添加一个 JPanel。
To open a new JFrame using a button, create an instance of the JFrame in the actionPerformed method of the button. In your case it would look similar to this:
要使用按钮打开新的 JFrame,请在按钮的 actionPerformed 方法中创建 JFrame 的实例。在您的情况下,它看起来类似于:
openReportSelection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JFrame frame = new JFrame();
// Do something with the frame
}
}
});
回答by Wall-E
You're probably looking up to create and open a new JFrame. For this purpose, first you need to instantiate an object from JFrame Class. As an example, Let's instantiate a new JFrame with specific boundries.
您可能正在寻找创建和打开新 JFrame 的方法。为此,首先需要从 JFrame 类实例化一个对象。例如,让我们实例化一个具有特定边界的新 JFrame。
JFrame testFrame = new testFrame();
verificationFrame.setBounds(400, 100, 250, 250);
Then you need to create your components like JButtons, Jlabels and so on, and next you should add them to your new testFrame object.
然后你需要创建你的组件,比如 JButtons、Jlabels 等等,接下来你应该将它们添加到你的新 testFrame 对象中。
for example, let's create a Jlabel and add it testFrame:
例如,让我们创建一个 Jlabel 并将其添加到 testFrame:
JLabel testLbl = new JLabel("Ok");
testLbl.setBounds(319, 49, 200, 30);
testFrame.getContentPane().add(testLbl);
Now let's suppose you have a Jbutton which is named "jbutton" and by clicking it, a new JFrame object will be created and the Jlabel component will be added to it:
现在让我们假设您有一个名为“jbutton”的 Jbutton,通过单击它,将创建一个新的 JFrame 对象并将 Jlabel 组件添加到其中:
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JFrame testFrame = new testFrame();
verificationFrame.setBounds(400, 100, 250, 250);
Label testLbl = new JLabel("Ok");
testLbl.setBounds(319, 49, 200, 30);
testFrame.getContentPane().add(testLbl);
}}});