📄 memosaver.java
字号:
package gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MemoSaver extends JFrame implements ActionListener {
public static final int WIDTH = 600;
public static final int HEIGHT = 300;
public static final int LINES = 8;
public static final int CHAR_PER_LINE = 30;
private JTextArea theText;
private String memo1 = "No memo 1.";
private String memo2 = "No memo 2.";
public MemoSaver(String str) {
super(str);
setSize(WIDTH, HEIGHT);
setLocation(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.WHITE);
buttonPanel.setLayout(new FlowLayout());
JButton memo1Button = new JButton("Save memo 1");
JButton memo2Button = new JButton("Save memo 2");
JButton clear = new JButton("Clear");
JButton get1Button = new JButton("Get memo 1");
JButton get2Button = new JButton("Get memo 2");
memo1Button.addActionListener(this);
memo2Button.addActionListener(this);
clear.addActionListener(this);
get1Button.addActionListener(this);
get2Button.addActionListener(this);
buttonPanel.add(memo1Button);
buttonPanel.add(memo2Button);
buttonPanel.add(clear);
buttonPanel.add(get1Button);
buttonPanel.add(get2Button);
JPanel textPanel = new JPanel();
textPanel.setBackground(Color.LIGHT_GRAY);
theText = new JTextArea(LINES, CHAR_PER_LINE);
JScrollPane scrolledText = new JScrollPane(theText);
theText.setFont(new Font("Arial", Font.BOLD, 20));
theText.setBackground(Color.WHITE);
scrolledText.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrolledText.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
textPanel.add(scrolledText);
contentPane.add(textPanel, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
buttonPanel.add(memo1Button);
buttonPanel.add(memo2Button);
}
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Save memo 1")) {
memo1 = theText.getText();
} else if (actionCommand.equals("Save memo 2")) {
memo2 = theText.getText();
} else if (actionCommand.equals("Clear")) {
theText.setText("");
} else if (actionCommand.equals("Get memo 1")) {
theText.append("\n" + memo1);
} else if (actionCommand.equals("Get memo 2")) {
theText.append("\n" + memo2);
}
}
public static void main(String[] args) {
MemoSaver guiMemo = new MemoSaver("memo saver");
guiMemo.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -