【发布时间】:2016-04-28 09:26:10
【问题描述】:
我无法理解为什么我的代码无法运行。
就目前而言,Eclipse 没有向我显示任何错误,但是当我尝试运行它时代码不会启动。
我的程序的想法是使用 JFrame 的受密码保护的记事本。
这是代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;
public class notepad extends JFrame implements ActionListener {
public static void main(String args[]){}
private TextArea textArea = new TextArea("", 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY);
private MenuBar menuBar = new MenuBar();
private Menu file = new Menu();
private MenuItem openFile = new MenuItem();
private MenuItem saveFile = new MenuItem();
private MenuItem close = new MenuItem();
public notepad() {
this.setSize(700, 500);
this.setTitle("Projet Java");
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.textArea.setFont(new Font("Helvetica", Font.ROMAN_BASELINE, 12));
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(textArea);
this.setMenuBar(this.menuBar);
this.menuBar.add(this.file);
this.file.setLabel("File");
this.openFile.setLabel("Open");
this.openFile.addActionListener(this);
this.file.add(this.openFile);
this.saveFile.setLabel("Save");
this.saveFile.addActionListener(this);
this.file.add(this.saveFile);
}
public void actionPerformed (ActionEvent e) {
if (e.getSource() == this.close)
this.dispose();
else if (e.getSource() == this.openFile) {
JFileChooser open = new JFileChooser();
int option = open.showOpenDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
this.textArea.setText("");
try {
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
while (scan.hasNext())
this.textArea.append(scan.nextLine() + "\n");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
else if (e.getSource() == this.saveFile) {
JFileChooser save = new JFileChooser();
int option = save.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
out.write(this.textArea.getText());
out.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
}
static class password{
public static String password = "password";
public static void main(String args[]) {
JFrame box = new JFrame("Password");
box.setVisible(true);
box.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
box.setSize(400,100);
JPasswordField pass = new JPasswordField(10);
pass.setEchoChar('*');
}
static class AL implements ActionListener{
public void actionPerformed(ActionEvent e){
JPasswordField input = (JPasswordField) e.getSource();
char[] pass = input.getPassword();
String yes = new String(pass);
if (yes.equals(password)){
notepad app = new notepad();
app.setVisible(true);
}else{
System.exit(0);
}
}
}
}
}
【问题讨论】:
-
pass何时添加到任何内容中? -
您还有两个
main方法,所以我不知道您要调用哪个方法来开始工作。您还将重量级 (AWT) 组件与轻量级组件 (Swing) 混合在一起,这不会结束 -
@MadProgrammer 很好发现,我什至没有看到第一个
main方法,它肯定没有做太多:public static void main(String args[]){} -
我不确定您需要什么,因为您只提到此代码不会运行。它会运行,它只是不做你期望它做的事情。您没有指定任何问题,所以我假设您希望它显示一个可以写入密码的字段。为此,您需要在声明
pass后添加行box.add(pass); -
我的问题是,如果没有第一个 main 方法,eclipse 找不到记事本类的 main。之前,我没有尝试为密码输入创建 GUI,当我使用扫描仪时它工作得很好。 @WrongRhyme 是的,很抱歉,我希望它首先运行密码字段,如果密码正确,则继续使用记事本。否则,我希望它关闭
标签: java swing jframe passwords notepad