0

我在向我的主 JFrame 添加面板并立即隐藏它时遇到问题,只有在按下按钮时才使其可见。这是我的代码。寻找有关问题所在的任何见解。我尝试添加到面板的标签也没有显示。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cis2430_a4;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 *
 * @author Tristan
 */
public class MainWindow extends JFrame implements ActionListener{
    public static final int WIDTH = 600;
    public static final int HEIGHT = 700;

    private JPanel addPanel;

public MainWindow()
{
    super("Day Planner");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());


    JLabel intro1 = new JLabel("Welcome to your Day Planner", JLabel.CENTER);
    add(intro1, BorderLayout.NORTH);

    JLabel intro2 = new JLabel("Please choose an option from the menu bar above.", JLabel.CENTER);
    add(intro2, BorderLayout.CENTER);

    JMenu commands = new JMenu("Commands");

    JMenuItem addOption = new JMenuItem("Add");
    addOption.addActionListener(this);
    commands.add(addOption);

    JMenuItem searchOption = new JMenuItem("Search");
    searchOption.addActionListener(this);
    commands.add(searchOption);

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(commands);
    setJMenuBar(menuBar);

    JButton button = new JButton("Add");
    button.addActionListener(this);
    add(button, BorderLayout.SOUTH);

    //add panel
    addPanel = new JPanel();
    addPanel.setLayout(new BorderLayout());
    addPanel.setSize(600,400);
    addPanel.setBackground(Color.CYAN);
    addPanel.add(new JLabel("add panel"), BorderLayout.CENTER);
    add(addPanel, BorderLayout.CENTER);
    addPanel.setVisible(false);


}

 @Override
 public void actionPerformed(ActionEvent ae)
 {
     /*String menuChoice = ae.getActionCommand();

     if (menuChoice.equals("Add")){
         addPanel.setVisible(true);
     }*/
     add(addPanel);
     //addPanel.setVisible(true);
 }
}
4

3 回答 3

2

我对你的例子没有意见。

你可能想...

1-确保您已在事件调度线程的上下文中启动您的 UI

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            MainWindow frame = new MainWindow();
            frame.setVisible(true);
        }
    });
}

2- 尝试repaintaddPanel.setVisible(true)
3- 之后拨打电话-如果这不起作用,请尝试在此invalidate之后addPanel.setVisible(true)但之前拨打电话。repaint

更好的解决方案是使用卡片布局来完成这种工作

更新

在花了一些时间阅读代码之后,我认为你似乎担心的是你的“介绍”标签没有出现......

这很容易解释。在 a 中的任何给定位置只能存在一个组件BorderLayout,因此当您添加 you 时addPanel,即使它是不可见的,它也会破坏intro2标签(有效地将其从容器中删除)。

下面是一个使用示例CardLayout

public class CardWindow extends JFrame implements ActionListener {

    public static final int WIDTH = 600;
    public static final int HEIGHT = 700;
    private JPanel addPanel;

    private JPanel cardPane;
    private CardLayout cardLayout;
    private final JLabel intro2;

    public CardWindow() {
        super("Day Planner");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        cardPane = new JPanel((cardLayout = new CardLayout()));
        add(cardPane, BorderLayout.CENTER);


        JLabel intro1 = new JLabel("Welcome to your Day Planner", JLabel.CENTER);
        add(intro1, BorderLayout.NORTH);

        intro2 = new JLabel("Please choose an option from the menu bar above.", JLabel.CENTER);
        cardPane.add(intro2, "intro");

        JMenu commands = new JMenu("Commands");

        JMenuItem addOption = new JMenuItem("Add");
        addOption.addActionListener(this);
        commands.add(addOption);

        JMenuItem searchOption = new JMenuItem("Search");
        searchOption.addActionListener(this);
        commands.add(searchOption);

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(commands);
        setJMenuBar(menuBar);

        JButton button = new JButton("Add");
        button.addActionListener(this);
        add(button, BorderLayout.SOUTH);

        //add panel
        addPanel = new JPanel();
        addPanel.setLayout(new BorderLayout());
        addPanel.setSize(600, 400);
        addPanel.setBackground(Color.CYAN);
        addPanel.add(new JLabel("add panel"), BorderLayout.CENTER);
        addPanel.setVisible(false);
        cardPane.add(addPanel, "Add");

        cardLayout.show(cardPane, "intro");

    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        String menuChoice = ae.getActionCommand();
        System.out.println(menuChoice);
        if (menuChoice.equals("Add")) {
            cardLayout.show(cardPane, "Add");
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                CardWindow frame = new CardWindow();
                frame.setVisible(true);
            }
        });
    }
}
于 2012-12-03T00:09:40.370 回答
1

标签出现是因为您在框架上添加标签后添加了一个面板,所以基本上面板与标签重叠。还可以显示您可以使用的不同面板

 panel.setVisible(true); //For the panel you want to show and false for others

或者您可以使用 CardLayout 将面板作为卡片并一次显示其中一个。

于 2012-12-03T00:00:39.933 回答
1

只是稍微编辑了代码,但它似乎工作 -

在此处输入图像描述

public class MainWindow extends JFrame implements ActionListener{
    public static final int WIDTH = 600;
    public static final int HEIGHT = 700;

    private JPanel addPanel;

    public static void main(String[] args) {
        MainWindow mainWindow = new MainWindow();
        mainWindow.setVisible(true);
    }
public MainWindow()
{
    super("Day Planner");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());


    JLabel intro1 = new JLabel("Welcome to your Day Planner", JLabel.CENTER);
    add(intro1, BorderLayout.NORTH);

    JLabel intro2 = new JLabel("Please choose an option from the menu bar above.", JLabel.CENTER);
    add(intro2, BorderLayout.CENTER);

    JMenu commands = new JMenu("Commands");

    JMenuItem addOption = new JMenuItem("Add");
    addOption.addActionListener(this);
    commands.add(addOption);

    JMenuItem searchOption = new JMenuItem("Search");
    searchOption.addActionListener(this);
    commands.add(searchOption);

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(commands);
    setJMenuBar(menuBar);

    JButton button = new JButton("Add");
    button.addActionListener(this);
    add(button, BorderLayout.SOUTH);

    //add panel
    addPanel = new JPanel();
    addPanel.setLayout(new BorderLayout());
    addPanel.setSize(600,400);
    addPanel.setBackground(Color.CYAN);
    addPanel.add(new JLabel("add panel"), BorderLayout.CENTER);
    add(addPanel, BorderLayout.CENTER);
    addPanel.setVisible(false);


}

 @Override
 public void actionPerformed(ActionEvent ae)
 {
     String menuChoice = ae.getActionCommand();

     if (menuChoice.equals("Add")){
         addPanel.setVisible(true);
     }
     add(addPanel);
     //addPanel.setVisible(true);
 }
}
于 2012-12-03T00:08:31.687 回答