gpt4 book ai didi

java - Java 中受密码保护的记事本问题

转载 作者:行者123 更新时间:2023-11-30 03:05:22 25 4
gpt4 key购买 nike

我无法理解为什么我的代码无法运行。

就目前情况而言,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);
}


}
}
}
}

最佳答案

  • 您的第一个 main 方法是公共(public)的,但为空,因此不会执行任何操作。
  • 您的第二个 main 方法位于非公共(public)类中,因此 JVM 永远不会找到它来启动您的程序。
  • 将 AWT 图形组件与 Swing 图形组件混合使用,规则是选择其中一种,切勿混合使用两者。
  • 所有 Swing 应用程序都应在 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);
}
}

关于java - Java 中受密码保护的记事本问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34935522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com