基本上我一直在努力实现" NotePad"按钮打开我称为记事本的另一个gui类。我曾试图使用actionlistener功能,但我没有运气,知道我怎么能做那样的事情?
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Label;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLayeredPane;
public class Maingui extends JFrame{
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Maingui frame = new Maingui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Maingui() {
setForeground(Color.RED);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setForeground(Color.RED);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNotepad = new JButton("Notepad");
btnNotepad.setBounds(10, 62, 414, 23);
contentPane.add(btnNotepad);
JButton btnWeather = new JButton("Weather");
btnWeather.setBounds(10, 99, 414, 23);
contentPane.add(btnWeather);
JButton btnAddressBook = new JButton("Address Book");
btnAddressBook.setBounds(10, 133, 414, 23);
contentPane.add(btnAddressBook);
JButton btnAgenda = new JButton("Agenda");
btnAgenda.setBounds(10, 167, 414, 23);
contentPane.add(btnAgenda);
Label label = new Label("Montclair Panel");
label.setFont(new Font("Aparajita", Font.PLAIN, 16));
label.setBounds(160, 10, 121, 22);
contentPane.add(label);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setBounds(98, 62, 1, 1);
contentPane.add(layeredPane);
}
}
This is my Notepad class
import javax.swing.*;
import org.jasypt.util.text.BasicTextEncryptor;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;
public class Notepad extends JFrame implements ActionListener {
private static JTextArea textArea = new JTextArea();
private static String myEncryptionPassword;
private static String myText;
private MenuBar menuBar = new MenuBar();
private Menu file = new Menu();
private Menu edit = new Menu();
private Menu shortcut = new Menu();
//UNDO/REDO//
private MenuItem redoFile = new MenuItem();
private MenuItem undoFile = new MenuItem();
private MenuItem openFile = new MenuItem();
private MenuItem saveFile = new MenuItem();
private MenuItem close = new MenuItem();
private MenuItem cut = new MenuItem();
private MenuItem copy = new MenuItem();
private MenuItem paste = new MenuItem();
private MenuItem setBackground = new MenuItem();
public static final int WIDTH = 700;
public static final int HEIGHT = 500;
public static final String TITLE = "Montclair Notepad";
public Notepad() {
this.setSize(WIDTH, HEIGHT);
this.setTitle(TITLE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(textArea);
this.setMenuBar(this.menuBar);
this.menuBar.add(this.file);
this.setMenuBar(this.menuBar);
this.menuBar.add(this.shortcut);
//EDIT//
this.setMenuBar(this.menuBar);
this.menuBar.add(this.edit);
this.edit.setLabel("Edit");
this.shortcut.setLabel("Shortcuts");
this.file.setLabel("File");
this.openFile.setLabel("Open");
this.openFile.addActionListener(this);
this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false));
this.file.add(this.openFile);
this.saveFile.setLabel("Save");
this.saveFile.addActionListener(this);
this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
this.file.add(this.saveFile);
this.close.setLabel("Close");
this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.close.addActionListener(this);
this.file.add(this.close);
//REDO//
this.redoFile.setLabel("Redo");
this.redoFile.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.redoFile.addActionListener(this);
this.edit.add(this.redoFile);
//UNDO//
this.undoFile.setLabel("Undo");
this.undoFile.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.undoFile.addActionListener(this);
this.edit.add(this.undoFile);
this.cut.setLabel("Cut");
this.cut.setShortcut(new MenuShortcut(KeyEvent.VK_T, false));
this.cut.addActionListener(this);
this.shortcut.add(this.cut);
this.copy.setLabel("Copy");
this.copy.setShortcut(new MenuShortcut(KeyEvent.VK_C, false));
this.copy.addActionListener(this);
this.shortcut.add(this.copy);
this.paste.setLabel("Paste");
this.paste.setShortcut(new MenuShortcut(KeyEvent.VK_V, false));
this.paste.addActionListener(this);
this.shortcut.add(this.paste);
this.setBackground.setLabel("Set Background");
this.setBackground.setShortcut(new MenuShortcut(KeyEvent.VK_B, false));
this.setBackground.addActionListener(this);
this.edit.add(this.setBackground);
}
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());
}
}
}
}
public static void main(String args[]) {
Notepad app = new Notepad();
app.setVisible(true);
;
//JFrame f = new JFrame ("Display Color");
app.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JPanel cp = new JPanel();
cp.setBackground(Color.white);
cp.setPreferredSize(new Dimension (350, 150));
app.getContentPane().add(cp);
app.pack();
Color s = Color.white;
s = JColorChooser.showDialog(app, "Choose Your Color", s);
textArea.setBackground(s);
}
}
答案 0 :(得分:0)
我没试过,但它应该有效:
btnNotepad.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setContentPane(newPanel); //Your new JPanel
invalidate();
validate();
}
});