import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;java
public class dd {
public static void main(String[] args) {
//建立界面
final JFrame frm = new JFrame("用戶登陸系統");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setLayout(null);ide
//副窗口
final JDialog jd = new JDialog(frm, "登陸成功");
jd.setBounds(500,250,300,150);
//建立用戶名和密碼
JLabel label1 = new JLabel("用戶名:");
label1.setBounds(50, 40, 120, 25);
JLabel label2 = new JLabel("密 碼:");
label2.setBounds(50, 80, 120, 25);
orm
frm.add(label1);
frm.add(label2);
//組合框
JComboBox text = new JComboBox();
text.setBounds(120, 40, 150, 25);
text.addItem("LDD");
text.addItem("ldd");
text.setEditable(true);//組合框可編輯
JPasswordField password = new JPasswordField("1");
password.setBounds(120, 80, 150, 25);
frm.add(text);
frm.add(password);get
//建立按鈕
JButton yes = new JButton("肯定");
yes.setBounds(90, 140, 90,30);
yes.setBackground(Color.LIGHT_GRAY);
JButton out = new JButton("退出");
out.setBounds(190, 140, 90, 30);
out.setBackground(Color.orange);
frm.add(yes);
frm.add(out);
String account = text.getSelectedItem().toString();
String pwd = new String(password.getPassword());it
//建立監聽效果
yes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(account.equals("LDD")&&pwd.equals(password.getText()))
{
JOptionPane.showMessageDialog(null," "+"您的用戶名:"+" "+account+"\n "+"您的密 碼:"+" "+pwd,"登錄成功", JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null, "錯誤", "提示", JOptionPane.ERROR_MESSAGE);
}
}
});
out.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
});
JLabel label = new JLabel();
label.setBounds(1,1,400,200);
Icon z1 = new ImageIcon("image/tt.jpg");
label.setIcon(z1);
frm.add(label);
frm.setBounds(450,230,400,240);
frm.setVisible(true);io
}
}event
原本想添加單選框 記住密碼和自動保存,可是沒有弄明白哪裏錯了。 再去問問同窗,繼續改進。 table
週五由於回家沒有交做業,如今交上,謝謝老師了。form