关于java中if 和showMessageDialog()错误的问题。

这段代码好奇怪。
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);
    }

}

}

}



代码写错了。string psw1=password.getpassword() string psw2=password2.getPassword()
如果按照你的写法,psw1永远等于psw2.至于showMessageDialog为什么没出现 对awt不熟悉所以无法解答

==比较的是地址,用equals来比较字符字符串

非常非常感谢各位的回答。此问题已找到答案。无奈近来项目进展缓慢,一直没时间上csdn,因此未能及时结贴。实在抱歉

if中用equals比较看看,psw可是字符串