2012-12-02 102 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