[原创]学了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);
}
}
----------------解决方案--------------------------------------------------------
给你顶一下!
----------------解决方案--------------------------------------------------------
学了4、5天就能写这么复杂的东西啦……天才啊……
----------------解决方案--------------------------------------------------------
4。5天就能写东西了
厉害厉害
----------------解决方案--------------------------------------------------------
谢谢 两位了啊...
....多亏千里冰封的帮助才行的...
----------------解决方案--------------------------------------------------------
楼主确实不错,进步相当大
我没见过学JAVA学这么快的,可见,楼主和JAVA是相当有缘啊
应该是相见恨晚啊
----------------解决方案--------------------------------------------------------
千里冰封来了啊....真是太感谢他了..他教了我很多知识....
...真是太谢谢了..
----------------解决方案--------------------------------------------------------
不要夸我,是你自己有实力
继续努力吧
----------------解决方案--------------------------------------------------------
嗯 嗯 ..是的..水平还是很低..我还会继续努力的..
----------------解决方案--------------------------------------------------------
那确实,不错,这历害,,,我学了一个月了,还看不懂呀。。
puraaa能介绍点方法吗?
谢谢了。。
----------------解决方案--------------------------------------------------------