簡體   English   中英

java swing面板問題

[英]java swing panel problems

我在將面板添加到主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);
 }
}

你的例子我沒有問題。

您可能想要...

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-嘗試在addPanel.setVisible(true)之后調用repaint
3-嘗試調用invalidateaddPanel.setVisible(true)但在此之前repaint如果不工作。

更好的解決方案是使用Card Layout進行此類工作

更新

花了一些時間閱讀代碼后,我認為您似乎擔心的是,您的“介紹”標簽沒有顯示出來。

這很容易解釋。 BorderLayout內的任何給定位置上只能存在一個組件,因此,當您添加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);
            }
        });
    }
}

標簽之所以顯示,是因為您在框架上添加標簽后添加了一個面板,因此面板基本上與標簽重疊。 也顯示可以使用的不同面板

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

或者,您可以使用CardLayout將面板作為卡並一次顯示其中之一。

只是稍微修改了一下代碼,但似乎可以正常工作-

在此處輸入圖片說明

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