[英]how do you customise position of a JPanel within a 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的位置或邊界也不適用於任何Layout。 我已經在網上在線閱讀了很多其他文章,但是它們似乎都不同且令人困惑,如何控制新JPanel的位置?
通常,我建議使用CardLayout
在不同的視圖之間進行切換,但是很難從可用信息中確定是否有幫助
相反,您可以使用復合布局。 那就是使用不同的布局將一個容器包裝在另一個容器中。
在此示例中,我僅結合使用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);
}
}
}
刪除不同面板的組件而不是直接將其直接添加到GridBagLayout的目的是什么?
因為它們會干擾其他組件的布局。
然后我想要一個小的Jpanel彈出並且不引人注目
您可以使用框架的glassPane
或使用OverlayLayout
例如:
這些信息中的大部分應該在您最初的問題中,它會浪費彼此的時間更少
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.