简体   繁体   English

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

[英]how do you customise position of a JPanel within a Jpanel

The class extends 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();

}

on startup this code works fine and the JPanel completely covers the size of the Parent frame 在启动时,此代码可以正常工作,并且JPanel完全覆盖了父框架的大小

However my program later tries to add a new JPanel to the class's extend JPanel with: 但是,我的程序稍后尝试使用以下命令向类的扩展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();


}

This new JPanel appears with the contents within the correct BorderLayout format, however the JPanel itself will remain at the top center of the extended JPanel, I know this is because the default Layout is set to FlowLayout, however setting this to BorderLayout will just cause the panel to take up the entire screen. 这个新的JPanel的内容以正确的BorderLayout格式显示,但是JPanel本身将保持在扩展JPanel的顶部中心,我知道这是因为默认Layout设置为FlowLayout,但是将其设置为BorderLayout只会导致面板占据整个屏幕。 Setting the Layout to null completely breaks the frame and nothing appears but the Minimize and Close buttons of the Frame. 将布局设置为null会完全破坏框架,除了框架的“最小化”和“关闭”按钮以外,什么都不会出现。 Trying to set the position or Bounds of this JPanel doesn't work with any Layout either. 尝试设置此JPanel的位置或边界也不适用于任何Layout。 I have read a lot of other post online about this but they all seem to differ and become confusing, how do I gain control of the position of my new JPanel? 我已经在网上在线阅读了很多其他文章,但是它们似乎都不同且令人困惑,如何控制新JPanel的位置?

Normally I'd recommend using a CardLayout for switching between different views, but it's difficult to ascertain from the available information if that would help or not 通常,我建议使用CardLayout在不同的视图之间进行切换,但是很难从可用信息中确定是否有帮助

Instead, you could make use of compounding layouts. 相反,您可以使用复合布局。 That is wrap one container in another using different layouts. 那就是使用不同的布局将一个容器包装在另一个容器中。

In this example, I simply use a combination of BorderLayout and GridBagLayout 在此示例中,我仅结合使用BorderLayoutGridBagLayout

游戏结束 游戏真的超过了男人

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

    }

}

what is the purpose of removing the components of a different panel instead of just directly adding it to GridBagLayout? 删除不同面板的组件而不是直接将其直接添加到GridBagLayout的目的是什么?

Because they interfere with the layout of other components. 因为它们会干扰其他组件的布局。

i then want a small Jpanel to popup within and be unobtrusive 然后我想要一个小的Jpanel弹出并且不引人注目

You could make use of the frame's glassPane or use a OverlayLayout 您可以使用框架的glassPane或使用OverlayLayout

For example: 例如:

Much of this information should have been in your original question, it would have wasted less of each other's time 这些信息中的大部分应该在您最初的问题中,它会浪费彼此的时间更少

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

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