[原创]学了4、5天,写了一个东西
学了4,5天,,学了一个小东西..(应该说.不是什么东西.)..大家看看..多多指点...
谢谢 千里冰封的帮助....
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class JNotepad{
public static void main(String[] args){
UINotepadFrame mainFrame = new UINotepadFrame("新建文本文件");
ExitWindow exitWindow = new ExitWindow();
mainFrame.addWindowListener(exitWindow); //注册监听器
}
}
class UINotepadFrame extends JFrame{
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenu editMenu;
private JMenuItem menuOpen;
private JMenuItem menuSave;
private JMenuItem menuSaveAs;
private JMenuItem menuExit;
private JMenuItem cutMenu;
private JMenuItem copyMenu;
private JMenuItem pasteMenu;
private JTextArea textArea ;
private JScrollPane scrollPane;
private Container contentPane;
private JLabel stateBar;
public UINotepadFrame(String frmTitle){ //创建构造函数
super(frmTitle); //调用父类的构造函数
setBounds(200,200,640,480);
setUIMenuBar();
SetUITextAreaState();
setVisible(true);
}
private void setUIMenuBar(){
menuBar = new JMenuBar(); //设置MenuBar
fileMenu = new JMenu("文件(&F)");
JMenuItem menuOpen = new JMenuItem("打开");
//设置快捷键
menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
menuOpen.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
openFile();
}
}
);
menuSave = new JMenuItem("保存");
menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
menuSaveAs = new JMenuItem("另存为...");
menuExit = new JMenuItem("退出");
menuExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK));
//将菜单项添加到fileMenu菜单中
fileMenu.add(menuOpen);
fileMenu.addSeparator();
fileMenu.add(menuSave);
fileMenu.add(menuSaveAs);
fileMenu.addSeparator();
fileMenu.add(menuExit);
editMenu = new JMenu("编辑(&E)");
cutMenu = new JMenuItem("剪切");
cutMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
copyMenu = new JMenuItem("复制");
copyMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
pasteMenu = new JMenuItem("粘贴");
pasteMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
menuBar.add(fileMenu);
menuBar.add(editMenu);
setJMenuBar(menuBar);
}
private void SetUITextAreaState(){
textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setFont(new Font("宋体",Font.PLAIN,16));
scrollPane = new JScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
contentPane = getContentPane();
contentPane.add(scrollPane,BorderLayout.CENTER);
stateBar = new JLabel("未修改");
stateBar.setHorizontalAlignment(SwingConstants.LEFT);
stateBar.setBorder(BorderFactory.createEtchedBorder());
contentPane.add(stateBar,BorderLayout.SOUTH);
}
private void openFile(){ //打开文件
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showDialog(null,null);
if(option == JFileChooser.APPROVE_OPTION){
try{
BufferedReader buf = new BufferedReader(new FileReader(fileChooser.getSelectedFile()));
setTitle(fileChooser.getSelectedFile().toString());
textArea.setText("");
String strLine = null;
while( (strLine=buf.readLine()) !=null ){
textArea.append(strLine + "\n");
}
buf.close();
}
catch(IOException e){
JOptionPane.showMessageDialog(null,"读取文件失败!");
}
}
}
}
class ExitWindow extends WindowAdapter{ //实现点击"X"时的事件
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
谢谢 千里冰封的帮助....
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class JNotepad{
public static void main(String[] args){
UINotepadFrame mainFrame = new UINotepadFrame("新建文本文件");
ExitWindow exitWindow = new ExitWindow();
mainFrame.addWindowListener(exitWindow); //注册监听器
}
}
class UINotepadFrame extends JFrame{
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenu editMenu;
private JMenuItem menuOpen;
private JMenuItem menuSave;
private JMenuItem menuSaveAs;
private JMenuItem menuExit;
private JMenuItem cutMenu;
private JMenuItem copyMenu;
private JMenuItem pasteMenu;
private JTextArea textArea ;
private JScrollPane scrollPane;
private Container contentPane;
private JLabel stateBar;
public UINotepadFrame(String frmTitle){ //创建构造函数
super(frmTitle); //调用父类的构造函数
setBounds(200,200,640,480);
setUIMenuBar();
SetUITextAreaState();
setVisible(true);
}
private void setUIMenuBar(){
menuBar = new JMenuBar(); //设置MenuBar
fileMenu = new JMenu("文件(&F)");
JMenuItem menuOpen = new JMenuItem("打开");
//设置快捷键
menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
menuOpen.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
openFile();
}
}
);
menuSave = new JMenuItem("保存");
menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
menuSaveAs = new JMenuItem("另存为...");
menuExit = new JMenuItem("退出");
menuExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK));
//将菜单项添加到fileMenu菜单中
fileMenu.add(menuOpen);
fileMenu.addSeparator();
fileMenu.add(menuSave);
fileMenu.add(menuSaveAs);
fileMenu.addSeparator();
fileMenu.add(menuExit);
editMenu = new JMenu("编辑(&E)");
cutMenu = new JMenuItem("剪切");
cutMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
copyMenu = new JMenuItem("复制");
copyMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
pasteMenu = new JMenuItem("粘贴");
pasteMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
menuBar.add(fileMenu);
menuBar.add(editMenu);
setJMenuBar(menuBar);
}
private void SetUITextAreaState(){
textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setFont(new Font("宋体",Font.PLAIN,16));
scrollPane = new JScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
contentPane = getContentPane();
contentPane.add(scrollPane,BorderLayout.CENTER);
stateBar = new JLabel("未修改");
stateBar.setHorizontalAlignment(SwingConstants.LEFT);
stateBar.setBorder(BorderFactory.createEtchedBorder());
contentPane.add(stateBar,BorderLayout.SOUTH);
}
private void openFile(){ //打开文件
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showDialog(null,null);
if(option == JFileChooser.APPROVE_OPTION){
try{
BufferedReader buf = new BufferedReader(new FileReader(fileChooser.getSelectedFile()));
setTitle(fileChooser.getSelectedFile().toString());
textArea.setText("");
String strLine = null;
while( (strLine=buf.readLine()) !=null ){
textArea.append(strLine + "\n");
}
buf.close();
}
catch(IOException e){
JOptionPane.showMessageDialog(null,"读取文件失败!");
}
}
}
}
class ExitWindow extends WindowAdapter{ //实现点击"X"时的事件
public void windowClosing(WindowEvent e){
System.exit(0);
}
}