该类扩展了 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 的顶部中心,我知道这是因为默认布局设置为 FlowLayout,但是将其设置为 BorderLayout 将只是使面板占据整个屏幕。将布局设置为 null 会完全破坏框架,除了框架的最小化和关闭按钮之外,不会出现任何内容。尝试设置此 JPanel 的位置或边界也不适用于任何布局。我在网上阅读了很多关于此的其他帖子,但它们似乎都不同并且变得令人困惑,我如何控制我的新 JPanel 的位置?
最佳答案
通常我建议使用 CardLayout
用于在不同 View 之间切换,但很难从可用信息中确定这是否有帮助
相反,您可以使用复合布局。即使用不同的布局将一个容器包装在另一个容器中。
在此示例中,我简单地使用了 BorderLayout
和 GridBagLayout
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?
因为它们会干扰其他组件的布局。
i then want a small Jpanel to popup within and be unobtrusive
您可以使用框架的glassPane
或使用OverlayLayout
例如:
- Floating JPanel above a JPanel with BorderLayout
- Rectangle is not drawn on top
- Placing a marker within the image
- Display a message on the screen
大部分信息应该在您最初的问题中,这样可以减少彼此浪费的时间
关于java - 如何自定义 JPanel 在 Jpanel 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50107734/