专注写记事本三十年

  惯例发个JAR

  这么个玩意也加了rr3,我太看得起自己了。

  

  1. 元件JTextField只支持单行内容,要换行得用JTextArea,JTextPane干啥用的我也不知道。

  2. 其实要知道元件都有什么功能,打上setget试试就行了。不过还是记两个吧。

  3. 文本类元件有个方法,setEditable(boolean flag)。如字面意思,决定能不能编辑。在这个软件里用在文件路径显示上,这次就不这么蛋疼让用户自己输入路径了。

  4. 文本区有个方法文本域没有,就是换行(setLineWrap(boolean flag)),因为文本域不能换行。参数用false的话文本区就也不能换行了。

  5. 接上条,还有个补充方法,setWrapStyleWord(boolean flag),决定要不要以空格为分界,也就是以单词为单位换行,true了比较好看。

 

  6. 布局不是问题,跟Python的基本一致,从小面板开始拼,然后把小面板add进大面板,不过把最后的大面板嵌进框架里的时候默认是没有边距的,也就是说是整个顶在窗口四边上,所以得记得给大面板setBorder(Border border)。新建Border有点麻烦,要用BorderFactory类里的createEmptyBorder(1234)方法,这个方法接受四个参数,就是四个边距,顺序不知道,不过多数时候四个数都一样,所以用得着的时候再研究吧。

  7. setBackground(Color c)方法就不用说了吧,不重用的话直接在参数里边new一个颜色就行了,可惜Color类不能直接用十六进制数定义颜色,只能接受三个0-255的RGB值。

  8. 布局管理器有三种,FlowLayout有点蛋疼,说下GridLayout和BorderLayout。

  9. GridLayout如字面意思,构造的时候用两个参数定好行列数,再加两个参数的话就是横竖方向上的元件间隔,默认是0。

  10. BorderLayout的排列方式是分成东西南北中五块,南北块能左右扩大,东西块能上下扩大,中间的横竖都可以扩大,所以把文本框放在中间。五块的定义是BorderLayout里边的final static变量,中间是BorderLayout.CENTER,东西南北类推。在add的时候多加一个参数指定元件放的位置。

 

  11. 补一个JFrame实例的方法,setMinimumSize(Dimension d),用来限制窗口的最小大小,Dimension可以直接用横竖像素数来构造。不过setMaximumSize()限制不了最大窗口大小,不知道为啥……

 

  12. 还有个元件,JFileChooser,用来新建一个文件选择窗口。

  13. setFileSelectionMode(arg)决定是能选文件还是目录还是文件目录都可以。参数是常量,FILES_ONLY,DIRECTORIES_ONLY,FILES_AND_DIRECTORIES,三选一。

  14. setMultiSelectionEnabled(boolean flag),决定能不能多选。

  15. 最后用showXXXXDialog(Component parent),XXXX处用Save或者Open,懂。直接省略的话还能接受第二个参数,字符串表示的按钮文本。这个方法返回一个int,不过其实也就是一个常量。确定选项的常量叫JFileChooser.APPROVE_OPTION,取消的话反正窗口就关了,直接用else就是了。大概就叫CANCEL_OPTION啦……

  16. getSelectedFile()方法返回双击或确定选择的文件,File类型,很方便。

 

  17. 至于import……直接用星号导入啦……

  18. 对了忘说了,框架类直接就继承JFrame就行了,要用的时候new一下。

  19. new完了还要设置一些基本属性。setTitle(String s)设置标题栏的内容。setSize(int x, int y)设置窗口大小。setLocationRelativeTo(Component c)设置……设置什么我也不知道,反正参数用null就是居中框架。setDefaultCloseOperation(int blah)设置标准关闭时的动作,别想太多,用常量JFrame.EXIT_ON_CLOSE。

  20. 最后用setVisible(boolean flag)使框架可见,就成啦。

 

  1 package jLowNote;

  2 

  3 import java.awt.*;

  4 import java.awt.event.*;

  5 

  6 import javax.swing.*;

  7 

  8 import java.io.*;

  9 

 10 public class MainFrame extends JFrame {

 11     private static final long serialVersionUID = -5276129912800884818L;

 12     private File fileOpened;

 13     private JTextField filePath;

 14     private JTextArea content;

 15     private JButton newButton;

 16     private JButton openButton;

 17     private JButton saveButton;

 18     private JButton exitButton;

 19     

 20     public MainFrame() {

 21         fileOpened = new File(".");

 22         

 23         //按钮部分面板。

 24         JPanel buttonPanel = new JPanel(new GridLayout(0, 4, 5, 5));

 25         

 26         //布局按钮。

 27         newButton = new JButton("新建");

 28         openButton = new JButton("打开");

 29         saveButton = new JButton("保存");

 30         exitButton = new JButton("退出");

 31         newButton.addActionListener(new NewListener(this));

 32         openButton.addActionListener(new OpenListener(this));

 33         saveButton.addActionListener(new SaveListener(this));

 34         exitButton.addActionListener(new ExitListener());

 35         //等会补上监听器。

 36         

 37         buttonPanel.add(newButton);

 38         buttonPanel.add(openButton);

 39         buttonPanel.add(saveButton);

 40         buttonPanel.add(exitButton);

 41         buttonPanel.setBackground(new Color(66, 204, 255));

 42         

 43         //=================================================

 44         

 45         //文件操作面板。

 46         JPanel filePanel = new JPanel(new BorderLayout(5, 5));

 47         

 48         //布局。

 49         filePath = new JTextField("文件路径");

 50         filePath.setEditable(false);

 51         

 52         filePanel.add(filePath, BorderLayout.CENTER);

 53         filePanel.add(buttonPanel, BorderLayout.EAST);

 54         filePanel.setBackground(new Color(66, 204, 255));

 55         

 56         //=================================================

 57         

 58         //总体面板。

 59         JPanel overallPanel = new JPanel(new BorderLayout(5, 5));

 60         

 61         //布局。

 62         content = new JTextArea();

 63         content.setLineWrap(true);

 64         content.setWrapStyleWord(true);

 65         

 66         overallPanel.add(filePanel, BorderLayout.NORTH);

 67         overallPanel.add(content, BorderLayout.CENTER);

 68         overallPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

 69         overallPanel.setBackground(new Color(66, 204, 255));

 70         

 71         //=================================================

 72         

 73         this.add(overallPanel);

 74         this.setMinimumSize(new Dimension(400, 300));

 75     }

 76     

 77     public JTextField getPathField() {return this.filePath;}

 78     public JTextArea getContentArea() {return this.content;}

 79     public File getFileOpened() {return this.fileOpened;}

 80     public void setFileOpened(File file) {this.fileOpened = file;}

 81 }

 82 

 83 /**新建按钮的事件监听器。*/

 84 class NewListener implements ActionListener {

 85     MainFrame parent;

 86     

 87     NewListener(MainFrame parent) {this.parent = parent;}

 88     

 89     public void actionPerformed(ActionEvent e) {

 90         parent.getPathField().setText("一个新文件(记得保存哦)");

 91         parent.getContentArea().setText("");

 92         parent.setFileOpened(new File("."));

 93     }

 94 }

 95 

 96 /**打开按钮的事件监听器。*/

 97 class OpenListener implements ActionListener {

 98     MainFrame parent;

 99     

100     OpenListener(MainFrame parent) {this.parent = parent;}

101     

102     @Override

103     public void actionPerformed(ActionEvent e) {

104         //选择文件。

105         JFileChooser fileChooser = new JFileChooser();

106         fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

107         fileChooser.setMultiSelectionEnabled(false);

108         fileChooser.setCurrentDirectory(parent.getFileOpened());

109         if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {

110             parent.setFileOpened(fileChooser.getSelectedFile());

111         } else {

112             return;

113         }

114         

115         //读取文件内容后向文本框一次性写入。

116         parent.getPathField().setText(parent.getFileOpened().getAbsolutePath());

117         

118         try {

119             InputStreamReader in = new InputStreamReader(new FileInputStream(parent.getFileOpened()), "GBK");

120             BufferedReader reader = new BufferedReader(in);

121             String s;

122             StringBuffer sb = new StringBuffer("");

123             while ((s = reader.readLine()) != null) {

124                 sb.append(s + "\n");

125             }

126             parent.getContentArea().setText(sb.toString());

127             reader.close();

128         } catch (FileNotFoundException ex) {

129             ex.printStackTrace();

130         } catch (IOException ioe) {

131             ioe.printStackTrace();

132         }

133     }

134 }

