Java摇摆.从JButton打开新的JPanel并使按钮漂亮 [英] Java Swing. Opening a new JPanel from a JButton and making the buttons pretty

查看:280
本文介绍了Java摇摆.从JButton打开新的JPanel并使按钮漂亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个带有2个按钮的主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);
    }    



}

我认为打开新的JPanel而不是JFrame是一种方法.通过Jbutton单击执行此操作的最佳方法是什么?

解决方案

从使用其他布局管理器开始,FlowLayoutGridBagLayout可能会更好

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

这些布局将采用您喜欢的按钮尺寸

关于打开另一个窗口,好了,您已经创建了一个窗口,因此过程几乎相同.话虽如此,您可以考虑看看使用多个JFrame :好的还是不好的做法?,然后再致力于.

更好的方法可能是使用JMenuBarJMenuItem充当打开"和退出"动作.看一看如何使用菜单,那么您可以使用例如,使用 CardLayout 可以在视图之间进行切换

从纯粹的设计角度(我知道这只是实践,但是完美的实践才是完美的),我不会扩展JFrame的任何内容,而是依赖于围绕JPanel之类的主GUI进行构建. /p>

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

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.

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.

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:

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?

解决方案

Start by using a different layout manager, FlowLayout or GridBagLayout might work better

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.

A better approach might be to use a JMenuBar and JMenuItems to act as the "open" and "exit" actions. Take a look at How to Use Menus then you could use a CardLayout to switch between views instead, for example

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.

This affords you the flexibility to decide how to use these components, as you could add them to frames, applets or other components...

这篇关于Java摇摆.从JButton打开新的JPanel并使按钮漂亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