import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JFrame; public class KaisaFrame extends JFrame {
JLabel jlDackText = new JLabel();
JButton btnBrightText = new JButton();
JPasswordField jpf = new JPasswordField();
JTextField jtf2 = new JTextField();
char[] buf;
public KaisaFrame() {
this.setSize(600,400); //设置窗体大小
this.setTitle("凯撒加密法");
this.setResizable(false);
jbInit();
}
private void isLetter() {
//判断输入的内容是否为字母
buf = jpf.getPassword();
for(char c:buf) {
if(Character.isLetter(c) == false) {
JOptionPane.showMessageDialog(this, "不能为非字符", "Error", JOptionPane.ERROR_MESSAGE);
jpf.setText("");
jtf2.setText("");
return;
}
else {
makeBrightText();
}
}
}
private void makeBrightText() {
// 产生明文
char[] arr = new char[buf.length];
int index=0,temp;
for(char c:buf) {
temp = c+3; //字母后移三位
if( (temp>90 && temp<97) || temp>122 ) {
//若ASCII码在此区间则减去26(使字符XYZ,xyz循环到XAB,xab)
temp = temp - 26;
arr[index++] = (char)temp;
}
else{
arr[index++] = (char)temp;
}
}
String str = new String(arr);
jtf2.setText(str);
}
private void jbInit() {
// 设置窗体内容
this.setLayout(null);
jlDackText.setText("输入密文:");
jlDackText.setBounds(30, 20, 80, 30);
btnBrightText.setText("生成明文:");
btnBrightText.setBounds(30, 90, 100, 30);
jpf.setBounds(160, 20, 80, 30);
jpf.setEchoChar('*');
jtf2.setBounds(160, 90, 80, 30);
jtf2.setEditable(false);
this.add(jlDackText);
this.add(jpf);
this.add(btnBrightText);
this.add(jtf2);
btnBrightText.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) { isLetter(); //判断输入的内容是否为字母
}
});
}
}
红色那行报错 类型 new ActionListener(){} 的方法 actionPerformed(ActionEvent)必须覆盖超类方法
怎么处理?
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JFrame; public class KaisaFrame extends JFrame {
JLabel jlDackText = new JLabel();
JButton btnBrightText = new JButton();
JPasswordField jpf = new JPasswordField();
JTextField jtf2 = new JTextField();
char[] buf;
public KaisaFrame() {
this.setSize(600,400); //设置窗体大小
this.setTitle("凯撒加密法");
this.setResizable(false);
jbInit();
}
private void isLetter() {
//判断输入的内容是否为字母
buf = jpf.getPassword();
for(char c:buf) {
if(Character.isLetter(c) == false) {
JOptionPane.showMessageDialog(this, "不能为非字符", "Error", JOptionPane.ERROR_MESSAGE);
jpf.setText("");
jtf2.setText("");
return;
}
else {
makeBrightText();
}
}
}
private void makeBrightText() {
// 产生明文
char[] arr = new char[buf.length];
int index=0,temp;
for(char c:buf) {
temp = c+3; //字母后移三位
if( (temp>90 && temp<97) || temp>122 ) {
//若ASCII码在此区间则减去26(使字符XYZ,xyz循环到XAB,xab)
temp = temp - 26;
arr[index++] = (char)temp;
}
else{
arr[index++] = (char)temp;
}
}
String str = new String(arr);
jtf2.setText(str);
}
private void jbInit() {
// 设置窗体内容
this.setLayout(null);
jlDackText.setText("输入密文:");
jlDackText.setBounds(30, 20, 80, 30);
btnBrightText.setText("生成明文:");
btnBrightText.setBounds(30, 90, 100, 30);
jpf.setBounds(160, 20, 80, 30);
jpf.setEchoChar('*');
jtf2.setBounds(160, 90, 80, 30);
jtf2.setEditable(false);
this.add(jlDackText);
this.add(jpf);
this.add(btnBrightText);
this.add(jtf2);
btnBrightText.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) { isLetter(); //判断输入的内容是否为字母
}
});
}
}
红色那行报错 类型 new ActionListener(){} 的方法 actionPerformed(ActionEvent)必须覆盖超类方法
怎么处理?
public class MainKaisa {
public static void main(String[] args) {
//实例化一个窗体
KaisaFrame kaisaFrame = new KaisaFrame();
kaisaFrame.setVisible(true);
kaisaFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
这是主函数!