[英]How to move the button to the center?
以下代碼使用BoxLayout
創建一個JDialog
。
public static void main(String[] args) {
// TODO Auto-generated method stub
Border border1 = BorderFactory.createLineBorder(Color.GREEN, 5);
JDialog j7=new JDialog();
JPanel j8=new JPanel();
JButton j10=new JButton("OK");
BoxLayout c1=new BoxLayout(j8,1);
j8.setLayout(c1);
j8.setBorder(border1);
JLabel j9=new JLabel("Yeeks!!Game was about to crash.We managed it.Numbers Only!");
j9.setFont(new Font("Serif", Font.BOLD, 25));
j8.add(j9);
j8.add(j10,1);
j7.add(j8);
Dimension d=new Dimension(710,200);
j7.setSize(d);
j7.setTitle("Humans");
j7.setEnabled(true);
j7.setVisible(true);
j7.setLocation(400, 200);
}
我如何居中JButton
?
另一個問題是,當我更改JDialog的大小時,組件沒有調整大小。
使組件在框架/對話框上居中的最簡單方法是使用GridBagLayout。
JPanel panel = new JPanel( new GridBagLayout() );
panel.add(new JButton("Centered"), new GridBagConstraints());
frame.add(panel, BorderLayout.CENTER);
如果要使用BoxLayout,則需要在第一個組件之前和最后一個組件之后添加“膠水”。 閱讀Swing教程中有關如何使用BoxLayout的部分, 以了解更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.