java面板设计

写面板真的是写到吐血,深深感受到什么叫又臭又长,可能是自己用的太烂吧。

关于布局管理器不写一下还是不懂,另外加面板的思想跟html中div的感觉差不多。

发现的一个小彩蛋:用JScrollPane的时候想在其中加入JTextArea必须在newJScrollPane的时候加,用jsp.add(t)是怎么也不会出现的。

还有一个小问题,每次测试打开图形化界面的时候一定要改变一下窗口大小才会刷新界面是为什么?

事件处理还是百思不得其解。

12-20

写了很久其实对事件处理还是一知半解,稍微懂了点匿名监听,上次的上机作业算是完成了,贴上代码。

 1 import java.awt.*;
 2 import java.awt.event.*;
 3 import java.io.FileWriter;
 4 import java.io.IOException;
 5 
 6 import javax.swing.*;
 7 
 8 
 9 public class outlook extends JFrame{
10 
11     JFrame frm=new JFrame("文本编辑器");
12     JButton save=new JButton("Save");
13     JButton can=new JButton("Cancel");
14     JButton exit=new JButton("Exit");
15     JPanel areabottom=new JPanel();
16     JTextArea t=new JTextArea();
17     JScrollPane mid=new JScrollPane(t,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
18             JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
19     public outlook()
20     {
21         
22         frm.setVisible(true);
23         
24         frm.setBounds(50, 50, 500, 400);
25         frm.setLayout(new BorderLayout(5,5));
26 
27         areabottom.setLayout(new FlowLayout(FlowLayout.CENTER,40,10));
28 
29         t.setLineWrap(true);
30         t.setWrapStyleWord(true);
31         
32         save.setPreferredSize(new Dimension(80,30));
33         can.setPreferredSize(new Dimension(80,30));
34         exit.setPreferredSize(new Dimension(80,30));
35         
36         areabottom.add(save);
37         areabottom.add(can);
38         areabottom.add(exit);
39 
40         frm.add("South",areabottom);
41         frm.add("Center",mid);
42         
43         areabottom.setSize(140, 0);
44         
45         can.addActionListener(new ActionListener(){
46             public void actionPerformed(ActionEvent e) {
47                 t.setText("");
48             }
49         });
50         
51         save.addActionListener(new ActionListener(){
52             public void actionPerformed(ActionEvent e){
53                 String s=t.getText();
54                 try {
55                     FileWriter wf=new FileWriter("2.txt");
56                     wf.write(s);
57                     wf.flush();
58                     wf.close();
59                 } catch (IOException e1) {
60                     e1.printStackTrace();
61                 }
62             }
63         });
64         exit.addActionListener(new ActionListener(){
65             public void actionPerformed(ActionEvent e){
66                 frm.dispose();
67             }
68         });
69     }
70         
71         
72     
73     public static void main(String[] args){
74         new outlook();
75     }
76 
77     
78 }
原文地址:https://www.cnblogs.com/verlen11/p/4170715.html