1、java简单计算机与多线程完整源码记事本源码package untitled3;import javax.swing.UIManager;import java.awt.*;/* * Title: * Description: * Copyright: Copyright (c) 2013 * Company: * author not attributable * version 1.0 */public class Application1 boolean packFrame = false; /Construct the application public Application1()
2、 Frame1 frame = new Frame1(); /Validate frames that have preset sizes /Pack frames that have useful preferred size info, e.g. from their layout if (packFrame) frame.pack(); else frame.validate(); /Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSi
3、ze = frame.getSize(); if (frameSize.height screenSize.height) frameSize.height = screenSize.height; if (frameSize.width screenSize.width) frameSize.width = screenSize.width; frame.setLocation(screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); frame.setVisible(true)
4、; /Main method public static void main(String args) try UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName(); catch(Exception e) e.printStackTrace(); new Application1(); package untitled3;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import j
5、ava.io.File;import java.io.*;/* * Title: * Description: * Copyright: Copyright (c) 2013 * Company: * author not attributable * version 1.0 */public class Frame1 extends JFrame JPanel contentPane; JMenuBar jMenuBar1 = new JMenuBar(); JMenu jMenuFile = new JMenu(); JMenuItem jMenuFileExit = new JMenuI
6、tem(); JMenu jMenuHelp = new JMenu(); JMenuItem jMenuHelpAbout = new JMenuItem(); BorderLayout borderLayout1 = new BorderLayout(); JMenuItem jMenuItem1 = new JMenuItem(); JMenuItem jMenuItem2 = new JMenuItem(); JMenuItem jMenuItem3 = new JMenuItem(); JMenuItem jMenuItem4 = new JMenuItem(); /Construc
7、t the frame private int a; JScrollPane jScrollPane1 = new JScrollPane(); JTextArea txt = new JTextArea(); public Frame1() enableEvents(AWTEvent.WINDOW_EVENT_MASK); try jbInit(); catch(Exception e) e.printStackTrace(); /Component initialization private void jbInit() throws Exception contentPane = (JP
8、anel) this.getContentPane(); contentPane.setLayout(borderLayout1); this.setSize(new Dimension(400, 300); this.setTitle(无标题-记事本); jMenuFile.setText(文件); jMenuFileExit.setText(退出); jMenuFileExit.addActionListener(new Frame1_jMenuFileExit_ActionAdapter(this); jMenuHelp.setText(帮助); jMenuHelpAbout.setTe
9、xt(关于); jMenuHelpAbout.addActionListener(new Frame1_jMenuHelpAbout_ActionAdapter(this); jMenuItem1.setText(新建); jMenuItem1.addActionListener(new Frame1_jMenuItem1_actionAdapter(this); jMenuItem2.setText(打开); jMenuItem2.addActionListener(new Frame1_jMenuItem2_actionAdapter(this); jMenuItem3.setText(保
10、存); jMenuItem3.addActionListener(new Frame1_jMenuItem3_actionAdapter(this); jMenuItem4.setText(另存为); jMenuItem4.addActionListener(new Frame1_jMenuItem4_actionAdapter(this); txt.addKeyListener(new Frame1_txt_keyAdapter(this); jMenuFile.add(jMenuItem1); jMenuFile.add(jMenuItem2); jMenuFile.add(jMenuIt
11、em3); jMenuFile.add(jMenuItem4); jMenuFile.add(jMenuFileExit); jMenuHelp.add(jMenuHelpAbout); jMenuBar1.add(jMenuFile); jMenuBar1.add(jMenuHelp); contentPane.add(jScrollPane1, BorderLayout.CENTER); jScrollPane1.getViewport().add(txt, null); this.setJMenuBar(jMenuBar1); /File | Exit action performed
12、public void jMenuFileExit_actionPerformed(ActionEvent e) if(a=0) System.exit(0); if(a=1) int ret=JOptionPane.showOptionDialog(null,你选择退出,但是文件未保存,是否选择保存?,提示,JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE,null,null,null); if(ret=0)JFileChooser jfc = new JFileChooser(); int ret1=jfc.showS
13、aveDialog(this); if(ret1=0)File file = jfc.getSelectedFile(); setTitle(file.toString(); try file.createNewFile(); catch (IOException ex) try BufferedWriter buf = new BufferedWriter(new FileWriter(file);buf.write(txt.getText();buf.close();a=0;System.exit(0); catch (IOException ex1) else ;else if (ret
14、=1)System.exit(0); else; /Help | About action performed public void jMenuHelpAbout_actionPerformed(ActionEvent e) Frame1_AboutBox dlg = new Frame1_AboutBox(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation(frmSize.width - dlg
15、Size.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.pack(); dlg.show(); /Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) super.processWindowEvent(e); if (e.getID() = WindowEvent.WINDOW_CLOSING) jMenuFileExi
16、t_actionPerformed(null); void jMenuItem1_actionPerformed(ActionEvent e) if(a=0)txt.setText();this.setTitle(无标题-记事本); else if(a=1)int ret=JOptionPane.showOptionDialog(null,选择文件新建,但是您的文件尚未保存,是否选择保存,提示,JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE,null,null,null); if (ret = 0) JFileChoos
17、er jfc = new JFileChooser(); int ret1=jfc.showSaveDialog(this); if(ret1=0)File file = jfc.getSelectedFile(); setTitle(file.toString(); try file.createNewFile(); catch (IOException ex) try BufferedWriter buf = new BufferedWriter(new FileWriter(file);buf.write(txt.getText();buf.close();txt.setText();
18、this.setTitle(无标题-记事本);a=0; catch (IOException ex1) else else if(ret = 1) txt.setText();this.setTitle(无标题-记事本);a=0; else void txt_keyPressed(KeyEvent e) a=1; void jMenuItem2_actionPerformed(ActionEvent e) if(a=0) JFileChooser jfc = new JFileChooser(); int ret=jfc.showOpenDialog(this); if (ret = 0) F
19、ile file = jfc.getSelectedFile(); try a=0; BufferedReader buf = new BufferedReader(new FileReader(file); setTitle(file.toString();txt.setText();String lineSeparator = System.getProperty(line.separator); String text; try while ( (text = buf.readLine() != null) txt.append(text); txt.append(lineSeparat
20、or); catch (IOException ex1) catch (FileNotFoundException ex) else if(a=1)int ret=JOptionPane.showOptionDialog(null,已经打开的文件尚未保存,需要保存吗?,提示,JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE,null,null,null); if(ret=0) JFileChooser jfc = new JFileChooser(); int ret1=jfc.showSaveDialog(this);
21、if(ret1=0)File file = jfc.getSelectedFile(); setTitle(file.toString(); try file.createNewFile(); catch (IOException ex) try BufferedWriter buf = new BufferedWriter(new FileWriter(file);buf.write(txt.getText();buf.close();a=0; JFileChooser jfc1 = new JFileChooser(); int ret2=jfc1.showOpenDialog(this)
22、; if (ret2 = 0) File file1 = jfc1.getSelectedFile(); try BufferedReader buf1 = new BufferedReader(new FileReader(file1); setTitle(file1.toString();txt.setText();String lineSeparator = System.getProperty(line.separator); String text; try while ( (text = buf1.readLine() != null) txt.append(text); txt.
23、append(lineSeparator); catch (IOException ex1) catch (FileNotFoundException ex) else catch (IOException ex1) else else if (ret=1)JFileChooser jfc = new JFileChooser(); int ret3=jfc.showOpenDialog(this); if (ret3 = 0) File file = jfc.getSelectedFile(); try a=0; BufferedReader buf = new BufferedReader
24、(new FileReader(file); setTitle(file.toString();txt.setText();String lineSeparator = System.getProperty(line.separator); String text; try while ( (text = buf.readLine() != null) txt.append(text); txt.append(lineSeparator); catch (IOException ex1) catch (FileNotFoundException ex) else void jMenuItem3
25、_actionPerformed(ActionEvent e) File file1 = new File(getTitle(); if(getTitle()=无标题-记事本) JFileChooser jfc = new JFileChooser(); int ret1=jfc.showSaveDialog(this); if(ret1=0)File file = jfc.getSelectedFile(); setTitle(file.toString(); try file.createNewFile(); catch (IOException ex) try BufferedWrite
26、r buf = new BufferedWriter(new FileWriter(file);buf.write(txt.getText();buf.close(); catch (IOException ex1) else elsetry BufferedWriter buf = new BufferedWriter(new FileWriter(file1);buf.write(txt.getText();buf.close();a=0; catch (IOException ex1) void jMenuItem4_actionPerformed(ActionEvent e) JFil
27、eChooser jfc = new JFileChooser(); int ret1=jfc.showSaveDialog(this); if(ret1=0)File file = jfc.getSelectedFile(); setTitle(file.toString(); try file.createNewFile(); catch (IOException ex) try BufferedWriter buf = new BufferedWriter(new FileWriter(file);buf.write(txt.getText();buf.close();a=0; catc
28、h (IOException ex1) else class Frame1_jMenuFileExit_ActionAdapter implements ActionListener Frame1 adaptee; Frame1_jMenuFileExit_ActionAdapter(Frame1 adaptee) this.adaptee = adaptee; public void actionPerformed(ActionEvent e) adaptee.jMenuFileExit_actionPerformed(e); class Frame1_jMenuHelpAbout_Acti
29、onAdapter implements ActionListener Frame1 adaptee; Frame1_jMenuHelpAbout_ActionAdapter(Frame1 adaptee) this.adaptee = adaptee; public void actionPerformed(ActionEvent e) adaptee.jMenuHelpAbout_actionPerformed(e); class Frame1_jMenuItem1_actionAdapter implements java.awt.event.ActionListener Frame1 adaptee; Frame1_jMenuItem1_actionAdapter(Frame1 adaptee) this.adaptee = adaptee;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1