📄 serialsample.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class SerialSample
extends JFrame {
private JPanel contentPane;
private JPanel jPanel1 = new JPanel();
private BorderLayout borderLayout1 = new BorderLayout();
private GridLayout gridLayout1 = new GridLayout(1, 3, 1, 1);
private JButton jButton1 = new JButton();
private JButton jButton2 = new JButton();
private JButton jButton3 = new JButton();
private DrawPicture area = new DrawPicture();
ArrayList picture = new ArrayList();
public SerialSample() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
jButton1.setRolloverEnabled(true);
jButton1.setText("Save");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 200));
this.setTitle("Serial Sample");
jButton2.setText("Clear");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton3.setRolloverEnabled(true);
jButton3.setText("Load");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
area.addMouseListener(draw);
jPanel1.setLayout(gridLayout1);
jPanel1.add("b1", jButton1);
jPanel1.add("b2", jButton2);
jPanel1.add("b3", jButton3);
contentPane.add(jPanel1, BorderLayout.NORTH);
contentPane.add(area, BorderLayout.CENTER);
}
void jButton1_actionPerformed(ActionEvent e) {
FileDialog fd = new FileDialog(this, "另存为", FileDialog.SAVE);
fd.show();
String fileName = fd.getFile();
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
fileName));
out.writeObject(picture);
out.flush();
out.close();
}
catch (Exception ex) {
}
}
void jButton2_actionPerformed(ActionEvent e) {
picture = new ArrayList();
repaint();
}
void jButton3_actionPerformed(ActionEvent e) {
FileDialog fd = new FileDialog(this, "打开文件", FileDialog.LOAD);
fd.setVisible(true);
String fileName = fd.getFile();
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
picture = (ArrayList) (in.readObject());
in.close();
repaint();
}
catch (Exception ex) {
}
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public static void main(String[] args) {
SerialSample frame1 = new SerialSample();
frame1.setSize(400, 400);
frame1.setVisible(true);
}
MouseAdapter draw = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Point p = new Point(e.getX(), e.getY());
picture.add(p);
repaint();
}
};
class DrawPicture
extends JTextArea {
DrawPicture() {
super("", 15, 20);
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
int i = 0;
while (i < picture.size()) {
Point p0 = (Point) (picture.get(i++));
int x = p0.x;
int y = p0.y;
g.drawOval(x, y, 50, 50);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -