如何在Jpanel中自定义JPanel的位置

时间:2018-04-30 20:27:43

标签: java swing layout jpanel

该课程扩展了JPanel,

 public void createDisplay(){
    frame = new JFrame();
    frame.setTitle(title);
    frame.setSize(new Dimension(width, height));
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setPreferredSize(new Dimension(width, height));
    this.setMaximumSize(new Dimension(width, height));
    this.setMinimumSize(new Dimension(width, height));

    this.setLayout(null); //have tried default and BorderLayout
    this.setSize(new Dimension(width, height));
    this.setBounds(0, 0, width, height);

    //basically trying everything

    frame.add(this);
    frame.pack();

}

启动时此代码工作正常,JPanel完全覆盖父框架的大小

但是我的程序后来尝试在类中添加一个新的JPanel扩展JPanel:

    public void gameOverWindow(){ ;
    JPanel panel = new JPanel(new BorderLayout());
    panel.setPreferredSize(new Dimension(200, 100));
    JLabel label = new JLabel("Game Over");
    label.setPreferredSize(new Dimension(40, 15));

   //trying setPosition also doesn't work with BorderLayout or FlowLayout

    JButton button_01 = new JButton("new");
    button_01.setPreferredSize(new Dimension(100, 10));
    JButton button_02 = new JButton("exit");
    button_02.setPreferredSize(new Dimension(100, 10));


    panel.add(label, BorderLayout.NORTH);
    panel.add(button_01, BorderLayout.WEST);
    panel.add(button_02, BorderLayout.EAST);



    this.add(panel);
    this.revalidate();


}

这个新的JPanel显示的内容是正确的BorderLayout格式,但是JPanel本身将保留在扩展JPanel的顶部中心,我知道这是因为默认的Layout设置为FlowLayout,但将此设置为BorderLayout将只是让面板占据整个屏幕。将“布局”设置为“null”会使框架完全中断,并且除了“框架”的“最小化”和“关闭”按钮外,不显示任尝试设置此JPanel的位置或边界也不适用于任何布局。我已经在网上阅读了很多关于此的帖子,但它们似乎都有所不同并且变得混乱,我如何控制我的新JPanel的位置?

1 个答案:

答案 0 :(得分:1)

通常情况下,我建议使用CardLayout在不同视图之间切换,但很难从可用信息中确定是否有帮助

相反,您可以使用复合布局。这是使用不同的布局将一个容器包装在另一个容器中。

在此示例中,我只使用BorderLayoutGridBagLayout

的组合

Game Over Man Game Really Over Man

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            JButton gameOver = new JButton("Game over man");
            gameOver.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JPanel panel = new JPanel(new BorderLayout());
                    panel.add(new JLabel("Game Over Man", JLabel.CENTER), BorderLayout.NORTH);
                    panel.add(new JButton("New"), BorderLayout.WEST);
                    panel.add(new JButton("Exit"), BorderLayout.EAST);

                    removeAll();
                    JPanel inner = new JPanel(new GridBagLayout());
                    inner.add(panel);
                    add(inner);
                    revalidate();
                    repaint();
                }
            });
            JPanel inner = new JPanel(new GridBagLayout());
            inner.add(gameOver);
            add(inner);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}
  

删除不同面板组件的目的是什么,而不是直接将其添加到GridBagLayout?

因为它们会干扰其他组件的布局。

  

然后我想要一个小的Jpanel弹出并且不引人注目

您可以使用框架glassPane或使用OverlayLayout

例如:

这些信息大部分应该出现在你原来的问题中,它会浪费掉彼此的时间