重叠面板摆动

时间:2013-05-10 01:25:08

标签: java swing

我正在尝试执行以下布局,当程序启动时,除了panel2之外,所有JPanel都可见。当用户单击btn1时,JCalendar和panel3将设置为不可见,并且panel2变为可见。我遇到的问题是panel2没有显示btn1被点击。但是,如果我将panel2的borderlayout更改为未使用的(在这种情况下为WEST),它将显示单击按钮的时间,但它在左侧对齐,我希望它在整个表单中居中。

代码:

public class GUI extends JFrame implements ActionListener, PropertyChangeListener
{
    private JPanel panel1, panel2, panel3;
    private com.toedter.calendar.JCalendar calendar;
    private Button btn1, btn2;
    private JLabel label1, label2;

public GUI()
{
    init();
}

private void init()
{
    //panel1 components
    panel1 = new JPanel();
    btn1 = new JButton("Click me");
    panel1.add(btn1);

    //panel2 components
    panel2 = new JPanel();
    label1 = new JLabel("Time:");
    label2 = new JLabel("Date:");
    panel2.add(label1); panel2.add(label2);

    //JCalendar
    calendar = new com.toedter.calendar.JCalendar();

    //panel3
    panel3 = new JPanel();

    //Add panels to JFrame
    add(panel1, BorderLayout.NORTH);
    add(calendar, BorderLayout.CENTER);
    add(panel2, BorderLayout.CENTER); //if i set this to WEST it show!!
    add(panel3, BorderLayout.EAST);

    //event handling
    btn1.addActionListener(this);

    //hide panel2
    panel2.setVisible(false); 
    pack();
}

public void actionPerformed(ActionEvent ae)
{
    if(ae.getSource().equals(btn1)
    {
        calendar.setVisible(false);
        panel3.setVisible(false);
        panel2.setVisible(true); //make panel2 visible
        panel2.updateUI();
        revalidate();
        repaint();
    }
}

public static void main(String args[])
{
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            new GUI().setVisible(true);
        }
    });
}

enter image description here

当我点击btn1时,JCalendar和panel3是不可见的但是panel2没有显示

enter image description here

2 个答案:

答案 0 :(得分:4)

我可以找到许多问题......

  • BorderLayout只允许单个组件占用任何给定的位置。也就是说,两个组件不能同时共享CENTER位置,无论其中一个是不可见的。
  • 你永远不应该调用updateUI,这用于告诉UI组件外观已经改变,应该更新它们以响应它。
  • 使用revalidate告诉容器布局发生了一些变化,它应该执行新的布局过程......

点击之前......

enter image description here

点击后......

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class GUI extends JFrame implements ActionListener, PropertyChangeListener {

    private JPanel panel1, panel2, panel3;
//    private com.toedter.calendar.JCalendar calendar;
    private JPanel calendar;
    private JButton btn1, btn2;
    private JLabel label1, label2;

    public GUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        init();
    }

    private void init() {
        //panel1 components
        panel1 = new JPanel();
        btn1 = new JButton("Click me");
        panel1.add(btn1);

        //panel2 components
        panel2 = new JPanel();
        label1 = new JLabel("Time:");
        label2 = new JLabel("Date:");

        panel2.add(label1);
        panel2.add(label2);

        //JCalendar
        calendar = new JPanel();//new com.toedter.calendar.JCalendar();
        calendar.setBorder(new LineBorder(Color.RED));
        calendar.add(new JLabel("Calendar"));

        //panel3
        panel3 = new JPanel();
        panel3.setBorder(new LineBorder(Color.BLUE));
        panel3.add(new JLabel("Panel3"));

        panel2.setBorder(new LineBorder(Color.GREEN));

        //Add panels to JFrame
        add(panel1, BorderLayout.NORTH);
        add(calendar, BorderLayout.WEST);
        add(panel2, BorderLayout.CENTER);
        add(panel3, BorderLayout.EAST);

        //event handling
        btn1.addActionListener(this);

        //hide panel2
        panel2.setVisible(false);
        pack();
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource().equals(btn1)) {
            calendar.setVisible(false);
            panel3.setVisible(false);
            panel2.setVisible(true); //make panel2 visible
//            panel2.updateUI();
            revalidate();
            repaint();
        }
    }

    public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new GUI().setVisible(true);
            }
        });

    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
    }
}

现在,我很确定这不会满足你的所有要求(我看到它们)......你至少有两个选择......

  1. 删除Calendar组件,并在点击按钮时将panel2添加到CENTER位置
  2. 最好使用CardLayout

答案 1 :(得分:1)

BorderLayout没有重叠,IIRC没有任何布局管理器关于“重叠布局”。

您需要以不同的方式执行此操作 - 尝试将JLayeredPane与您现有的JPanel& BorderLayout作为底层,然后你的(可选)面板作为顶层。

请参阅:http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html