Java Swing。从JButton打开一个新的JPanel并使按钮漂亮

Java Swing。从JButton打开一个新的JPanel并使按钮漂亮,java,swing,user-interface,netbeans,jpanel,Java,Swing,User Interface,Netbeans,Jpanel,我正在尝试构建一个小程序,它有一个带有两个按钮的主GUI。一个按钮关闭程序,另一个我想打开一个新的JPanel,它将有文本字段等 我想能够使按钮,使他们看起来像正常的应用程序按钮,我猜,好和方形,大小相等等,等等,我不知道如何做到这一点,虽然 此外,我不确定如何通过单击按钮打开新的JFrame GUI代码: package practice; public class UserInterface extends JFrame { private JButton openReportS

我正在尝试构建一个小程序,它有一个带有两个按钮的主GUI。一个按钮关闭程序,另一个我想打开一个新的JPanel,它将有文本字段等

我想能够使按钮,使他们看起来像正常的应用程序按钮,我猜,好和方形,大小相等等,等等,我不知道如何做到这一点,虽然

此外,我不确定如何通过单击按钮打开新的JFrame

GUI代码:

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类代码:

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:我认为打开一个新的JPanel将是一条路,而不是一个JFrame。通过点击Jbutton来实现这一点的最佳方法是什么?

从使用不同的布局管理器开始,
FlowLayout
gridbagloayout
可能会更好

JPanel centerPanel = new JPanel(new FlowLayout());
centerPanel.add(openReportSelection);     
centerPanel.add(closeButton);    
这些布局将符合您按钮的首选尺寸

至于打开另一个窗口,您已经创建了一个窗口,所以过程基本相同。话虽如此,你也可以考虑在自己承诺之前再看一看。

更好的方法可能是使用
JMenuBar
JMenuItem
s作为“打开”和“退出”操作。例如,看一看,您可以使用在视图之间切换

从纯粹的设计角度来看(我知道这只是实践,但完美的实践造就完美),我不会从
JFrame
扩展任何东西,而是依赖于围绕
JPanel
之类的东西构建主GUI


这为您提供了决定如何使用这些组件的灵活性,因为您可以将它们添加到框架、小程序或其他组件中。

如果您希望按钮具有本机外观(L&F),请将以下内容添加到程序中:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())

您不需要打开另一个
JFrame
,而是需要使用
JDialog
,通常是在模态设置的情况下

在爪哇,你只能扩展一个类,因此你应该仔细考虑是否适当扩展另一个类。你可以问自己,“我真的在扩展JFrame的功能吗?”如果答案是否定的,那么你实际上想要使用一个实例变量

以下是上述建议中的示例程序:


从你的问题我不知道我想做什么。要打开新的JFrame还是要将JPanel添加到现有的frame

要使用按钮打开新的JFrame,请在按钮的actionPerformed方法中创建JFrame的实例。在您的情况下,它看起来与此类似:

    openReportSelection.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            JFrame frame = new JFrame();
            // Do something with the frame
            }
        }
    });

您可能正在查找创建并打开一个新的JFrame。为此,首先需要从JFrame类实例化一个对象。例如,让我们用特定的边界实例化一个新的JFrame

JFrame testFrame = new testFrame();
verificationFrame.setBounds(400, 100, 250, 250);
然后您需要创建组件,比如JButtons、jlabel等等,接下来您应该将它们添加到新的testFrame对象中

例如,让我们创建一个Jlabel并将其添加到testFrame:

JLabel testLbl = new JLabel("Ok");
testLbl.setBounds(319, 49, 200, 30);
testFrame.getContentPane().add(testLbl);
现在,假设您有一个名为“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);
}}});

先生,你刚刚回答了我50%的问题。非常感谢,非常有帮助。@splunk希望,通过update@MadProgrammer事实上,是的,JPanel将是一个不错的选择。但是我怎么能在点击按钮时执行一个新的JPanel呢?@Splunk使用一个
卡片布局
,这就是它的设计目的,允许你在不同的视图之间切换,以及布局填充和边框。你的意思是你想在给定的
JFrame
中打开一个新的
JPanel
,你可能会补充一些我错过的见解
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);
}}});