javaswing
*Main.class
package Swing;
import javax.swing.JFrame;
public class Main {
public static void main(String[]args){
/* JFrame frame=new JFrame("Dang ki thong tin");
frame.setBounds(100,100,500,500);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
*/
Vidu vd=new Vidu();
}
}
*Vidu.class
package Swing;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Vidu extends JFrame implements ListSelectionListener {
JButton button;
JLabel label;
JCheckBox checkbox;
// JRadioButton radiobutton;
JList list;
DefaultListModel listModel;
// JScrollPane listScrollPane;
JTextField textfield;
public Vidu(){
super("Dang ki thong tin");
setBounds(100,100,400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout layout=new GridBagLayout();
GridBagConstraints constraint=new GridBagConstraints();
getContentPane().setLayout(layout);
//tao nhan hoten, ngaysinh, gioi tinh, lop
label=new JLabel("Ho ten:");
constraint.gridx=0;
constraint.gridy=0;
constraint.gridwidth=0;
constraint.gridheight=0;
constraint.weightx=0;
constraint.weighty=0;
constraint.anchor = GridBagConstraints.NORTHWEST;
constraint.insets = new Insets(10,20,10,10);
getContentPane().add(label,constraint);
//tao textfield
textfield=new JTextField(15);
constraint.gridx=1;
constraint.gridy=0;
constraint.gridwidth=2;
constraint.gridheight=1;
// constraint.weightx=0;
// constraint.weighty=0;
getContentPane().add(textfield,constraint);
label=new JLabel("Ngay sinh:");
constraint.gridx=0;
constraint.gridy=1;
constraint.gridheight=1;
constraint.gridwidth=1;
getContentPane().add(label,constraint);
textfield=new JTextField(15);
constraint.gridx=1;
constraint.gridy=1;
constraint.gridwidth=2;
constraint.gridheight=1;
getContentPane().add(textfield,constraint);
label=new JLabel("Gioi tinh:");
constraint.gridx=0;
constraint.gridy=2;
constraint.gridheight=1;
getContentPane().add(label,constraint);
/* checkbox=new JCheckBox("Nam");
constraint.gridx=1;
constraint.gridy=2;
constraint.gridheight=1;
constraint.gridwidth=1;
// constraint.insets=new Insets(0,0,0,5);
getContentPane().add(checkbox,constraint);
checkbox=new JCheckBox("Nu");
constraint.gridx=2;
constraint.gridy=2;
constraint.gridheight=1;
getContentPane().add(checkbox,constraint);
*/
@SuppressWarnings("unused")
ButtonGroup cb=new ButtonGroup();
JRadioButton radiobutton1=new JRadioButton("Nam",true);
constraint.gridx=1;
constraint.gridy=2;
constraint.gridheight=1;
constraint.gridwidth=1;
getContentPane().add(radiobutton1,constraint);
JRadioButton radiobutton2=new JRadioButton("Nu",false);
constraint.gridx=2;
constraint.gridy=2;
constraint.gridheight=1;
constraint.gridwidth=1;
getContentPane().add(radiobutton2,constraint);
ButtonGroup bg=new ButtonGroup();
bg.add(radiobutton1);
bg.add(radiobutton2);
label=new JLabel("Lop:");
constraint.gridx=0;
constraint.gridy=3;
constraint.gridheight=1;
constraint.gridwidth=1;
getContentPane().add(label,constraint);
listModel=new DefaultListModel();
listModel.addElement("Danh sach");
listModel.addElement("08B1");
listModel.addElement("08B2");
listModel.addElement("08B3");
listModel.addElement("08B4");
listModel.addElement("08B5");
listModel.addElement("08B6");
list=new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
JScrollPane listScrollPane=new JScrollPane(list);
listScrollPane.setSize(10,10);
constraint.gridx=1;
constraint.gridy=3;
constraint.gridheight=2;
constraint.gridwidth=2;
constraint.weighty=1;
// getContentPane().add(list,constraint);
getContentPane().add(listScrollPane,constraint);
//tao button Dangki
button=new JButton("Dang ki");
constraint.gridx=1;
constraint.gridy=10;
constraint.gridwidth=1;
constraint.gridheight=1;
// constraint.fill=GridBagConstraints.NONE;
getContentPane().add(button,constraint);
setVisible(true);
}
}
}
Bạn đang đọc truyện trên: AzTruyen.Top