简体   繁体   English

java swing面板问题

[英]java swing panel problems

I am having problems adding a panel to my main JFrame and hiding it right away, only making it visilble when a button it pressed. 我在将面板添加到主JFrame并将其立即隐藏时遇到了问题,仅在按下按钮时才使其可见。 here is my code. 这是我的代码。 Looking for any insight as to what the problem is. 寻找有关问题所在的任何见解。 Also the label I try to add to the panel doesnt show up either. 我尝试添加到面板上的标签也没有显示。

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

I have no issue with your example. 你的例子我没有问题。

You may want to... 您可能想要...

1- Make sure you've launched your UI in the context of the Event Dispatching Thread 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- Try calling repaint after addPanel.setVisible(true) 2-尝试在addPanel.setVisible(true)之后调用repaint
3- Try calling invalidate after addPanel.setVisible(true) but before repaint if that doesn't work. 3-尝试调用invalidateaddPanel.setVisible(true)但在此之前repaint如果不工作。

Much better solution is to use Card Layout for this kind of work 更好的解决方案是使用Card Layout进行此类工作

UPDATED 更新

After spending some time reading through the code, what I think you seem to be concerned about is the fact that you're "intro" label isn't showing up... 花了一些时间阅读代码后,我认为您似乎担心的是,您的“介绍”标签没有显示出来。

This easily explained. 这很容易解释。 Only one component can exists at any given position within a BorderLayout , so when you add you addPanel , even though it's invisible, it will clobber the intro2 label (effectively removing it from the container). BorderLayout内的任何给定位置上只能存在一个组件,因此,当您添加addPanel ,即使它不可见,它也会intro2标签(有效地将其从容器中移除)。

Below is an example using CardLayout 以下是使用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);
            }
        });
    }
}

Labels are showing up because you have added a panel after adding the labels on the frame so basically the panels are overlapping the labels. 标签之所以显示,是因为您在框架上添加标签后添加了一个面板,因此面板基本上与标签重叠。 Also to show different panels you can use 也显示可以使用的不同面板

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

or you can use CardLayout which makes panels as cards and shows one of them at a time. 或者,您可以使用CardLayout将面板作为卡并一次显示其中之一。

Just edited the code a little but it seems to work - 只是稍微修改了一下代码,但似乎可以正常工作-

在此处输入图片说明

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM