java小问题

[复制链接]
查看11 | 回复0 | 2007-6-27 21:23:31 | 显示全部楼层 |阅读模式
带GUI的小型文本编辑器,是我导师的一个学生的作业。把所需要的图片放到同一目录下即可。import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.net.*;import java.util.*;//主函数 调用下面的类 public class TextApp{public static void main( String [] args){
TextFrame f = new TextFrame();
f.setTitle(" 文本编辑器 ");
f.setSize(800,600);
f.show();}}//-----------------------------------------------------------------------------------------------------class TextFrame extends JFrame{
File file = null;
Color color = Color.yellow;
GraphicsEnvironment getFont = GraphicsEnvironment.getLocalGraphicsEnvironment();//这是一个抽象类
Font []fonts = getFont.getAllFonts();//字体
JTextPane text = new JTextPane(); //创建一个新的文本组件
JColorChooser colorchooser = new JColorChooser();//提供一个用于允许用户操作和选择颜色的控制器窗格
JFileChooser filechooser = new JFileChooser();//构造一个指向用户默认目录的机制
JDialog about = new JDialog(this);//创建一个没有标题但将该类作为其所有者的无模式对话框
JMenuBar menubar = new JMenuBar();//创建新的菜单栏//成员函数
TextFrame()
{
initTextPane();//面板
initMenu();//菜单
initToolBar();//工具栏
}
void initTextPane()
{
setFont(new Font("Times New Roman",Font.PLAIN,12));//根据指定名称为Times New Rom,普通样式和点大小为12,创建一个新 Font。
getContentPane().add( new JScrollPane(text));
}//---------------------------------------------------------------------------//菜单的定义
JMenu [] menus= new JMenu[]//定义菜单
{
new JMenu("文件"),
new JMenu("编辑"), new JMenu("工具"),
};
//sub Menue
JMenuItem menuitems [][] =new JMenuItem[][]
{
{
new JMenuItem("新建"),
new JMenuItem("打开..."),
new JMenuItem("保存..."),
new JMenuItem("Exit")
},
{
new JMenuItem("Copy"),
new JMenuItem("Cut"),
new JMenuItem("Paste"),
new JMenuItem("选择全部"),
new JMenuItem("Color...")
},{
new JMenuItem("图画"),
new JMenuItem("计算器"),
new JMenuItem("记事本")
}}; //----------------------------------------------------------------------------- //菜单的初始化
void initMenu()
{
for(int i=0;i<menus.length;i++)
{
menubar.add(menus);
for(int j=0;j<menuitems.length;j++)
{
menus.add(menuitems[j]);
menuitems[j].addActionListener(action);
}
}
this.setJMenuBar(menubar);
} //-----------------------------------------------------------------------------
ActionListener action = new ActionListener()//接收菜单操作事件的侦听器接口
{
public void actionPerformed(ActionEvent e)
{
JMenuItem mi = (JMenuItem)e.getSource();
String id = mi.getText();
if(id.equals("新建")){
Date date=new Date();
text.setText(date.toString());
file = null;
}else if(id.equals("打开...")){
if(file != null)
filechooser.setSelectedFile(file);
int returnVal = filechooser.showOpenDialog(TextFrame.this);
if(returnVal == JFileChooser.APPROVE_OPTION){
file = filechooser.getSelectedFile();
openFile();
}
}
else if(id.equals("保存...")){
if(file != null)
filechooser.setSelectedFile(file);
int returnVal = filechooser.showSaveDialog(TextFrame.this);
if(returnVal == JFileChooser.APPROVE_OPTION){
file = filechooser.getSelectedFile();
saveFile();
}}else if(id.equals("Exit")){
System.exit(0);
}else if(id.equals("Cut")){
text.cut();
}else if(id.equals("Copy")){
text.copy();
}else if(id.equals("Paste")){
text.paste();
}else if(id.equals("选择全部")){
text.selectAll();
}else if(id.equals("Color...")){
color = JColorChooser.showDialog(TextFrame.this,"",color);
text.setForeground(color);
}else if(id.equals("图画")){
try{
String command = "mspaint.exe";
Process child = Runtime.getRuntime().exec(command);
}
catch (IOException ex)
{
}
}else if(id.equals("计算器")){
try{
String command = "calc.exe";
Process child = Runtime.getRuntime().exec(command);
}
catch (IOException ex)
{
}
}else if(id.equals("记事本")){
try{
String command = "notepad.exe";
Process child = Runtime.getRuntime().exec(command);
}
catch (IOException ex)
{
}
}
}
};//-------------------------------------------------------------------------------
void saveFile()
{
try{
FileWriter fw = new FileWriter(file);
fw.write(text.getText());
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
} //---------------------------------------------------------------
void openFile()
{
try{
FileReader fr = new FileReader(file);
int len = (int) file.length();
char [] buffer = new char[len];
fr.read(buffer,0,len);
fr.close();
text.setText(new String(buffer));
}
catch(Exception e)
{
e.printStackTrace();
}
}
JToolBar toolbar = new JToolBar();
JButton [] buttons = new JButton[]//添加图画
{
new JButton("",new ImageIcon("copy.jpg")),
new JButton("",new ImageIcon("cut.jpg")),
new JButton("",new ImageIcon("paste.jpg")),
new JButton("",new ImageIcon("MS1.jpg")),
new JButton("",new ImageIcon("MS2.jpg")),new JButton("",new ImageIcon("MS3.jpg")),
};//-------------------------------------------------------------------------------------------------------------------
void initToolBar()
{
for(int i=0; i<buttons.length;i++)
toolbar.add(buttons);
buttons[0].setToolTipText("copy");//复制功能
buttons[0].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.copy();
}
});
buttons[1].setToolTipText("cut");//剪切功能
buttons[1].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.cut();
}
});
buttons[2].setToolTipText("paste");//粘贴功能
buttons[2].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.paste();
}
});
buttons[3].setToolTipText("图画");//调用系统图画.exe
buttons[3].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
String command = "mspaint.exe";
Process child = Runtime.getRuntime().exec(command);
}
catch (IOException ex)
{
}
}
});buttons[4].setToolTipText("计算器");//调用系统计算器.exebuttons[4].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
String command = "calc.exe";
Process child = Runtime.getRuntime().exec(command);
}
catch (IOException ex)
{
}
}
});
buttons[5].setToolTipText("记事本");//调用系统记事本.exebuttons[5].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
String command = "notepad.exe";
Process child = Runtime.getRuntime().exec(command);
}
catch (IOException ex)
{
}
}
});this.getContentPane().add(toolbar,BorderLayout.NORTH);
}}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行