2012-12-02 99 views
0

我有添加面板到我的主要的JFrame問題和隱藏它的時候了,只有使它visilble當按鈕在按下狀態。這是我的代碼。尋找任何有關問題的見解。此外,我嘗試添加到面板的標籤也不會顯示出來。的Java Swing面板問題

/* 
* 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

標籤被顯示出來,因爲你已經在框架所以基本上面板重疊標籤上添加標籤後添加的面板。 同時顯示不同的面板,你可以使用

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

,或者您可以使用CardLayout這使得面板的卡,並顯示其中一人在一個時間。

+0

我只是嘗試,說:「加板」面板上添加一個標籤的例子。我想讓其他人被小組掩蓋。我確實使用panel.setVisible(false),它不隱藏面板。 –

+0

可能應該已經嘗試過,然後再打折......謝謝。 –

1

剛纔編輯的代碼一點點,但它似乎工作 -

enter image description here

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

你做了什麼編輯,爲什麼? – MadProgrammer

+0

沒有編輯,但只是取消了註釋行......示例代碼正在工作......另外,請參閱CardLayout的此問題http://stackoverflow.com/questions/11338229/how-to-make-an-added-jpanel-visible - 內部-A-家長的JPanel/11338493#11338493 – Chan

+1

哦,我同意,對我來說,工作得很好,只是好奇任何變化;)... – MadProgrammer

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-嘗試addPanel.setVisible(true)
3後調用repaint - 嘗試在addPanel.setVisible(true)之後撥打invalidate,但在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); 
      } 
     }); 
    } 
} 
+0

之後,當我選擇了命令菜單欄中的選項時,我需要面板顯示並隱藏。按鈕在那裏,看看我是否可以用它來隱藏。 –

+0

不會有任何區別。您展示面板的方式無關緊要,基本概念保持不變。我仍然使用'CardLayout'來完成這項工作,因爲它會讓你的生活變得容易很長很長的錯誤 – MadProgrammer

+0

@TristanPearce我已經用一個例子更新了答案,我認爲它會解決你的基本問題 – MadProgrammer