java编程界面_怎么用JAVA编程编写一个界面程序(急求!!!)

package org.leadfar; import java.awt.Color;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.Writer; import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;public class Note extends JFrame implementsActionListener, KeyListener { privateJPanel panel; privateFile file = null; privateJTextArea jt; publicNote() { init(); } publicvoid init() { this.setSize(400,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocation(400,400); panel= new JPanel(); panel.setSize(400,400); panel.setLayout(newGridLayout(1, 1)); panel.setBackground(Color.white); //panel.setLocation(0, 100); jt= new JTextArea(); jt.setLineWrap(true); JScrollPanejsp = new JScrollPane(jt); jt.addKeyListener(this); panel.add(jsp); JMenuBarjmb = new JMenuBar(); jmb.setLayout(newFlowLayout(FlowLayout.LEFT)); JMenujm = new JMenu("菜单"); JMenuItemjmi1 = new JMenuItem("关闭 ctrl+e"); JMenuItemjmi2 = new JMenuItem("保存ctrl+s"); JMenuItemjmi3 = new JMenuItem("另存为"); JMenuItemjmi4 = new JMenuItem("打开ctrl+o"); jmi1.addActionListener(this); jmi2.addActionListener(this); jmi3.addActionListener(this); jmi4.addActionListener(this); jm.add(jmi4); jm.add(jmi2); jm.add(jmi3); jm.add(jmi1); jmb.add(jm); this.setJMenuBar(jmb); this.add(panel); this.setVisible(true); } publicstatic void main(String[] args) { newNote(); } @Override publicvoid actionPerformed(ActionEvent e) { Stringcmd = e.getActionCommand(); if("打开ctrl+o".equals(cmd)){ read(); } if("关闭ctrl+e".equals(cmd)) { System.exit(0); } if("保存ctrl+s".equals(cmd)){ save(); } if("另存为".equals(cmd)){ resave(); } } publicvoid read() { JFileChooserjfc = new JFileChooser(); jfc.showOpenDialog(jfc); file= jfc.getSelectedFile(); try{ if(file != null) { FileInputStreamin = new FileInputStream(file); InputStreamReaderipr = new InputStreamReader(in, "GBK"); BufferedReaderbf = new BufferedReader(ipr); Stringstr = ""; while((str=bf.readLine()) != null) { jt.append(str); } } }catch (FileNotFoundException e) { System.out.println("打开文件失败"); }catch (IOException e) { System.out.println("打开文件失败"); } } publicvoid save() { if(file == null) { JFileChooserjfc = new JFileChooser(); jfc.showSaveDialog(jfc); file= jfc.getSelectedFile(); if(file != null) { try{ file.createNewFile(); }catch (IOException e) { //TODO Auto-generated catch block e.printStackTrace(); } } } OutputStreamWriterout = null; if(file != null) { try{ FileOutputStreamfos= new FileOutputStream(file); out= new OutputStreamWriter(fos, "GBK"); out.write(jt.getText()); out.flush(); out.close(); }catch (IOException e) { //TODO Auto-generated catch block e.printStackTrace(); } } } publicvoid resave() { JFileChooserjfc = new JFileChooser(); jfc.showSaveDialog(jfc); file= jfc.getSelectedFile(); if(file != null) { try{ file.createNewFile(); }catch (IOException e) { //TODO Auto-generated catch block e.printStackTrace(); } } OutputStreamWriterout = null; if(file != null) { try{ FileOutputStreamfos= new FileOutputStream(file); out= new OutputStreamWriter(fos, "GBK"); out.write(jt.getText()); out.flush(); out.close(); }catch (IOException e) { //TODO Auto-generated catch block e.printStackTrace(); } } } @Override publicvoid keyTyped(KeyEvent e) { } @Override publicvoid keyPressed(KeyEvent e) { if(e.isControlDown() && e.getKeyCode() == 83) { save(); } } @Override publicvoid keyReleased(KeyEvent e) { //System.out.println(e.getKeyCode()); if(e.isControlDown() && e.getKeyCode() == 69) { System.exit(0); } if(e.isControlDown() && e.getKeyCode() == 79) { read(); } } }这是我曾经写的一个java记事本的程序,你要的东西里边包含了,你试试看!

取消

评论

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值