这个JPanel代码好吗?单击按钮时不显示其他JPanel [英] Is this JPanel code good? Not showing the other JPanel when clicked on the button

查看:107
本文介绍了这个JPanel代码好吗?单击按钮时不显示其他JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当有人点击按钮时,如何显示第二个JPanel?
代码给出错误 - 未知来源。我缺少什么?

How to get that second JPanel to show when someone clicked on the button? The code is giving an error - unknown source. What am I missing?

package Com.global;

import java.awt.BorderLayout;

public class Govinda extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JPanel panel;
    private JPanel panel_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Govinda frame = new Govinda();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Govinda() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new CardLayout(0, 0));

        final JPanel panel = new JPanel();
        contentPane.add(panel, "name_273212774632866");
        panel.setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                panel_1.setVisible(true);
                panel.setVisible(true);

            }
        });
        btnNewButton.setBounds(126, 105, 89, 23);
        panel.add(btnNewButton);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, "name_273214471684839");
        panel_1.setLayout(null);

        JLabel lblHaiiiiiiiii = new JLabel("HAIIIIIIIII");
        lblHaiiiiiiiii.setBounds(159, 129, 46, 14);
        panel_1.add(lblHaiiiiiiiii);
    }
}


推荐答案


这个JPanel代码好吗?

Is this JPanel code good?

不是真的(对不起),你应该使用 CardLayout 要使其正常工作,请参阅如何使用CardLayout

Not really (sorry), you should be using a CardLayout to get this to work, see How to Use CardLayout

您还通过重新声明变量来隐藏变量(面板并且 panel_1 )在你的构造函数中。

You're also shadowing your variables by redeclaring your variables (panel and panel_1) inside your constructor.

你还应该避免使用 null 布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理人员合作,放弃这些将导致问题和问题的结束,您将花费越来越多的时间来纠正

You should also avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

你也永远不要将 panel_1 添加到 contentPane ,以便调用 setVisible on它(假设变量阴影是固定的)将不会做任何事情

You also never add panel_1 to the contentPane so calling setVisible on it (assuming the variable shadowing is fixed) won't do anything

通常不鼓励直接从 JFrame扩展或其他顶级容器,你没有在类中添加任何新功能并将自己锁定在一个用例中

It's also generally discouraged to extend directly from JFrame or other top level containers, you're not adding any new functionality to the class and are locking yourself into a single use case

所以你可能会做一些事情更像是......

So you might, instead, do something more like...

import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Govinda());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Govinda extends JPanel {

        /**
         *
         */
        private static final long serialVersionUID = 1L;
        private JPanel panel;
        private JPanel panel_1;

        /**
         * Create the frame.
         */
        public Govinda() {
            setBorder(new EmptyBorder(5, 5, 5, 5));
            setLayout(new CardLayout(0, 0));

            JPanel panel = new JPanel();
            add(panel, "name_273212774632866");

            JButton btnNewButton = new JButton("New button");
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ((CardLayout) getLayout()).show(Govinda.this, "name_273214471684839");
                }
            });
            panel.add(btnNewButton);

            JPanel panel_1 = new JPanel();
            add(panel_1, "name_273214471684839");

            JLabel lblHaiiiiiiiii = new JLabel("HAIIIIIIIII");
            panel_1.add(lblHaiiiiiiiii);
        }
    }
}

您可能还考虑使用更多模型 - 视图 - 控制器方法导航,因此用于确定下一个视图的逻辑将由控制器根据模型的要求进行,例如,查看听众放置坚持传统(非中介)MVC模式

You might also consider using more a Model-View-Controller approach to the navigation, so the logic that is used to determine which view is next would be made by a controller, based on the requirements of a model, for example, have a look at Listener Placement Adhering to the Traditional (non-mediator) MVC Pattern

这篇关于这个JPanel代码好吗?单击按钮时不显示其他JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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