我在理解为什么我的代码无法运行时遇到问题。
就目前而言,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);
}
}
}
}
}
SwingUtilities.invokeLater
以确保所有修改GUI的线程均由事件分派器线程(EDT)执行。JTextArea
始终将其放在里面JScrollPane
,以便能够显示超出组件大小的文本。这是经过修改的测试代码:
import java.awt.Font;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
@SuppressWarnings("serial")
public class Notepad extends JFrame
{
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(
"javax.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (ClassNotFoundException | InstantiationException
| IllegalAccessException
| UnsupportedLookAndFeelException e)
{
e.printStackTrace();
}
new Notepad();
}
});
}
private JTextArea textArea = new JTextArea("", 0, 0);
private JMenuBar menuBar = new JMenuBar();
private JMenu file = new JMenu();
private JMenuItem openFile = new JMenuItem();
private JMenuItem saveFile = new JMenuItem();
private JMenuItem close = new JMenuItem();
private JFileChooser open, save;
public Notepad()
{
setBounds(100, 100, 700, 500);
setTitle("Projet Java");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea.setFont(new Font("Helvetica", Font.ROMAN_BASELINE, 12));
add(new JScrollPane(textArea));
open = new JFileChooser();
save = new JFileChooser();
setJMenuBar(menuBar);
menuBar.add(file);
file.setText("File");
openFile.setText("Open");
openFile.addActionListener(e -> {
if (open.showOpenDialog(
Notepad.this) == JFileChooser.APPROVE_OPTION)
{
textArea.setText("");
try (BufferedReader br = new BufferedReader(
new FileReader(open.getSelectedFile())))
{
String s = null;
while ((s = br.readLine()) != null)
{
textArea.append(s + "\n");
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
});
file.add(openFile);
saveFile.setText("Save");
saveFile.addActionListener(e -> {
if (save.showSaveDialog(
Notepad.this) == JFileChooser.APPROVE_OPTION)
{
try (BufferedWriter out = new BufferedWriter(
new FileWriter(save.getSelectedFile())))
{
out.write(textArea.getText());
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
});
file.add(saveFile);
close.setText("Close");
close.addActionListener(e -> {
System.exit(0);
});
file.add(close);
setVisible(true);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句