没有从 JPanel 显示的 GUI 组件 [英] No GUI Component showing from a JPanel

查看:35
本文介绍了没有从 JPanel 显示的 GUI 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为自己编写一个小的密码管理器,我的问题是第一个面板没有显示(JPanel menue)(下图).如果我启用仅显示第二个面板(JPanel pwPanel),它将显示.但是,如果我切换它,则不会显示面板.

public static void main(String[] args) {Font font = new Font("Arial", Font.BOLD, 35);Font font2 = new Font("Arial", Font.BOLD, 20);//框架gui = new JFrame(DataKeeper-0.1");gui.setSize(700, 500);gui.setLocationRelativeTo(null);gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//面板menue = new JPanel();menue.setLayout(new BoxLayout(menue, BoxLayout.PAGE_AXIS));menue.setBorder(new EmptyBorder(new Insets(50, 50, 50, 50)));menue.setBackground(Color.DARK_GRAY);menue.setVisible(true);gui.add(菜单);pwPanel = new JPanel();pwPanel.setLayout(new BoxLayout(pwPanel, BoxLayout.PAGE_AXIS));pwPanel.setBorder(new EmptyBorder(new Insets(50, 50, 50, 50)));pwPanel.setBackground(Color.DARK_GRAY);pwPanel.setVisible(false);gui.add(pwPanel);//标签welcLabel = new JLabel(欢迎使用 DataKeeper");welcLabel.setForeground(Color.WHITE);welcLabel.setFont(字体);welcLabel.setHorizo​​ntalAlignment(SwingConstants.CENTER);menue.add(welcLabel);pwLabel = new JLabel(密码管理器");pwLabel.setForeground(Color.WHITE);pwLabel.setFont(字体);pwLabel.setHorizo​​ntalAlignment(SwingConstants.CENTER);pwPanel.add(pwLabel);//纽扣bPwSw = new JButton(密码管理器");bPwSw.setFont(font2);bPwSw.addActionListener(new ButtonListener());menue.add(bPwSw);bGvFeedbc = new JButton(提供反馈");bGvFeedbc.setFont(font2);bGvFeedbc.addActionListener(new ButtonListener());menue.add(bGvFeedbc);bNewPw = new JButton("+|新项目");bNewPw.addActionListener(new ButtonListener());bNewPw.setFont(font2);pwPanel.add(bNewPw);bShowPw = new JButton(显示密码");bShowPw.addActionListener(new ButtonListener());bShowPw.setFont(font2);pwPanel.add(bShowPw);gui.setVisible(true);//FileManager.readItems();//FileManager.arrayInfo();}

当您左键单击密码管理器 JButton 时,您会看到此显示.

Oracle 教程,使用 JFC/Swing 创建 GUI,将向您展示如何创建 Swing GUI.跳过 Netbeans 部分.

我通过调用 SwingUtilities invokeLater 方法启动了应用程序.此方法确保在 事件调度线程上创建和执行 Swing 组件.

我将您的代码分组到方法中.run 方法创建唯一的JFrame.私有 JPanel 方法创建卡片布局 JPanel,菜单JPanel,密码JPanel.

我创建了一个匿名 ActionListener 类来显示菜单 JPanel 中的密码 JPanel.

将您的代码分成易于理解的小方法.

这是完整的、可运行的代码.

import java.awt.BorderLayout;导入 java.awt.CardLayout;导入 java.awt.Color;导入 java.awt.Font;导入 java.awt.Insets;导入 java.awt.event.ActionEvent;导入 java.awt.event.ActionListener;导入 javax.swing.BoxLayout;导入 javax.swing.JButton;导入 javax.swing.JFrame;导入 javax.swing.JLabel;导入 javax.swing.JPanel;导入 javax.swing.SwingConstants;导入 javax.swing.SwingUtilities;导入 javax.swing.border.EmptyBorder;公共类 DataKeeperGUI 实现 Runnable {公共静态无效主(字符串 [] args){SwingUtilities.invokeLater(new DataKeeperGUI());}私人最终 CardLayout cardLayout;私人 JPanel 主面板;公共 DataKeeperGUI() {this.cardLayout = new CardLayout();}@覆盖公共无效运行(){//框架JFrame gui = new JFrame(DataKeeper-0.1");gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.mainPanel = createMainPanel();gui.add(mainPanel, BorderLayout.CENTER);gui.setSize(700, 500);gui.setLocationRelativeTo(null);gui.setVisible(true);}私人 JPanel createMainPanel() {JPanel 面板 = 新 JPanel(cardLayout);panel.add(createMenuPanel(), 菜单");panel.add(createPasswordPanel(), 密码");返回面板;}私人 JPanel createMenuPanel() {//面板JPanel menue = new JPanel();menue.setLayout(new BoxLayout(menue, BoxLayout.PAGE_AXIS));menue.setBorder(new EmptyBorder(new Insets(50, 50, 50, 50)));menue.setBackground(Color.DARK_GRAY);Font font = new Font("Arial", Font.BOLD, 35);Font font2 = new Font("Arial", Font.BOLD, 20);//标签JLabel welcLabel = new JLabel(欢迎使用 DataKeeper");welcLabel.setForeground(Color.WHITE);welcLabel.setFont(字体);welcLabel.setHorizo​​ntalAlignment(SwingConstants.CENTER);menue.add(welcLabel);//纽扣JButton bPwSw = new JButton(密码管理器");bPwSw.setFont(font2);bPwSw.addActionListener(new ActionListener() {@覆盖public void actionPerformed(ActionEvent e) {cardLayout.show(mainPanel, 密码");}});menue.add(bPwSw);JButton bGvFeedbc = new JButton(提供反馈");bGvFeedbc.setFont(font2);//bGvFeedbc.addActionListener(new ButtonListener());menue.add(bGvFeedbc);返回菜单;}私人 JPanel createPasswordPanel() {JPanel pwPanel = new JPanel();pwPanel.setLayout(new BoxLayout(pwPanel, BoxLayout.PAGE_AXIS));pwPanel.setBorder(new EmptyBorder(new Insets(50, 50, 50, 50)));pwPanel.setBackground(Color.DARK_GRAY);Font font = new Font("Arial", Font.BOLD, 35);Font font2 = new Font("Arial", Font.BOLD, 20);JLabel pwLabel = new JLabel(密码管理器");pwLabel.setForeground(Color.WHITE);pwLabel.setFont(字体);pwLabel.setHorizo​​ntalAlignment(SwingConstants.CENTER);pwPanel.add(pwLabel);JButton bNewPw = new JButton("+|新项目");//bNewPw.addActionListener(new ButtonListener());bNewPw.setFont(font2);pwPanel.add(bNewPw);JButton bShowPw = new JButton(显示密码");//bShowPw.addActionListener(new ButtonListener());bShowPw.setFont(font2);pwPanel.add(bShowPw);返回 pwPanel;}}

I am programming a little Password-Manager for myself and my problem is, that the first Panel isn't shown (JPanel menue)(image down below). If I enable that the 2nd Panel(JPanel pwPanel) is shown only, it will show. But if I switch it the panel isn't shown.

public static void main(String[] args) {
    Font font = new Font("Arial", Font.BOLD, 35);
    Font font2 = new Font("Arial", Font.BOLD, 20);
    
    //Frame
    gui = new JFrame("DataKeeper-0.1");
    gui.setSize(700, 500);
    gui.setLocationRelativeTo(null);
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    //Panels
    menue = new JPanel();
    menue.setLayout(new BoxLayout(menue, BoxLayout.PAGE_AXIS));
    menue.setBorder(new EmptyBorder(new Insets(50, 50, 50, 50)));
    menue.setBackground(Color.DARK_GRAY);
    menue.setVisible(true);
    gui.add(menue);
    
    pwPanel = new JPanel();
    pwPanel.setLayout(new BoxLayout(pwPanel, BoxLayout.PAGE_AXIS));
    pwPanel.setBorder(new EmptyBorder(new Insets(50, 50, 50, 50)));
    pwPanel.setBackground(Color.DARK_GRAY);
    pwPanel.setVisible(false);
    gui.add(pwPanel);
    
    //Labels
    welcLabel = new JLabel("Welcome to DataKeeper");
    welcLabel.setForeground(Color.WHITE);
    welcLabel.setFont(font);
    welcLabel.setHorizontalAlignment(SwingConstants.CENTER);
    menue.add(welcLabel);
    
    pwLabel = new JLabel("Password-Manager");
    pwLabel.setForeground(Color.WHITE);
    pwLabel.setFont(font);
    pwLabel.setHorizontalAlignment(SwingConstants.CENTER);
    pwPanel.add(pwLabel);
    
    //buttons
    bPwSw = new JButton("Password Manager");
    bPwSw.setFont(font2);
    bPwSw.addActionListener(new ButtonListener());
    menue.add(bPwSw);
    
    bGvFeedbc = new JButton("Give Feedback");
    bGvFeedbc.setFont(font2);
    bGvFeedbc.addActionListener(new ButtonListener());
    menue.add(bGvFeedbc);
    
    bNewPw = new JButton("+|New Item");
    bNewPw.addActionListener(new ButtonListener());
    bNewPw.setFont(font2);
    pwPanel.add(bNewPw);
    
    bShowPw = new JButton("Show Passwords");
    bShowPw.addActionListener(new ButtonListener());
    bShowPw.setFont(font2);
    pwPanel.add(bShowPw);
    
    gui.setVisible(true);
    
    //FileManager.readItems();
    //FileManager.arrayInfo();
}

Thats what it looks linke, when I run the Program

解决方案

I rearranged your code and came up with the following GUI.

When you left-click on the Password Manager JButton, you get this display.

The Oracle tutorial, Creating a GUI With JFC/Swing, will show you how to create a Swing GUI. Skip the Netbeans section.

I started the application with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

I grouped your code into methods. The run method creates the one and only JFrame. The private JPanel methods create the card layout JPanel, the menu JPanel, and the password JPanel.

I created an anonymous ActionListener class to show the password JPanel from the menu JPanel.

Group your code into small, easy to understand, methods.

Here's the complete, runnable code.

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class DataKeeperGUI implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new DataKeeperGUI());
    }
    
    private final CardLayout cardLayout;
    
    private JPanel mainPanel;
    
    public DataKeeperGUI() {
        this.cardLayout = new CardLayout();
    }

    @Override
    public void run() {
        // Frame
        JFrame gui = new JFrame("DataKeeper-0.1");
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        this.mainPanel = createMainPanel(); 
        gui.add(mainPanel, BorderLayout.CENTER);

        gui.setSize(700, 500);
        gui.setLocationRelativeTo(null);
        gui.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(cardLayout);
        panel.add(createMenuPanel(), "menu");
        panel.add(createPasswordPanel(), "password");
        
        return panel;
    }

    private JPanel createMenuPanel() {
        // Panels
        JPanel menue = new JPanel();
        menue.setLayout(new BoxLayout(menue, BoxLayout.PAGE_AXIS));
        menue.setBorder(new EmptyBorder(new Insets(50, 50, 50, 50)));
        menue.setBackground(Color.DARK_GRAY);

        Font font = new Font("Arial", Font.BOLD, 35);
        Font font2 = new Font("Arial", Font.BOLD, 20);

        // Labels
        JLabel welcLabel = new JLabel("Welcome to DataKeeper");
        welcLabel.setForeground(Color.WHITE);
        welcLabel.setFont(font);
        welcLabel.setHorizontalAlignment(SwingConstants.CENTER);
        menue.add(welcLabel);

        // buttons
        JButton bPwSw = new JButton("Password Manager");
        bPwSw.setFont(font2);
        bPwSw.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(mainPanel, "password");
            }
        });
        menue.add(bPwSw);

        JButton bGvFeedbc = new JButton("Give Feedback");
        bGvFeedbc.setFont(font2);
