标签:
我这周做的是登录界面。主要使用的组件有:
1.组合框(JComboBox):addItem(Object item):向选项列表尾部添加选项。
setMaximumRoxCount(int count):设置选项最大的显示行数,默认为八行。(name.setMaximumRowCount(4);我设置的是四行)
setEditable:设置选择框可否编辑,默认为不可编辑,即false。 (name.setEditable(true);)
2.标签(JLabel):private JLabel nameLbl,styleLbl,sizeLbl;
JLabel user = new JLabel("用户名 :");
3.文本框(JTextField):private JTextField nameText,styleText,sizeText;
JTextField mimaText = new JTextField();
代码如下:
package o;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
public class jiemian {
public static void main(String[] args) {
//创建界面
final JFrame frm = new JFrame("用户登录");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setLayout(null);
JLabel user = new JLabel("用户名 :");
user.setBounds(40, 30, 120, 25);
//创建组合框
JComboBox name = new JComboBox();
name.addItem("刘亚娟");
name.addItem("安于他梦");
name.addItem("伴于他心");
name.addItem("白雪");
name.addItem("啦啦啦");
name.addItem("Shine");
name.setEditable(true);
name.setMaximumRowCount(4);
name.setBounds(110,30,130,25);
JLabel mima = new JLabel("密 码 :");
mima.setBounds(40, 70, 120, 25);
JTextField mimaText = new JTextField();
mimaText.setEditable(true);
mimaText.setHorizontalAlignment(SwingConstants.LEFT);
mimaText.setColumns(20);
mimaText.setBounds(110,70,130,25);
frm.add(user);
frm.add(name);
frm.add(mima);
frm.add(mimaText);
JButton an1 = new JButton("确定");
an1.setBounds(40, 110, 75,30);
final JDialog dlg1 = new JDialog(frm,"确认");
dlg1.setBounds(400, 300, 200, 150);
dlg1.setLayout(null);
dlg1.setModal(true);
an1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dlg1.setVisible(true);
}
});
frm.add(an1);
JButton btn1 = new JButton("登录");
btn1.setBounds(50, 40, 100, 25);
btn1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dlg1.setVisible(false);
}
});
dlg1.add(btn1);
JButton an2 = new JButton("取消");
an2.setBounds(160, 110, 75,30);
frm.add(an1);
frm.add(an2);
frm.setBounds(400,300,300,200);
frm.setVisible(true);
}
}
标签:
原文地址:http://www.cnblogs.com/liuyajuan/p/5302676.html