当前位置: 技术问答>java相关
如何让一个Dialog里面的Ok和Cancel两个按钮居中呀?
来源: 互联网 发布时间:2015-07-24
本文导语: 我用的是GridBagLayout,Dialog上方是一行文字,下方有两个按钮,一个是Ok,一个是Cancel,可我怎么都不能把他们象其他语言里面那样居中。 GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstr...
我用的是GridBagLayout,Dialog上方是一行文字,下方有两个按钮,一个是Ok,一个是Cancel,可我怎么都不能把他们象其他语言里面那样居中。
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill=GridBagConstraints.HORIZONTAL;
constraints.insets=new Insets(3,1,3,1);
okButton = new Button(" Ok ");
canButton = new Button("Cancle");
Label messageLabel = new Label(message);
setLayout(gridbag);
constraints.gridwidth=2;
constraints.gridheight=1;
constraints.gridx=0;
constraints.gridy=0;
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(messageLabel, constraints);
add(messageLabel);
constraints.fill=GridBagConstraints.NONE;
constraints.gridwidth=1;
constraints.gridy++;
constraints.gridx=0;
gridbag.setConstraints(okButton, constraints);
okButton.addActionListener(this);
add(okButton);
constraints.anchor=GridBagConstraints.WEST;
constraints.gridx=2;
constraints.gridwidth=1;
gridbag.setConstraints(canButton, constraints);
canButton.addActionListener(this);
add(canButton);
pack();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill=GridBagConstraints.HORIZONTAL;
constraints.insets=new Insets(3,1,3,1);
okButton = new Button(" Ok ");
canButton = new Button("Cancle");
Label messageLabel = new Label(message);
setLayout(gridbag);
constraints.gridwidth=2;
constraints.gridheight=1;
constraints.gridx=0;
constraints.gridy=0;
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(messageLabel, constraints);
add(messageLabel);
constraints.fill=GridBagConstraints.NONE;
constraints.gridwidth=1;
constraints.gridy++;
constraints.gridx=0;
gridbag.setConstraints(okButton, constraints);
okButton.addActionListener(this);
add(okButton);
constraints.anchor=GridBagConstraints.WEST;
constraints.gridx=2;
constraints.gridwidth=1;
gridbag.setConstraints(canButton, constraints);
canButton.addActionListener(this);
add(canButton);
pack();
|
很简单,你可以先new 一个Jpanel,然后,你将你的button 放在Jpanel里,就好像这样:
JPanel p = new JPanel();
JButton ok = new JButton("ok");
JButton cancel = new JButton("cancel");
p.add(ok);
p.add(cancel);
getContentPane().add("Center",p);
祝你好运!!
JPanel p = new JPanel();
JButton ok = new JButton("ok");
JButton cancel = new JButton("cancel");
p.add(ok);
p.add(cancel);
getContentPane().add("Center",p);
祝你好运!!
|
你把那两个button先放到一个Jpanel里,然后把添加那个Jpanel窗体里
不过这样的话,你得用swing组件了,不可以用Button,必须用JButton
不过这样的话,你得用swing组件了,不可以用Button,必须用JButton
|
楼上说得对,只有这样,你的布局才能最灵活和漂亮.
方法是将两个button放入一个panel里去,panel的布局可以是flowlayout,也可以是GridBagLayout都可以很好得达到你的居中得效果.
然后,你的label也可以放入一个panel里去.
最后,你的Dialog得布局可以设为Borderlayout,你再将上述两个panel放入你的Dialog中,放button得panel位置为sourth,放label得panel得位置为center即可.
你试试,着一切都可以在可视化下进行,不需自己写代码得.
good luck!
方法是将两个button放入一个panel里去,panel的布局可以是flowlayout,也可以是GridBagLayout都可以很好得达到你的居中得效果.
然后,你的label也可以放入一个panel里去.
最后,你的Dialog得布局可以设为Borderlayout,你再将上述两个panel放入你的Dialog中,放button得panel位置为sourth,放label得panel得位置为center即可.
你试试,着一切都可以在可视化下进行,不需自己写代码得.
good luck!