135 

136 /**保存按钮的事件监听器。*/

137 class SaveListener implements ActionListener {

138     MainFrame parent;

139     

140     SaveListener(MainFrame parent) {this.parent = parent;}

141     

142     @Override

143     public void actionPerformed(ActionEvent e) {

144         File fileToWrite = null;

145         

146         //如果已经打开了文件,要编辑的就是已打开的文件,否则就弹窗让用户选择或新建。

147         if (parent.getFileOpened().isFile()) {

148             fileToWrite = parent.getFileOpened();

149         } else {

150             JFileChooser fileChooser = new JFileChooser();

151             fileChooser.setCurrentDirectory(parent.getFileOpened());

152             fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

153             fileChooser.setMultiSelectionEnabled(false);

154             if (fileChooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) {

155                 fileToWrite = fileChooser.getSelectedFile();

156             } else {

157                 return;

158             }

159             

160             //可以自行输入文件名来创建新文件。

161             try {

162                 if (!fileToWrite.exists())

163                     fileToWrite.createNewFile();

164             } catch (IOException ex) {

165                 ex.printStackTrace();

166             }

167         }

168         

169         //读取文本框内容并向文件中写入。

170         try {

171             OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileToWrite));

172             BufferedWriter writer = new BufferedWriter(out);

173             writer.write(parent.getContentArea().getText());

174             writer.close();

175         } catch (IOException ex) {

176             ex.printStackTrace();

177         }

178         

179         JOptionPane.showMessageDialog(parent, "文件已保存。", "保存文件", JOptionPane.PLAIN_MESSAGE);

180     }

181 }

182 

183 /**退出按钮的事件监听器。*/

184 class ExitListener implements ActionListener {

185     @Override

186     public void actionPerformed(ActionEvent e) {

187         System.exit(0);

188     }

189 }
MainFrame及事件监听器
 1 package jLowNote;

 2 

 3 import javax.swing.JFrame;

 4 

 5 public class Run {

 6     public static void main(String[] args) {

 7         MainFrame frame = new MainFrame();

 8         frame.setTitle("低端笔记本Java版");

 9         frame.setSize(640, 480);

10         frame.setLocationRelativeTo(null);

11         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

12         frame.setVisible(true);

13     }

14 }
Run

 

你可能感兴趣的:(记事本)