发表于:2007-12-24 19:48:59 楼主
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Remember
{
public static void main(String[] args)
{
Memary mm=new Memary();
mm.init();
}
}
class Memary extends JFrame implements ActionListener,ItemListener
{
FileDialog filedia1,filedia2;
JMenuBar mymenu;
JMenu my,others1,one1,one2,one3,wordsize;
JMenuItem two,exit1,jm1;
String str,str1;
JPopupMenu jpm;
int i,j;
JCheckBoxMenuItem cbm;
Font fn;
JTextArea ta;
File file2,file;
FileWriter fw;
FileReader fr;
BufferedReader br;
BufferedWriter bw;
public Memary()
{
super("My menu");
Dimension d=new Dimension(400,400);
setSize(d);
}
public void init()
{
jpm=new JPopupMenu();
jpm.add("Copy");
jpm.add("Paste");
try{ta.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
trigger(e);
}
public void mouseReleased(MouseEvent e)
{
trigger(e);
}
public void trigger(MouseEvent e)
{
if(e.isPopupTrigger())
jpm.show(e.getComponent(),e.getX(),e.getY());
}
}
);
}
catch(NullPointerException e){System.out.println(e.getMessage());}
filedia1=new FileDialog(this,"保存对话框",FileDialog.SAVE);
filedia1.setVisible(false);
filedia2=new FileDialog(this,"打开对话框",FileDialog.LOAD);
filedia2.setVisible(true);
filedia1.addWindowListener(new WindowAdapter(){public void WindowClosing(WindowEvent e)
{
filedia1.setVisible(false);
}
});
filedia2.addWindowListener(new WindowAdapter(){public void WindowClosing(WindowEvent e)
{
filedia2.setVisible(false);
}
});
fn=new Font("楷体",Font.BOLD,20);
mymenu=new JMenuBar();
setJMenuBar(mymenu);
cbm=new JCheckBoxMenuItem("ReadOnly");
my=new JMenu("File");
JMenuItem jm1=new JMenuItem("Open");
my.add(jm1);
jm1.addActionListener(this);
my.add(new JMenuItem("Save"));
my.add(others1=new JMenu("Install"));
my.add(two=new JMenuItem("Operate"));
my.add(exit1=new JMenuItem("Exit"));
my.addActionListener(this);
mymenu.add(my);
others1.add(one1=new JMenu("Script"));
others1.add(wordsize=new JMenu("Size"));
others1.add(one3=new JMenu("Color"));
wordsize.add("12");
wordsize.add("24");
wordsize.add("36");
wordsize.addActionListener(this);
one3.add("Red");
one3.add("Blue");
one3.add("Green");
one3.add("Yellow");
one3.addActionListener(this);
others1.add(cbm);
one1.add("Songti");
one1.add("RegularScript");
one1.add("Lishu");
one1.addActionListener(this);
others1.add(one2=new JMenu("Style"));
one2.addActionListener(this);
one2.add("Common");
one2.add("Blackbody");
one2.add("Inclined");
exit1.addActionListener(this);
ta=new JTextArea(5,5);
ta.add(jpm);
System.out.println(fn.getSize());
addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e)
{
setVisible(false);
System.exit(0);
}
});
this.getContentPane().add(jpm);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(BorderLayout.CENTER,ta);
//this.getContentPane().add(BorderLayout.NORTH,mymenu);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==jm1)
{
System.out.println("go on");
filedia1.setVisible(true);
file=new File(filedia1.getDirectory(),filedia1.getFile());
fr=null;
fr=new FileReader(file);
br=null;
br=new BufferedReader(fr);
while(br.readLine()!=null)
{
ta.append(br.readLine()+"\n");
}
br.close();
}
else
if(e.getActionCommand()=="Sava")
{
filedia2.setVisible(true);
file2=new File(filedia2.getDirectory(),filedia2.getFile());
fw=null;
fw=new FileWriter(file2);
bw=null;
bw=new BufferedWriter(fw);
bw.write(ta.getText(),0,ta.getText().length());
bw.flush();
bw.close();
}
}
catch (FileNotFoundException e1){System.out.println(e1.getMessage());}
catch(IOException e1){System.out.println(e1.getMessage());}
if(e.getSource()==exit1)
{
dispose();
//System.out.println("go on");
System.exit(0);
}
if(e.getActionCommand()=="Red")
ta.setForeground(Color.red);
if(e.getActionCommand()=="Blue")
ta.setForeground(Color.blue);
if(e.getActionCommand()=="Green")
ta.setForeground(Color.green);
if(e.getActionCommand()=="Yellow")
ta.setForeground(Color.yellow);
if(e.getActionCommand()=="Songti")
{
i=fn.getStyle();
j=fn.getSize();
fn=new Font("宋体",i,j);
ta.setFont(fn);
}
if(e.getActionCommand()=="RegularScript")
{
i=fn.getStyle();
j=fn.getSize();
fn=new Font("楷体",i,j);
System.out.println(fn.getName());
ta.setFont(fn);
}
if(e.getActionCommand()=="Lishu")
{
i=fn.getStyle();
j=fn.getSize();
fn=new Font("隶书",i,j);
ta.setFont(fn);
} if(e.getActionCommand()=="Common")
{
str=fn.getName();
j=fn.getSize();
fn=new Font(str,Font.PLAIN,j);
ta.setFont(fn);
}
if(e.getActionCommand()=="Blackbody")
{
str=fn.getName();
j=fn.getSize();
fn=new Font(str,Font.BOLD,j);
ta.setFont(fn);
}
if(e.getActionCommand()=="Inclined")
{
str=fn.getName();
j=fn.getSize();
fn=new Font(str,Font.ITALIC,j);
ta.setFont(fn);
}
if(e.getSource()=="12")
{
str=fn.getName();
i=fn.getStyle();
j=12;
fn=new Font(str,i,j);
ta.setFont(fn);
}
if(e.getSource()=="24")
{
str=fn.getName();
i=fn.getStyle();
j=24;
fn=new Font(str,i,j);
ta.setFont(fn);
}
if(e.getSource()=="36")
{
str=fn.getName();
i=fn.getStyle();
j=36;
fn=new Font(str,i,j);
ta.setFont(fn);
} }
public void itemStateChanged(ItemEvent e)
{
if(cbm.getState())
ta.setEditable(false);
else ta.setEditable(true);
}
public void textValueChanged(TextEvent e)
{}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Remember
{
public static void main(String[] args)
{
Memary mm=new Memary();
mm.init();
}
}
class Memary extends JFrame implements ActionListener,ItemListener
{
FileDialog filedia1,filedia2;
JMenuBar mymenu;
JMenu my,others1,one1,one2,one3,wordsize;
JMenuItem two,exit1,jm1;
String str,str1;
JPopupMenu jpm;
int i,j;
JCheckBoxMenuItem cbm;
Font fn;
JTextArea ta;
File file2,file;
FileWriter fw;
FileReader fr;
BufferedReader br;
BufferedWriter bw;
public Memary()
{
super("My menu");
Dimension d=new Dimension(400,400);
setSize(d);
}
public void init()
{
jpm=new JPopupMenu();
jpm.add("Copy");
jpm.add("Paste");
try{ta.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
trigger(e);
}
public void mouseReleased(MouseEvent e)
{
trigger(e);
}
public void trigger(MouseEvent e)
{
if(e.isPopupTrigger())
jpm.show(e.getComponent(),e.getX(),e.getY());
}
}
);
}
catch(NullPointerException e){System.out.println(e.getMessage());}
filedia1=new FileDialog(this,"保存对话框",FileDialog.SAVE);
filedia1.setVisible(false);
filedia2=new FileDialog(this,"打开对话框",FileDialog.LOAD);
filedia2.setVisible(true);
filedia1.addWindowListener(new WindowAdapter(){public void WindowClosing(WindowEvent e)
{
filedia1.setVisible(false);
}
});
filedia2.addWindowListener(new WindowAdapter(){public void WindowClosing(WindowEvent e)
{
filedia2.setVisible(false);
}
});
fn=new Font("楷体",Font.BOLD,20);
mymenu=new JMenuBar();
setJMenuBar(mymenu);
cbm=new JCheckBoxMenuItem("ReadOnly");
my=new JMenu("File");
JMenuItem jm1=new JMenuItem("Open");
my.add(jm1);
jm1.addActionListener(this);
my.add(new JMenuItem("Save"));
my.add(others1=new JMenu("Install"));
my.add(two=new JMenuItem("Operate"));
my.add(exit1=new JMenuItem("Exit"));
my.addActionListener(this);
mymenu.add(my);
others1.add(one1=new JMenu("Script"));
others1.add(wordsize=new JMenu("Size"));
others1.add(one3=new JMenu("Color"));
wordsize.add("12");
wordsize.add("24");
wordsize.add("36");
wordsize.addActionListener(this);
one3.add("Red");
one3.add("Blue");
one3.add("Green");
one3.add("Yellow");
one3.addActionListener(this);
others1.add(cbm);
one1.add("Songti");
one1.add("RegularScript");
one1.add("Lishu");
one1.addActionListener(this);
others1.add(one2=new JMenu("Style"));
one2.addActionListener(this);
one2.add("Common");
one2.add("Blackbody");
one2.add("Inclined");
exit1.addActionListener(this);
ta=new JTextArea(5,5);
ta.add(jpm);
System.out.println(fn.getSize());
addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e)
{
setVisible(false);
System.exit(0);
}
});
this.getContentPane().add(jpm);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(BorderLayout.CENTER,ta);
//this.getContentPane().add(BorderLayout.NORTH,mymenu);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==jm1)
{
System.out.println("go on");
filedia1.setVisible(true);
file=new File(filedia1.getDirectory(),filedia1.getFile());
fr=null;
fr=new FileReader(file);
br=null;
br=new BufferedReader(fr);
while(br.readLine()!=null)
{
ta.append(br.readLine()+"\n");
}
br.close();
}
else
if(e.getActionCommand()=="Sava")
{
filedia2.setVisible(true);
file2=new File(filedia2.getDirectory(),filedia2.getFile());
fw=null;
fw=new FileWriter(file2);
bw=null;
bw=new BufferedWriter(fw);
bw.write(ta.getText(),0,ta.getText().length());
bw.flush();
bw.close();
}
}
catch (FileNotFoundException e1){System.out.println(e1.getMessage());}
catch(IOException e1){System.out.println(e1.getMessage());}
if(e.getSource()==exit1)
{
dispose();
//System.out.println("go on");
System.exit(0);
}
if(e.getActionCommand()=="Red")
ta.setForeground(Color.red);
if(e.getActionCommand()=="Blue")
ta.setForeground(Color.blue);
if(e.getActionCommand()=="Green")
ta.setForeground(Color.green);
if(e.getActionCommand()=="Yellow")
ta.setForeground(Color.yellow);
if(e.getActionCommand()=="Songti")
{
i=fn.getStyle();
j=fn.getSize();
fn=new Font("宋体",i,j);
ta.setFont(fn);
}
if(e.getActionCommand()=="RegularScript")
{
i=fn.getStyle();
j=fn.getSize();
fn=new Font("楷体",i,j);
System.out.println(fn.getName());
ta.setFont(fn);
}
if(e.getActionCommand()=="Lishu")
{
i=fn.getStyle();
j=fn.getSize();
fn=new Font("隶书",i,j);
ta.setFont(fn);
} if(e.getActionCommand()=="Common")
{
str=fn.getName();
j=fn.getSize();
fn=new Font(str,Font.PLAIN,j);
ta.setFont(fn);
}
if(e.getActionCommand()=="Blackbody")
{
str=fn.getName();
j=fn.getSize();
fn=new Font(str,Font.BOLD,j);
ta.setFont(fn);
}
if(e.getActionCommand()=="Inclined")
{
str=fn.getName();
j=fn.getSize();
fn=new Font(str,Font.ITALIC,j);
ta.setFont(fn);
}
if(e.getSource()=="12")
{
str=fn.getName();
i=fn.getStyle();
j=12;
fn=new Font(str,i,j);
ta.setFont(fn);
}
if(e.getSource()=="24")
{
str=fn.getName();
i=fn.getStyle();
j=24;
fn=new Font(str,i,j);
ta.setFont(fn);
}
if(e.getSource()=="36")
{
str=fn.getName();
i=fn.getStyle();
j=36;
fn=new Font(str,i,j);
ta.setFont(fn);
} }
public void itemStateChanged(ItemEvent e)
{
if(cbm.getState())
ta.setEditable(false);
else ta.setEditable(true);
}
public void textValueChanged(TextEvent e)
{}
}
if(){
}
if(){
}
...
改成
if(){
}else if(){
}else if(){
}...
就可以了
但是如果我把其中的用JMenuItem来响应的话又可以...
比如说颜色Red
JMenuItem Red=new JMenuItem("Red");
下面在事件处理又可以总之还是感觉改成if else还是有缺陷的
各位大哥帮帮我!谢谢!