簡單地用swing寫一個登陸頁面
阿新 • • 發佈:2018-12-26
import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.WindowConstants; public class Work3 extends JFrame{ public Work3(){ Container container=getContentPane(); container.setLayout(null); JLabel jl1=new JLabel("用 戶 名:");JLabel jl2=new JLabel("密 碼:"); JTextField jtf=new JTextField();JPasswordField jpf=new JPasswordField(); JButton jb1=new JButton("提交");JButton jb2=new JButton("重置"); container.add(jl1);jl1.setBounds(30,30,100,30); container.add(jl2);jl2.setBounds(30,80,100,30); container.add(jtf);jtf.setBounds(140,30,150,30); container.add(jpf);jpf.setBounds(140,80,150,30); container.add(jb1);jb1.setBounds(60,130,80,30); container.add(jb2);jb2.setBounds(160,130,80,30); jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO 自動生成的方法存根 if (jtf.getText().equals("mr")&&jpf.getText().equals("mrsoft")){ JOptionPane.showMessageDialog(null,"登陸成功!"); }else{ JOptionPane.showMessageDialog(null,"登陸失敗!"); } } }); jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO 自動生成的方法存根 jtf.setText("");jpf.setText(""); } }); setTitle("Work3"); setVisible(true); setBounds(500,500,380,350); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new Work3(); } }