JAVA教学用代码
源代码在线查看: frame2.java~24~
package joptionpaneex;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.io.File;
import java.io.FileInputStream;
import java.io.*;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import javax.swing.JPopupMenu;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
/**
* Title:
*
* Description:
*
* Copyright: Copyright (c) 2007
*
* Company:
*
* @author not attributable
* @version 1.0
*/
public class Frame2 extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem();
JMenuItem jMenuItem4 = new JMenuItem();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
JFileChooser jFileChooser1 = new JFileChooser("e:\\wkx");
JMenu jMenu2 = new JMenu();
JMenuItem jMenuItem5 = new JMenuItem();
JMenuItem jMenuItem6 = new JMenuItem();
JMenuItem jMenuItem7 = new JMenuItem();
JPopupMenu jPopupMenu1 = new JPopupMenu();
JMenuItem jMenuItem8 = new JMenuItem();
JMenuItem jMenuItem9 = new JMenuItem();
JMenuItem jMenuItem10 = new JMenuItem();
public Frame2() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public static void main(String args[])
{
Frame2 frm=new Frame2();
frm.setLocation(100,100);
frm.setSize(600,450);
frm.setVisible(true);
}
private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
jMenu1.setMnemonic('F');
jMenu1.setText("文件(F)");
jMenuItem1.setText("新建");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.
event.KeyEvent.VK_N, java.awt.event.KeyEvent.CTRL_MASK, false));
jMenuItem1.addActionListener(new Frame2_jMenuItem1_actionAdapter(this));
jMenuItem2.setText("打开");
jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.
event.KeyEvent.VK_O, java.awt.event.KeyEvent.CTRL_MASK, false));
jMenuItem2.addActionListener(new Frame2_jMenuItem2_actionAdapter(this));
jMenuItem3.setText("保存");
jMenuItem3.addInputMethodListener(new
Frame2_jMenuItem3_inputMethodAdapter(this));
jMenuItem4.setText("推出");
jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.
VERTICAL_SCROLLBAR_ALWAYS);
this.setJMenuBar(jMenuBar1);
jMenu2.setMnemonic('E');
jMenu2.setText("编辑(E)");
jMenuItem5.setText("粘贴");
jMenuItem6.setText("复制");
jMenuItem7.setText("剪切");
jMenuItem8.setText("粘贴");
jMenuItem9.setText("复制");
jMenuItem10.setText("剪切");
jTextArea1.addMouseListener(new Frame2_jTextArea1_mouseAdapter(this));
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
jMenu1.add(jMenuItem3);
jMenu1.add(jMenuItem4);
this.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextArea1);
jMenu2.add(jMenuItem5);
jMenu2.add(jMenuItem6);
jMenu2.add(jMenuItem7);
jPopupMenu1.add(jMenuItem8);
jPopupMenu1.add(jMenuItem9);
jPopupMenu1.add(jMenuItem10);
}
public void jMenuItem1_actionPerformed(ActionEvent e) {
jTextArea1.setText("");
}
public void jMenuItem2_actionPerformed(ActionEvent e) {
int t=jFileChooser1.showOpenDialog(this);
//int t=jFileChooser1.showSaveDialog(this);
File file=null;
String strline=null;
StringBuffer strbuf=null;
String str=null;
if(t==JFileChooser.APPROVE_OPTION)
{
JOptionPane.showMessageDialog(this,"打开");
file=jFileChooser1.getSelectedFile();
try {
// FileInputStream fin = new FileInputStream(file);
FileReader fin=new FileReader(file);
BufferedReader bu=new BufferedReader(fin);
strline=bu.readLine();
while(strline!=null)
{
str=str+strline+"\n";
strline=bu.readLine();
}
bu.close();
fin.close();
jTextArea1.setText(str);
} catch (FileNotFoundException ex) {
}catch(IOException ie)
{
}
}
if(t==JFileChooser.CANCEL_OPTION)
{
JOptionPane.showMessageDialog(this,"取消");
}
}
public void jMenuItem3_caretPositionChanged(InputMethodEvent event) {
}
public void jTextArea1_mouseClicked(MouseEvent e) {
// jPopupMenu1.setVisible(true);
jPopupMenu1.show(this,e.getX(),e.getY());
}
}
class Frame2_jTextArea1_mouseAdapter extends MouseAdapter {
private Frame2 adaptee;
Frame2_jTextArea1_mouseAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jTextArea1_mouseClicked(e);
}
}
class Frame2_jMenuItem3_inputMethodAdapter implements InputMethodListener {
private Frame2 adaptee;
Frame2_jMenuItem3_inputMethodAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void inputMethodTextChanged(InputMethodEvent event) {
}
public void caretPositionChanged(InputMethodEvent event) {
adaptee.jMenuItem3_caretPositionChanged(event);
}
}
class Frame2_jMenuItem2_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jMenuItem2_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem2_actionPerformed(e);
}
}
class Frame2_jMenuItem1_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jMenuItem1_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem1_actionPerformed(e);
}
}