這段代碼好奇怪。
1.它能編譯通過。
2.但是這一段有點奇怪
if((psw1==psw2))
{
JOptionPane.showMessageDialog(frame, "alert");
System.out.println(psw1);
}
什麼問題呢?
1.不論psw1==psw2是否成立,shouwMessageDialog 永遠沒有執行(或許執行了,但沒彈出對話框).我試過把 if()去了,或把 showMessageDialog 放到 Main()裡,打還是沒反應。
2.不論psw1==psw2是否成立,、System.out.println(psw1);都會被執行。。
這是怎麼回事?是bug嗎?還是我犯了其他 隱秘的錯誤?
欲知真相,
敬請關注以下源碼。
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
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.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class winReg implements ActionListener{
private JFrame frame = new JFrame("register");
private Container c = frame.getContentPane();
private JTextField username = new JTextField();
private JPasswordField password = new JPasswordField();
private JPasswordField password2= new JPasswordField();
private JButton confirm = new JButton("confirm");
private JButton cancel = new JButton("cancel");
public winReg(){
frame.setSize(600,400);
c.setLayout(new BorderLayout());
initFrame();
confirm.addActionListener(this);
cancel.addActionListener(this);
frame.setVisible(true);
}
private void initFrame() {
//頂部
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
titlePanel.add(new JLabel("new register"));
c.add(titlePanel,"North");
//中部表單
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
JLabel l1 = new JLabel("user name:");
l1.setBounds(50, 20, 50, 20);
JLabel l2 = new JLabel("password:");
JLabel l3 = new JLabel("confirm password:");
l2.setBounds(50, 60, 100, 20);
l3.setBounds(50, 90, 150, 20);
fieldPanel.add(l1);
fieldPanel.add(l2);
fieldPanel.add(l3);
username.setBounds(230,20,120,20);
password.setBounds(230,60,120,20);
password2.setBounds(230,90,120,20);
fieldPanel.add(username);
fieldPanel.add(password);
fieldPanel.add(password2);
c.add(fieldPanel,"Center");
//底部按鈕
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(confirm);
buttonPanel.add(cancel);
c.add(buttonPanel,"South");
}
public static void main(String[] args){
JOptionPane.showMessageDialog(frame, "alert", "alert", JOptionPane.ERROR_MESSAGE);
new winReg();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if((e.getSource()==confirm)){
String usn=username.getText();
String psw1=password.getPassword();
String psw2=password.getPassword();
//check wether psw same
if((psw1==psw2))
{
JOptionPane.showMessageDialog(frame, "alert");
System.out.println(psw1);
}
}
}
}
首先,你在main方法中用了frame,main方法是靜態的,你的frame也要聲明成static的
``` private JFrame frame = new JFrame("register");
要改成:
```private static JFrame frame = new JFrame("register");
其次,你的JOptionPane類要導入,加代碼
```import javax.swing.JOptionPane;
最後,password.getPassword();返回值類型是char[]
String psw1=password.getPassword();
String psw2=password.getPassword();
要換成:
String psw1=String.valueOf(password.getPassword());
String psw2=String.valueOf(password.getPassword());
修改後的完整代碼:
(你的類名首字母要大寫,等號左右兩端要空開,花括號要層層嵌入,這些編寫代碼的基本風格要注意,這些都沒幫你改,需要注意)
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
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.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
public class winReg implements ActionListener{
private static JFrame frame = new JFrame("register");
private Container c = frame.getContentPane();
private JTextField username = new JTextField();
private JPasswordField password = new JPasswordField();
private JPasswordField password2= new JPasswordField();
private JButton confirm = new JButton("confirm");
private JButton cancel = new JButton("cancel");
public winReg(){
frame.setSize(600,400);
c.setLayout(new BorderLayout());
initFrame();
confirm.addActionListener(this);
cancel.addActionListener(this);
frame.setVisible(true);
}
private void initFrame() {
//頂部
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
titlePanel.add(new JLabel("new register"));
c.add(titlePanel,"North");
//中部表單
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
JLabel l1 = new JLabel("user name:");
l1.setBounds(50, 20, 50, 20);
JLabel l2 = new JLabel("password:");
JLabel l3 = new JLabel("confirm password:");
l2.setBounds(50, 60, 100, 20);
l3.setBounds(50, 90, 150, 20);
fieldPanel.add(l1);
fieldPanel.add(l2);
fieldPanel.add(l3);
username.setBounds(230,20,120,20);
password.setBounds(230,60,120,20);
password2.setBounds(230,90,120,20);
fieldPanel.add(username);
fieldPanel.add(password);
fieldPanel.add(password2);
c.add(fieldPanel,"Center");
//底部按鈕
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(confirm);
buttonPanel.add(cancel);
c.add(buttonPanel,"South");
}
public static void main(String[] args){
JOptionPane.showMessageDialog(frame, "alert", "alert", JOptionPane.ERROR_MESSAGE);
new winReg();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if((e.getSource()==confirm)){
String usn=username.getText();
String psw1=String.valueOf(password.getPassword());
String psw2=String.valueOf(password.getPassword());
//check wether psw same
if((psw1==psw2))
{
JOptionPane.showMessageDialog(frame, "alert");
System.out.println(psw1);
}
}
}
}