class ChoiceListener implements ItemListener
{ public void itemStateChanged( ItemEvent e)
{
if(aComboBox.getSelectedItem()=="TimesRoman")
{
aTextArea.setFont(new Font("TimesRoman",Font.PLAIN+Font.ITALIC,16));
}
else if(aComboBox.getSelectedItem()=="Courier")
{
aTextArea.setFont(new Font("Courier",Font.ITALIC,5));
}
else if(aComboBox.getSelectedItem()=="楷体")
{
aTextArea.setFont(new Font("楷体",Font.ITALIC,30));
}
else if(aComboBox.getSelectedItem()=="宋体")
{
aTextArea.setFont(new Font("宋体",Font.ITALIC,20));
}
else if(aComboBox.getSelectedItem()=="Helvetica")
{
aTextArea.setFont(new Font("Helvetica",Font.ITALIC,10));
}
aTextArea.requestFocus();
}class Reminder{
Timer timer;
JLabel jl;
public Reminder(open op) { ////////////////////将open对象由此传入
jl=op.aLabel; ////////////////////将aLabel赋值给jl
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), new Date(),1000);
}
class RemindTask extends TimerTask {
public void run() {
jl.setText((new Date()).toLocaleString());
}
}
}}
public void actionPerformed(ActionEvent evt)
{
String cmd=evt.getActionCommand();
if(cmd=="new")
{
aTextArea.setText("");
}
else if(cmd=="open")
{
JFileChooser JFileChooser1 = new JFileChooser("D:\\");//启动一个文件选择器
if(JFileChooser1.APPROVE_OPTION==JFileChooser1.showOpenDialog(this))//如果文件选择完毕
{
openFile(JFileChooser1.getSelectedFile().getPath());//作为将来的接口
String filepath=JFileChooser1.getSelectedFile().getPath();//获取被选择文件的路径
System.out.println(filepath);//输出文件路径
}
}
//加上的保存功能
else if (cmd=="save")
{
JFileChooser JFileChooser2 = new JFileChooser("D:\\");
if(JFileChooser2.APPROVE_OPTION==JFileChooser2.showSaveDialog(this))//如果文件选择完毕
{
saveFile(JFileChooser2.getSelectedFile().getPath());//作为将来的接口
String filepath=JFileChooser2.getSelectedFile().getPath();//获取被选择文件的路径
System.out.println(filepath);//输出文件路径
} }
else if(cmd=="exit")
{
System.out.println("Exit");
System.exit(0);
}
else if(cmd=="help")
{
JOptionPane.showMessageDialog(null,"我的第一个小程序!!!!","帮助",JOptionPane.INFORMATION_MESSAGE);
} else if(cmd=="color")
{
Color color =JColorChooser.showDialog(this,"Foreground Color",aTextArea.getForeground());
if(color!= null){
aTextArea.setForeground(color);
}
}
else if(cmd=="copy")
{
String selection = aTextArea.getSelectedText();
StringSelection clipString = new StringSelection(selection);
aClipboard.setContents(clipString,clipString);}
else if(cmd=="paste")
{
Transferable clipData =
aClipboard.getContents(open.this);
try {
String clipString =
(String)clipData.
getTransferData(
DataFlavor.stringFlavor);
aTextArea.replaceRange(clipString,
aTextArea.getSelectionStart(),
aTextArea.getSelectionEnd());
} catch(Exception ex) {
System.out.println("not String flavor");
}
}
else if(cmd=="cut")
{
String selection = aTextArea.getSelectedText();
StringSelection clipString =
new StringSelection(selection);
aClipboard.setContents(clipString, clipString);
aTextArea.replaceRange("",
aTextArea.getSelectionStart(),
aTextArea.getSelectionEnd());
}aTextArea.requestFocus();
}
//打开方法
void openFile(String fileName)
{
try
{
File file=new File(fileName);
int size =(int)file.length();
int chars_read=0;
FileReader in=new FileReader(file);
char[] data=new char[size];
while(in.ready())
{
chars_read+=in.read(data,chars_read,size-chars_read);
//read(目标数组、文件起始位置、文件结束位置)
//返回读入的数据量
}
in.close();
aTextArea.setText(new String(data,0,chars_read));
}
catch(IOException e)
{
System.out.println(e.toString());
}
}
{ public void itemStateChanged( ItemEvent e)
{
if(aComboBox.getSelectedItem()=="TimesRoman")
{
aTextArea.setFont(new Font("TimesRoman",Font.PLAIN+Font.ITALIC,16));
}
else if(aComboBox.getSelectedItem()=="Courier")
{
aTextArea.setFont(new Font("Courier",Font.ITALIC,5));
}
else if(aComboBox.getSelectedItem()=="楷体")
{
aTextArea.setFont(new Font("楷体",Font.ITALIC,30));
}
else if(aComboBox.getSelectedItem()=="宋体")
{
aTextArea.setFont(new Font("宋体",Font.ITALIC,20));
}
else if(aComboBox.getSelectedItem()=="Helvetica")
{
aTextArea.setFont(new Font("Helvetica",Font.ITALIC,10));
}
aTextArea.requestFocus();
}class Reminder{
Timer timer;
JLabel jl;
public Reminder(open op) { ////////////////////将open对象由此传入
jl=op.aLabel; ////////////////////将aLabel赋值给jl
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), new Date(),1000);
}
class RemindTask extends TimerTask {
public void run() {
jl.setText((new Date()).toLocaleString());
}
}
}}
public void actionPerformed(ActionEvent evt)
{
String cmd=evt.getActionCommand();
if(cmd=="new")
{
aTextArea.setText("");
}
else if(cmd=="open")
{
JFileChooser JFileChooser1 = new JFileChooser("D:\\");//启动一个文件选择器
if(JFileChooser1.APPROVE_OPTION==JFileChooser1.showOpenDialog(this))//如果文件选择完毕
{
openFile(JFileChooser1.getSelectedFile().getPath());//作为将来的接口
String filepath=JFileChooser1.getSelectedFile().getPath();//获取被选择文件的路径
System.out.println(filepath);//输出文件路径
}
}
//加上的保存功能
else if (cmd=="save")
{
JFileChooser JFileChooser2 = new JFileChooser("D:\\");
if(JFileChooser2.APPROVE_OPTION==JFileChooser2.showSaveDialog(this))//如果文件选择完毕
{
saveFile(JFileChooser2.getSelectedFile().getPath());//作为将来的接口
String filepath=JFileChooser2.getSelectedFile().getPath();//获取被选择文件的路径
System.out.println(filepath);//输出文件路径
} }
else if(cmd=="exit")
{
System.out.println("Exit");
System.exit(0);
}
else if(cmd=="help")
{
JOptionPane.showMessageDialog(null,"我的第一个小程序!!!!","帮助",JOptionPane.INFORMATION_MESSAGE);
} else if(cmd=="color")
{
Color color =JColorChooser.showDialog(this,"Foreground Color",aTextArea.getForeground());
if(color!= null){
aTextArea.setForeground(color);
}
}
else if(cmd=="copy")
{
String selection = aTextArea.getSelectedText();
StringSelection clipString = new StringSelection(selection);
aClipboard.setContents(clipString,clipString);}
else if(cmd=="paste")
{
Transferable clipData =
aClipboard.getContents(open.this);
try {
String clipString =
(String)clipData.
getTransferData(
DataFlavor.stringFlavor);
aTextArea.replaceRange(clipString,
aTextArea.getSelectionStart(),
aTextArea.getSelectionEnd());
} catch(Exception ex) {
System.out.println("not String flavor");
}
}
else if(cmd=="cut")
{
String selection = aTextArea.getSelectedText();
StringSelection clipString =
new StringSelection(selection);
aClipboard.setContents(clipString, clipString);
aTextArea.replaceRange("",
aTextArea.getSelectionStart(),
aTextArea.getSelectionEnd());
}aTextArea.requestFocus();
}
//打开方法
void openFile(String fileName)
{
try
{
File file=new File(fileName);
int size =(int)file.length();
int chars_read=0;
FileReader in=new FileReader(file);
char[] data=new char[size];
while(in.ready())
{
chars_read+=in.read(data,chars_read,size-chars_read);
//read(目标数组、文件起始位置、文件结束位置)
//返回读入的数据量
}
in.close();
aTextArea.setText(new String(data,0,chars_read));
}
catch(IOException e)
{
System.out.println(e.toString());
}
}
解决方案 »
- 为什么说【移位运算】在运行时比普通乘除法效率高?如何用移位实现求余数(%)的运算?
- 判断三个变量只有一个不是空
- 某公司限时笔试题 10分钟 我做成了这个样子,请算法高手指点1、2
- eclipse中输入小写类名,弹出的那个错误提示框如何实现,哪位大大知道帮帮忙,在线等
- 大家做信息软件都是在程序代码里拼凑sql语句然后发送到数据库来获得数据吗?
- JFrame问题
- 加入了图标的菜单对齐问题?
- SYBASE11.9通过JDBC来链接据库时,我做的conn不支持ROLLBACK(来者有分)
- 紧急求助:用JAVA编写FTP客户端软件
- [新人问题] Java的队列方法 如何选择
- invoke问题,调用时总说java.lang.IllegalArgumentException: object is not an instance of declaring class
- 程序自升级里碰到的一个怪事,大家来看看是什么原因……
void saveFile(String fileName)
{
try
{
File file = new File(fileName);
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(aTextArea.getText());
out.flush();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
} public static void main(String[] args)
{
new open();
}
}
this 就是代表一个open类对象的引用!明白了吗我也是个初学者,能看懂人家程序,但编不出来啊!
楼主,做个朋友吧,我们的水平差不多,看样有共同语言啊,我的qq:239202525