帮忙看一道程序题
怎样让结果显示在屏幕的中间啊
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBag29 extends JFrame
{
private JTextField tf;
private JCheckBox cb1,cb2;
private JRadioButton rb1,rb2,rb3;
private JLabel lb1,lb2,lb3;
private JPanel p1,p2,p3;
public void init()
{
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
lb1=new JLabel("送货地址:");
tf=new JTextField(15);
lb2=new JLabel("货品项目:");
cb1=new JCheckBox("PIZZA");
cb2=new JCheckBox("可乐");
lb3=new JLabel("送货时段:");
rb1=new JRadioButton("上午",false);
rb2=new JRadioButton("下午",false);
rb3=new JRadioButton("晚上",false);
Container c=getContentPane();
c.setLayout(new FlowLayout());
p1.add(lb1);
p1.add(tf);
p2.add(lb2);
p2.add(cb1);
p2.add(cb2);
p3.add(lb3);
p3.add(rb1);
p3.add(rb2);
p3.add(rb3);
c.add(p1);
c.add(p2);
c.add(p3);
setSize(400,200);
setVisible(true);
}
}
----------------解决方案--------------------------------------------------------
用setBounds()的方法
先得到屏幕的大小用Toolkit.getDefaultToolkit().getScreenSize()返回一个Dimension对象表示屏幕的大小
----------------解决方案--------------------------------------------------------
setLocation(int,int)
----------------解决方案--------------------------------------------------------
this.setLocationRealiveto(null);
----------------解决方案--------------------------------------------------------
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBag extends JFrame
{
private JTextField tf;
private JCheckBox cb1,cb2;
private JRadioButton rb1,rb2,rb3;
private JLabel lb1,lb2,lb3;
private JPanel p1,p2,p3;
private ButtonGroup bg;
public GridBag()
{
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
lb1=new JLabel("送货地址:");
tf=new JTextField(15);
lb2=new JLabel("货品项目:");
cb1=new JCheckBox("PIZZA");
cb2=new JCheckBox("可乐");
lb3=new JLabel("送货时段:");
bg=new ButtonGroup();
rb1=new JRadioButton("上午",false);
rb2=new JRadioButton("下午",false);
rb3=new JRadioButton("晚上",false);
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
Container c=getContentPane();
c.setLayout(new FlowLayout());
p1.add(lb1);
p1.add(tf);
p2.add(lb2);
p2.add(cb1);
p2.add(cb2);
p3.add(lb3);
p3.add(rb1);
p3.add(rb2);
p3.add(rb3);
c.add(p1);
c.add(p2);
c.add(p3);
setSize(400,200);
setVisible(true);
}
public static void main(String args[])
{
GridBag gb = new GridBag();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize =gb.getSize();
gb.setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
}
}
在你的代码上改了一下`
----------------解决方案--------------------------------------------------------