首页 > 代码库 > 窗口创建的各种语法

窗口创建的各种语法

1.窗口设置模板:

public class Test extends JFrame {
public Test(){
//设置窗名
this.setTitle(" 静夜思");
//设置布局管理器,null布局表示,可以通过组建位置大小定义组件。
this.setLayout(null);
//创建标签对象
JLabel a=new JLabel("煞笔");
//标签颜色
a.setForeground(Color.RED);
//设置标签字体,(颜色,样式,大小)
a.setFont(new Font("宋体",Font.CENTER_BASELINE,30));
//设置标签位置和大小
a.setBounds(20,20,500,30);
//加入窗口
this.add(a);
//加入文本框
JTextField s=new JTextField();
s.setBounds(60,200,200,60);
this.add(s);
//加入按钮对象
JButton q=new JButton("赞!");
q.setFont(new Font("宋体",Font.CENTER_BASELINE,50));
q.setBounds(30,60,150,60);
this.add(q);
//创建图片标签
java.awt.Image i=new ImageIcon("img/图片1.jpg").getImage();
i=i.getScaledInstance(700, 600, 1);
JLabel r=new JLabel(new ImageIcon(i));
r.setBounds(0,0,700,600);
this.add(r);
//加入下拉框
JComboBox box= new JComboBox();
//添加下拉框选项
box.addItem("一年级");
box.addItem("二年级");
box.addItem("三年级");
box.setBounds(20,400,150,60);
this.add(box);
//单选框
JRadioButton jra1=new JRadioButton("智障",true);
jra1.setBounds(20,260,80,20);
this.add(jra1);
JRadioButton jra=new JRadioButton("逗比");
jra.setBounds(100,260,80,20);
this.add(jra);
//將两个单选放入按钮完成互斥
ButtonGroup bg=new ButtonGroup();
bg.add(jra);
bg.add(jra1);
//复选框
JCheckBox che=new JCheckBox("ADC");
che.setBounds(0,300,80,20);
this.add(che);
JCheckBox che1=new JCheckBox("上单");
che1.setBounds(80,300,80,20);
this.add(che1);
//设置窗体大小
this.setSize(700,600);
//设置窗体可见度
this.setVisible(true);
//关闭窗口
this.setDefaultCloseOperation(3);
//窗口居中
this.setLocationRelativeTo(null);
}

public static void main(String[] args) {
Test s=new Test();

}

}

 

2按钮接收跳转:

按钮名.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
跳转内容

}
});

窗口创建的各种语法