Java记事本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
| package cn.edu.seig.home; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class MainUI extends JFrame { private static final long serialVersionUID = -8533346717300013642L; private JTextArea jTextArea1; private JScrollPane jsp; private static String name; private JFileChooser jFC = new JFileChooser(); public static void main(String[] args) { new MainUI().init(); } public void init() { setSize(800, 600); setLocationRelativeTo(null); setDefaultCloseOperation(0); name = "新建文本文档.txt"; ImageIcon ii = new ImageIcon("file\\1.png"); setIconImage(ii.getImage()); setTitle(name + " - 记事本"); addMenu(); jTextArea1 = new JTextArea(); jsp = new JScrollPane(jTextArea1); jsp.setPreferredSize(new Dimension(780, 550)); add(jsp, BorderLayout.CENTER); setVisible(true); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { if (showSaveDialog() != JOptionPane.CANCEL_OPTION) setDefaultCloseOperation(3); } }); } private void addMenu() { JMenuBar jmb = new JMenuBar(); setJMenuBar(jmb); JMenu jm_file = new JMenu("文件"); JMenu jm_edit = new JMenu("编辑"); jmb.add(jm_file); jmb.add(jm_edit); JMenuItem item1 = new JMenuItem("打开"); JMenuItem item2 = new JMenuItem("保存"); jm_file.add(item1); jm_file.add(item2); item1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (showSaveDialog() == JOptionPane.CANCEL_OPTION) { return; } int select = jFC.showOpenDialog(MainUI.this); if (select == JFileChooser.APPROVE_OPTION) { jTextArea1.setText(readFile()); jTextArea1.setCaretPosition(0); } } }); item2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (jFC.getSelectedFile() == null) { createFile(); } else { showSaveDialog(); } } }); } private String readFile() { BufferedReader br = null; StringBuilder sb = null; try { br = new BufferedReader(new FileReader(jFC.getSelectedFile())); sb = new StringBuilder(); String str; int count = 0; while ((str = br.readLine()) != null) { if (count == 0) sb.append(str); if (count++ != 0) sb.append("\n" + str); } } catch (FileNotFoundException e1) { JOptionPane.showMessageDialog(null, "未找到该文件!"); return null; } catch (IOException e1) { JOptionPane.showMessageDialog(null, "文件读取异常"); return null; } finally { if (br != null) try { br.close(); } catch (IOException e1) { e1.printStackTrace(); } } name = jFC.getSelectedFile().getName(); setTitle(name + " - 记事本"); return sb.toString(); } private int showSaveDialog() { int select = 0; if (!jTextArea1.getText().equals("")) { if (jFC.getSelectedFile() == null) { select = JOptionPane.showConfirmDialog(MainUI.this, "是否保存修改?"); if (select == JOptionPane.YES_OPTION) { createFile(); } } else { if (!isSaved()) { select = JOptionPane.showConfirmDialog(MainUI.this, "是否保存修改?"); if (select == JOptionPane.YES_OPTION) { saveFile(jFC.getSelectedFile()); } } } } return select; } private void createFile() { File file = null; if (jFC.showSaveDialog(MainUI.this) == JFileChooser.APPROVE_OPTION) { file = jFC.getSelectedFile(); } else { return; } name = jFC.getName(file); if (file.exists()) { int i = JOptionPane.showConfirmDialog(null, "该文件已存在,是否覆盖原文件", "确认", JOptionPane.YES_NO_OPTION); if (i == JOptionPane.YES_OPTION) { saveFile(file); } else { jFC.showSaveDialog(MainUI.this); } } else { saveFile(file); } } private Boolean isSaved() { if (jTextArea1.getText().equals(readFile())) { return true; } return false; } private void saveFile(File file) { BufferedWriter bw = null; try { bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); bw.write(jTextArea1.getText()); bw.flush(); } catch (FileNotFoundException e1) { JOptionPane.showMessageDialog(MainUI.this, "文件保存出错" + e1.getMessage()); } catch (IOException e1) { e1.printStackTrace(); } finally { try { if (bw != null) bw.close(); } catch (IOException e1) { } } } }
|
ActionListener添加窗口联动
创建界面
不懂怎么创建可以看文章末尾
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import java.awt.EventQueue; import javax.swing.JFrame;
public class Window_text {
private JFrame frame;
public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Window_text window = new Window_text(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }
public Window_text() { initialize(); }
private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
}
|
触发事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JLabel; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class Window_text {
private JFrame frame; public JPanel panel = new JPanel();
public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Window_text window = new Window_text(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }
public Window_text() { initialize(); }
private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); panel.setBounds(0, 0, 103, 263); frame.getContentPane().add(panel); panel.setLayout(null); JButton btnNewButton = new JButton("btn"); btnNewButton.setBounds(0, 95, 97, 23); panel.add(btnNewButton); btnNewButton.addActionListener(new B1()); }
class B1 implements ActionListener{
public void actionPerformed(ActionEvent e) {
JPanel right = new JPanel(); right.setBounds(104, 0, 332, 263); frame.getContentPane().add(right); right.setLayout(null); JLabel right_label = new JLabel("成功啦"); right_label.setFont(new Font("楷体", Font.PLAIN, 28)); right_label.setBounds(91, 88, 193, 61); right.add(right_label); right_label.setLayout(null); } } }
|
如何新建WindowBuilder
如何你还没安装WindowBuilder
的话,先去安装再来打开