📄 menubar.java
字号:
/*
* menubar.java
*
* Created on 2007年6月24日, 上午9:22
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package huaban;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
/**
*
* @author Administrator
*/
public class menubar extends JMenuBar implements ActionListener{
JMenuItem Item[] = new JMenuItem[5];
JMenu menu[] = new JMenu[3];
JButton[] button = new JButton[3];
JCheckBox box;
Drawpanel pan;
String st = null;
public menubar(Drawpanel pan) {
this.pan = pan;
String st[] = {"文件","编辑","查看"};
String str[] = {"保存","打开","新建","撤销","显示颜色"};
for(int i = 0; i < 5; i++){
Item[i] = new JMenuItem(str[i]);
}
for(int j = 0; j < 3; j++){
menu[j] = new JMenu(st[j]);
}
for(int i = 0; i < 3; i++){
button[i] = new JButton();
button[i].add(menu[i]);
}
box = new JCheckBox(str[4],false);
Item[4].add(box);
menu[0].add(Item[0]);
menu[0].add(Item[1]);
menu[0].add(Item[2]);
menu[1].add(Item[3]);
menu[2].add(Item[4]);
this.add(menu[0]);
this.add(menu[1]);
this.add(menu[2]);
Item[0].addActionListener(this);
Item[1].addActionListener(this);
Item[2].addActionListener(this);
Item[3].addActionListener(this);
Item[4].addActionListener(this);
box.addActionListener(this);
this.setRequestFocusEnabled(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == Item[0]){
try{
if(st == null){
st = JOptionPane.showInputDialog(this,"输入存储文件名");
}
File file = new File(st);
FileOutputStream output = new FileOutputStream(file);
ObjectOutputStream objectout = new ObjectOutputStream(output);
objectout.writeObject(pan.panel1.vector);
}
catch(IOException h){
System.out.println(h);
}
}
if(e.getSource() == Item[1]){
try{
st = JOptionPane.showInputDialog(this,"输文件名");
File file = new File(st);
FileInputStream input = new FileInputStream(file);
ObjectInputStream objectin = new ObjectInputStream(input);
pan.panel1.vector.removeAllElements();
pan.panel1.vector = (Vector)objectin.readObject();
pan.panel1.repaint();
}
catch(Exception h){
System.out.println("Error" + e);
System.exit(1);
}
}
if(e.getSource() == Item[2]){
pan.panel1.pressed = true;
pan.panel1.vector.removeAllElements();
pan.panel1.repaint();
st = null;
}
if(e.getSource() == Item[3]){
pan.panel1.pressed = true;
if(pan.panel1.vector.size() > 0){
pan.panel1.vector.removeElementAt(pan.panel1.vector.size()-1);
pan.panel1.repaint();
}
}
if(e.getSource() == box){
if(box.isSelected()){
pan.panel.setVisible(true);
}
else{
pan.panel.setVisible(false);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -