无法将JPanel添加到BorderLayout.CENTER

时间:2015-11-22 18:29:25

标签: java swing jframe

我刚刚开始使用JAVA。 我说过JPanel x,JPanel和BorderLayout JPanel z。 当我尝试从默认的x t y更改z中心的内容时,它可以工作,但它不会返回到x。我在每次之后调用revalidate()。请帮忙。 下面的课程就是问题所在。

以下主要类

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.LayoutManager;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@SuppressWarnings({ "serial", "unused" })
public class Manager extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Manager frame = new Manager();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Manager() {
        setTitle("Popper");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double width = screenSize.getWidth();
        double height = screenSize.getHeight();
        height = height/5.1;
        setSize((int)width, (int)height);
        setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(0,0,0,0));
        setContentPane(contentPane);
        contentPane.setBackground(new Color(14,99,165));
        contentPane.setLayout(new BorderLayout(0, 0));
        ImageIcon image = new ImageIcon("D:/popper26.png");
        setIconImage(image.getImage());
        JPanel pane = new JPanel();
        calcu cal = new calcu();
        curr nup = new curr();
        stopc newst = new stopc();
        pane.setLayout(new FlowLayout(FlowLayout.CENTER));
        JPanel mainpanel = new JPanel();
        BorderLayout x =new BorderLayout(0,0);
        mainpanel.setLayout(x);
        mainpanel.setBackground(Color.WHITE);
        JLabel madeby = new JLabel("Project By Anant Bhasin");
        madeby.setHorizontalAlignment(SwingConstants.RIGHT);
        mainpanel.add(madeby, BorderLayout.SOUTH);
        JPanel logo = new JPanel();
        logo.setLayout(new FlowLayout(FlowLayout.CENTER));
        JLabel jk = new JLabel(new ImageIcon("D:/popper2.png"));
        logo.add(jk, BorderLayout.NORTH);
        logo.setBackground(Color.decode("#1abc9c"));
        mainpanel.add(logo, BorderLayout.NORTH);
        mainpanel.add(cal, BorderLayout.CENTER);
        contentPane.add(mainpanel, BorderLayout.CENTER);
        JPanel newj = new JPanel();
        BoxLayout bxl = new BoxLayout(newj, BoxLayout.PAGE_AXIS);
        newj.setLayout(bxl);
        newj.setBackground(new Color(58,115,144));
        contentPane.add(newj, BorderLayout.WEST);
        Border emptyBorder = BorderFactory.createEmptyBorder();
        JButton calc = new JButton(new ImageIcon("D:/calc.png"));
        newj.add(calc);
        calc.setBorder(emptyBorder);
        calc.setFocusPainted(false);
        calc.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                mainpanel.add(BorderLayout.CENTER, cal);
                mainpanel.revalidate();
            }
        });
        JButton currb = new JButton(new ImageIcon("D:/curr.png"));
        currb.setBorder(emptyBorder);
        newj.add(currb);
        currb.setFocusPainted(false);
        currb.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                mainpanel.add(BorderLayout.CENTER, nup);
                mainpanel.revalidate();
            }
        });
        JButton stop = new JButton(new ImageIcon("D:/stop.png"));
        stop.setBorder(emptyBorder);
        newj.add(stop);
        stop.setFocusPainted(false);
        stop.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                mainpanel.add(BorderLayout.CENTER, newst);
                mainpanel.revalidate();
            }
        });
        JButton timer = new JButton(new ImageIcon("D:/timer.png"));
        timer.setBorder(emptyBorder);
        newj.add(timer);
        timer.setFocusPainted(false);
        JButton memo = new JButton(new ImageIcon("D:/memo.png"));
        memo.setBorder(emptyBorder);
        newj.add(memo);
        memo.setFocusPainted(false);
    }
}

1 个答案:

答案 0 :(得分:4)

由于ZOrder绘制在Swing中的工作方式,BorderLayout不能显示具有相同约束的多个组件。

如果您需要交换面板的功能,那么您应该使用CardLayout

使用CardLayout可以指定要显示的面板的名称。阅读How to Use CardLayout上Swing教程中的部分,了解更多信息和工作示例。

使用以下代码设置布局:

JPanel main = new JPanel( new CardLayout() );
main.add(panelx, "X");
main.add(panely, "Y");

然后交换面板,您可以使用以下代码:

CardLayout cl = (CardLayout)(main.getLayout());
cl.show(main, "X");