【问题标题】:Java Swing. Opening a new JPanel from a JButton and making the buttons pretty爪哇摇摆。从 JButton 打开一个新的 JPanel 并使按钮漂亮
【发布时间】:2014-07-23 10:25:19
【问题描述】:

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

我希望能够使按钮看起来像我猜的普通应用程序按钮,漂亮和方形,大小相等等等,但我不知道该怎么做。

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

界面代码:

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);
    }    



}

编辑:我认为打开一个新的 JPanel 比 JFrame 更好。通过点击 Jbutton 来执行此操作的最佳方法是什么?

【问题讨论】:

标签: java swing user-interface netbeans jpanel


【解决方案1】:

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

JPanel centerPanel = new JPanel(new FlowLayout());
centerPanel.add(openReportSelection);     
centerPanel.add(closeButton);    

这些布局将遵循按钮的首选尺寸

至于打开另一个窗口,你已经创建了一个,所以过程几乎相同。话虽如此,您可能会考虑先看看The Use of Multiple JFrames: Good or Bad Practice?,然后再承诺。

更好的方法可能是使用JMenuBarJMenuItems 作为“打开”和“退出”操作。看看How to Use Menus 然后你可以使用CardLayout 在视图之间切换,例如

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

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

【讨论】:

  • 先生,您刚刚回答了我 50% 的问题。非常感谢,非常有帮助。
  • @splunk 希望更新后能接近 75%
  • @MadProgrammer 实际上是的,JPanel 将是要走的路。但是如何在单击按钮时执行新的 JPanel?
  • @Splunk 使用CardLayout,这是它的设计目的,允许您在不同的视图之间切换
【解决方案2】:

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

与其打开另一个JFrame,不如使用JDialog,通常带有模态集。

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

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

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();
                }
            }
        });
    }

}

【讨论】:

  • @CanadianDavid 随便填写,你可能会添加一些我错过的见解
【解决方案3】:

从你的问题中我不确定你想做什么。您是要打开一个新的 JFrame 还是要在现有的框架中添加一个 JPanel。

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

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

【讨论】:

    【解决方案4】:

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

    JFrame testFrame = new testFrame();
    verificationFrame.setBounds(400, 100, 250, 250);
    

    然后您需要创建 JButtons、Jlabels 等组件,接下来您应该将它们添加到新的 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);
    }}});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      相关资源
      最近更新 更多