简体   繁体   English

无法将JPanel添加到BorderLayout.CENTER

[英]Can't add JPanel to BorderLayout.CENTER

I am just Starting out with JAVA. 我只是从JAVA开始。 I have say a JPanel x, a JPanel y and a BorderLayout JPanel z. 我说过一个JPanel x,一个JPanel y和BorderLayout JPanel z。 When I try to change the contents of the center of z from default xty, it works but it doesn't go back to x. 当我尝试从默认xty更改z中心的内容时,它可以工作,但不会回到x。 I AM calling revalidate() after each. 我在每次之后都调用revalidate()。 Help please. 请帮助。 The class below is where the problem is. 下面的类是问题所在。

Main Class Below 主班以下

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

A BorderLayout is not designed to display multiple components with the same constraint because of the way ZOrder painting works in Swing. 由于ZOrder绘画在Swing中的工作方式,因此BorderLayout不能设计为显示具有相同约束的多个组件。

If you need the ability to swap panels, then you should be using a CardLayout . 如果需要交换面板的功能,则应使用CardLayout

A CardLayout lets you specify the name of the panel that you want to display. CardLayout使您可以指定要显示的面板的名称。 Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples. 阅读Swing教程中有关如何使用CardLayout的部分, 获取更多信息和工作示例。

You set up the layout with code like: 您可以使用以下代码设置布局:

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

Then to swap a panel you use code like: 然后使用以下代码交换面板:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM