找错误package text5;//窗口类 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class e9_1 { public static void main(String args[]) { PoliceWindow policeWin=new PoliceWindow(); } } class PoliceWindow extends JFrame { JTextField titleText; JPasswordField passwordText; PoliceWindow() { titleText=new JTextField(10); passwordText=new JPasswordField(10); passwordText.setEchoChar('*'); titleText.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JTextField textSourse=(JTextField)e.getSource(); if(textSourse==titleText) PoliceWindow.setTitle(titleText.getText()); else if(textSourse==passwordText){ char c[]=passwordText.getPassword(); titleText.setText(new String(c)); } }); passwordText.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JTextField textSourse=(JTextField)e.getSource(); if(textSourse==titleText) PoliceWindow.setTitle(titleText.getText()); else if(textSourse==passwordText){ char c[]=passwordText.getPassword(); titleText.setText(new String(c)); } }); setLayout(new FlowLayout()); add(titleText); add(passwordText); setBounds(100,100,150,150); setVisible(true); validate(); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } } }
时间: 2023-06-19 13:09:54 浏览: 93
(java swing毕业设计)图书管管理系统(视频+源码).zip
错误是在 `PoliceWindow` 类的构造函数中,第一个 `addActionListener` 方法和第二个 `addActionListener` 方法的代码完全相同,应该分别定义不同的功能。以下是修改后的代码:
```java
package text5;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class e9_1 {
public static void main(String args[]) {
PoliceWindow policeWin = new PoliceWindow();
}
}
class PoliceWindow extends JFrame {
JTextField titleText;
JPasswordField passwordText;
PoliceWindow() {
titleText = new JTextField(10);
passwordText = new JPasswordField(10);
passwordText.setEchoChar('*');
// 监听标题文本框
titleText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JTextField textSource = (JTextField) e.getSource();
if (textSource == titleText)
PoliceWindow.this.setTitle(titleText.getText());
}
});
// 监听密码文本框
passwordText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JTextField textSource = (JTextField) e.getSource();
if (textSource == passwordText) {
char c[] = passwordText.getPassword();
titleText.setText(new String(c));
}
}
});
setLayout(new FlowLayout());
add(titleText);
add(passwordText);
setBounds(100, 100, 150, 150);
setVisible(true);
validate();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
```
阅读全文