//      bGvFeedbc.addActionListener(new ButtonListener());
        menue.add(bGvFeedbc);

        return menue;
    }

    private JPanel createPasswordPanel() {
        JPanel pwPanel = new JPanel();
        pwPanel.setLayout(new BoxLayout(pwPanel, BoxLayout.PAGE_AXIS));
        pwPanel.setBorder(new EmptyBorder(new Insets(50, 50, 50, 50)));
        pwPanel.setBackground(Color.DARK_GRAY);

        Font font = new Font("Arial", Font.BOLD, 35);
        Font font2 = new Font("Arial", Font.BOLD, 20);

        JLabel pwLabel = new JLabel("Password-Manager");
        pwLabel.setForeground(Color.WHITE);
        pwLabel.setFont(font);
        pwLabel.setHorizontalAlignment(SwingConstants.CENTER);
        pwPanel.add(pwLabel);

        JButton bNewPw = new JButton("+|New Item");
//      bNewPw.addActionListener(new ButtonListener());
        bNewPw.setFont(font2);
        pwPanel.add(bNewPw);

        JButton bShowPw = new JButton("Show Passwords");
//      bShowPw.addActionListener(new ButtonListener());
        bShowPw.setFont(font2);
        pwPanel.add(bShowPw);

        return pwPanel;
    }

}

这篇关于没有从 JPanel 显示的 GUI 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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