首页 > 代码库 > GUI基础1
GUI基础1
JPanel,JButton,JLabel,JTextField,JPasswordField,JLabel,JCheckBox,JRadioButton
20170220上课课程记录代码1(JPanel,JButton,JLabel,JTextField,JPasswordField):
package test;
import java.awt.*;
import javax.swing.*;
public class test extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel jp1,jp2,jp3;
private JButton jb1,jb2,jb3,jb4,jb5;
private JLabel jlbl1,jbl1,jbl2;//文本控件
private JTextField jtf1,jtf2;
private JPasswordField jpf1;
private JButton jbt1,jbt2;
public test(){
this.setTitle("我的第一个窗体");//设置窗体标题 this 该类当前的对象
this.setLocation(200,200);//设置窗体显示的位置
this.setSize(200,200);//设置大小
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
/*jp1=new JPanel();
jp1.setLocation(0,0);
jp1.setSize(200,200);
jp1.setBackground(Color.red);
this.add(jp1);*/
//流式布局 设置了布局按钮大小事固定的
/* LayoutManager manager = new FlowLayout(FlowLayout.LEFT);
this.setLayout(manager);
jb1=new JButton();
jb1.setText("按钮1");
jb2=new JButton();
jb2.setText("按钮2");
jb3=new JButton();
jb3.setText("按钮3");
jb4=new JButton();
jb4.setText("按钮4");
jb5=new JButton();
jb5.setText("按钮5");
this.add(jb1);
this.add(jb2);
this.add(jb3);
this.add(jb4);
this.add(jb5);*/
//边框布局
/*LayoutManager manager = new BorderLayout();
this.setLayout(manager);
jb1=new JButton();
jb1.setText("按钮1");
jb2=new JButton();
jb2.setText("按钮2");
jb3=new JButton();
jb3.setText("按钮3");
jb4=new JButton();
jb4.setText("按钮4");
jb5=new JButton();
jb5.setText("按钮5");
this.add(jb1,BorderLayout.NORTH);
this.add(jb2,BorderLayout.WEST);
this.add(jb3,BorderLayout.CENTER);
this.add(jb4,BorderLayout.EAST);
this.add(jb5,BorderLayout.SOUTH);*/
//网格布局
/*this.setLayout(new GridLayout(3,2));//三行两列
jb1=new JButton();
jb1.setText("按钮1");
jb2=new JButton();
jb2.setText("按钮2");
jb3=new JButton();
jb3.setText("按钮3");
jb4=new JButton();
jb4.setText("按钮4");
jb5=new JButton();
jb5.setText("按钮5");
this.add(jb1);
this.add(jb2);
this.add(jb3);
this.add(jb4);
this.add(jb5);*/
//文本控件
/*ImageIcon icon=new ImageIcon("D:\1.bmp");
jlbl1=new JLabel("我是文本标签控件",icon,SwingConstants.CENTER);
this.add(JLabel);*/
//登陆界面
this.setLayout(new FlowLayout());
jp1=new JPanel();
//jp1.setLayout(new FlowLayout());默认流式
jbl1=new JLabel("用户名:");
jtf1=new JTextField(10);
jp1.add(jbl1);
jp1.add(jtf1);
this.add(jp1);
jp2=new JPanel();
jbl2=new JLabel("密 码:");
jpf1=new JPasswordField(10);
jp2.add(jbl2);
jp2.add(jpf1);
this.add(jp2);
jp3=new JPanel();
jbt1= new JButton("登陆");
jbt2= new JButton("重置");
jp3.add(jbt1);
jp3.add(jbt2);
this.add(jp3);
this.setVisible(true);
}
public static void main(String []args){
new test();
}
}
20170220上课课程记录代码2(JLabel,JCheckBox,JRadioButton):
package test;
import java.awt.*;
import javax.swing.*;
public class test1 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel jbl1,jbl2;//文本控件
private JCheckBox jcb1,jcb2,jcb3;
private JRadioButton jrb1,jrb2;
public test1(){
this.setTitle("我的第一个窗体");//设置窗体标题 this 该类当前的对象
this.setLocation(200,200);//设置窗体显示的位置
this.setSize(300,300);//设置大小
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
/*ImageIcon icon=new ImageIcon("1.gif");
JLabel jl1=new JLabel("这是haha",icon,SwingConstants.LEFT);
*/
jbl1=new JLabel("请选择你感兴趣的运动:");
jcb1=new JCheckBox("篮球");
jcb2=new JCheckBox("排球");
jcb3=new JCheckBox("花球");
this.add(jbl1);
this.add(jcb1);
this.add(jcb2);
this.add(jcb3);
jbl1=new JLabel("请选择你的性别:");
jrb1=new JRadioButton("男",false);
jrb2=new JRadioButton("女",false);
this.add(jbl1);
this.add(jrb1);
this.add(jrb2);
this.setVisible(true);
}
public static void main(String []args){
new test1();
}
}
GUI基础1