Swing习题

1.获取两个文本域的输入并求和,然后显示在第三个文本域中。

程序如下:

  1 package WindowBuilder;
  2 import java.awt.*;
  3 import java.awt.event.*;
  4 import javax.swing.*;
  5 import javax.swing.border.EmptyBorder;
  6 
  7 public class MyJFrame extends JFrame 
  8 {
  9     private JPanel contentPane;
 10     private final JTextPane textPane_1 = new JTextPane();
 11     public static void main(String[] args) 
 12     {
 13         try
 14         {
 15             MyJFrame frame = new MyJFrame();
 16             frame.setVisible(true);
 17         } 
 18         catch (Exception e) 
 19         {
 20             e.printStackTrace();
 21         }
 22     
 23     }
 24     public MyJFrame() {
 25         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 26         setBounds(100, 100, 450, 300);
 27         contentPane = new JPanel();
 28         contentPane.setBackground(Color.LIGHT_GRAY);
 29         contentPane.setBorder(BorderFactory.createEmptyBorder());//窗口设置为无边框
 30         setContentPane(contentPane); //把contentPane对象设置成为MyFrame的内容面板
 31         contentPane.setLayout(null);
 32         
 33         JButton btnNewButton = new JButton("\u786E\u5B9A");
 34         btnNewButton.setFont(new Font("楷体", Font.PLAIN, 12));
 35         btnNewButton.setBackground(Color.RED);
 36         btnNewButton.addActionListener(new ActionListener() 
 37         {
 38             public void actionPerformed(ActionEvent e) 
 39             {
 40                 System.exit(EXIT_ON_CLOSE);
 41             }
 42         });
 43         btnNewButton.setBounds(343, 234, 89, 24);
 44         contentPane.add(btnNewButton);
 45         btnNewButton.setVerticalAlignment(SwingConstants.BOTTOM);//垂直窗口底端
 46         
 47         JButton btnNewButton_1 = new JButton("\u53D6\u6D88");
 48         btnNewButton_1.setFont(new Font("楷体", Font.PLAIN, 12));
 49         btnNewButton_1.setBackground(Color.CYAN);
 50         btnNewButton_1.setBounds(8, 233, 89, 24);
 51         contentPane.add(btnNewButton_1);
 52         
 53         JLabel label = new JLabel("\u6587\u672C\u57DF1\uFF1A");
 54         label.setFont(new Font("楷体", Font.PLAIN, 12));
 55         label.setBounds(14, 51, 58, 15);
 56         contentPane.add(label);
 57         
 58         JTextArea textArea = new JTextArea();
 59         textArea.setLineWrap(true);
 60         textArea.setWrapStyleWord(true);
 61         textArea.setBounds(72, 47, 110, 80);
 62         contentPane.add(textArea);
 63         
 64         JLabel label_1 = new JLabel("\u6587\u672C\u57DF2\uFF1A");
 65         label_1.setFont(new Font("楷体", Font.PLAIN, 12));
 66         label_1.setBounds(207, 51, 58, 15);
 67         contentPane.add(label_1);
 68         
 69         JLabel label_2 = new JLabel("\u6587\u672C\u57DF3\uFF1A");
 70         label_2.setFont(new Font("楷体", Font.PLAIN, 12));
 71         label_2.setBounds(109, 170, 58, 15);
 72         contentPane.add(label_2);
 73         
 74         JTextArea textArea_2 = new JTextArea();
 75         textArea_2.setLineWrap(true);//设置自动换行
 76         textArea_2.setBounds(265, 49, 110, 80);
 77         contentPane.add(textArea_2);
 78         
 79         JPanel panel = new JPanel();
 80         panel.setBounds(162, 156, 158, 80);
 81         contentPane.add(panel);
 82         panel.setLayout(null);
 83         
 84         JScrollPane scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);//滑动条设置为需要时出现
 85         scrollPane.setBounds(0, 0, 158, 80);
 86         panel.add(scrollPane);
 87         
 88         JTextArea textArea_1 = new JTextArea(0,0);
 89         textArea_1.setEditable(false);
 90         textArea_1.setWrapStyleWord(true);
 91         scrollPane.setViewportView(textArea_1);    //scrollPane显示可滚动的子元素
 92         
 93         JButton btnSum = new JButton("Sum");
 94         btnSum.addActionListener(new ActionListener() {
 95             public void actionPerformed(ActionEvent e) 
 96             {
 97                 textArea_1.setEditable(true);
 98                 String s=textArea.getText()+textArea_2.getText();
 99                 textArea_1.setText(s);
100                 textArea.setText("");
101                 textArea_2.setText("");
102                 textArea_1.setEditable(false);
103                 btnSum.setEnabled(false);
104             }
105         });
106         btnSum.setBounds(335, 156, 97, 23);
107         contentPane.add(btnSum);
108     }
109 }

运行结果如下:

 

 注意:将组件加入到JscrollPane面板中,不是使用add方法,而是使用setViewportView方法使子元素可见。setWrapStyleword方法如果为true,则可以使文本域自动换行时避免出现断词。

2.编写一个用户登录程序

程序如下:

  1 package WindowBuilder;
  2 import java.awt.*;
  3 import java.awt.event.*;
  4 import javax.swing.*;
  5 import javax.swing.text.JTextComponent;
  6 class MyDialog extends JDialog 
  7 {
  8     public JLabel label = new JLabel("");
  9     public MyDialog(UserLoginApp frame)
 10     {
 11         super(frame,"结果");
 12         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 13         setBounds(400, 400, 450, 300);
 14         getContentPane().setLayout(null);
 15         {
 16             JPanel panel = new JPanel();
 17             panel.setBounds(0, 0, 436, 209);
 18             getContentPane().add(panel);
 19             panel.setLayout(null);
 20             {
 21                 
 22                 label.setHorizontalAlignment(SwingConstants.CENTER);
 23                 label.setBounds(0, 0, 436, 209);
 24                 panel.add(label);
 25             }
 26         }
 27         {
 28             JButton button = new JButton("\u786E\u5B9A");
 29             button.setForeground(Color.RED);
 30             button.setFont(new Font("楷体", Font.PLAIN, 12));
 31             button.setBounds(163, 219, 110, 34);
 32             button.setVisible(true);
 33             button.addActionListener
 34                 (
 35                     new ActionListener()
 36                     {
 37                         
 38                         public void actionPerformed(ActionEvent e) 
 39                         {
 40                             if(label.getText().equals("用户名错误或密码错误!"))
 41                             {
 42                                 frame.getTextField().setText("");
 43                                 frame.getPasswordField().setText("");
 44                             }
 45                             MyDialog.this.dispose();
 46                             
 47                             
 48                         }
 49                     }
 50                 );
 51             getContentPane().add(button);
 52         }
 53         
 54     }
 55 }
 56 public class UserLoginApp extends JFrame 
 57 {
 58 
 59     private JPanel contentPane;
 60     private JTextField textField;
 61     private JPasswordField passwordField;
 62     
 63     public JTextField getTextField() {
 64         return textField;
 65     }
 66     public void setTextField(JTextField textField) 
 67     {
 68         this.textField = textField;
 69     }
 70     public JPasswordField getPasswordField() 
 71     {
 72         return passwordField;
 73     }
 74     public void setPasswordField(JPasswordField passwordField)
 75     {
 76         this.passwordField = passwordField;
 77     }
 78     public static void main(String[] args) 
 79     {
 80         
 81         try
 82         {
 83             UserLoginApp frame = new UserLoginApp();
 84             frame.setVisible(true);
 85         } 
 86         catch (Exception e)
 87         {
 88             e.printStackTrace();
 89         }
 90         
 91     }
 92     public UserLoginApp()
 93     {
 94         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出应用程序默认窗口关闭
 95         setBounds(100, 100, 450, 300);
 96         contentPane = new JPanel();
 97         setContentPane(contentPane);
 98         contentPane.setLayout(null);
 99         
100         
101         JPanel panel_1 = new JPanel();
102         panel_1.setBounds(0, 0, 436, 200);
103         contentPane.add(panel_1);
104         panel_1.setLayout(new GridLayout(3, 2, 0, 0));
105         
106         JLabel label = new JLabel("\u7528\u6237\u7C7B\u578B");
107         label.setHorizontalAlignment(SwingConstants.CENTER);//标签文字水平居中
108         panel_1.add(label);
109         
110         JComboBox<String> comboBox = new JComboBox<String>();
111         comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u5B66\u751F\u7528\u6237", "\u6559\u5E08\u7528\u6237"}));
112         panel_1.add(comboBox);
113         
114         JLabel label_1 = new JLabel("\u7528\u6237\u540D:");
115         label_1.setHorizontalAlignment(SwingConstants.CENTER);
116         panel_1.add(label_1);
117         
118         textField = new JTextField();
119         panel_1.add(textField);
120         textField.setColumns(10);
121         
122         JLabel label_2 = new JLabel("\u5BC6\u7801\uFF1A");
123         label_2.setHorizontalAlignment(SwingConstants.CENTER);
124         panel_1.add(label_2);
125         
126         passwordField = new JPasswordField();
127         passwordField.setEchoChar('*');
128         panel_1.add(passwordField);
129         
130         JPanel panel = new JPanel();
131         panel.setBounds(0, 199, 436, 64);
132         contentPane.add(panel);
133         panel.setLayout(new GridLayout(1, 3, 5, 5));
134         
135         JButton button = new JButton("\u786E\u5B9A");
136         button.addActionListener(new ActionListener()
137                 {
138                     
139                     public void actionPerformed(ActionEvent e)
140                     {
141                         String S;
142                         if(textField.getText().equals(""))
143                         {
144                             S="用户名不可为空!";
145                             
146                         }
147                         else if(passwordField.getText().equals(""))
148                         {
149                             S="密码不可为空!";
150                         }
151                         else if(((String) comboBox.getSelectedItem()).equals("学生用户"))
152                         {
153                             if(textField.getText().equals("S")&&passwordField.getText().equals("S"))
154                             {
155                                 S="学生用户登录成功!";
156                             }
157                             else
158                             {
159                                 S="用户名错误或密码错误!";
160                             }
161                         }
162                         else
163                         {
164                             if(textField.getText().equals("T")&&passwordField.getText().equals("T"))
165                             {
166                                 S="教师用户登录成功!";
167                             }
168                             else
169                             {
170                                 S="用户名错误或密码错误!";
171                             }
172                             
173                         }
174                         MyDialog aw=new MyDialog(UserLoginApp.this);
175                         aw.setVisible(true);
176                         aw.label.setText(S);
177                     }
178                     
179                 }
180         );
181         panel.add(button);
182         
183         JButton button_1 = new JButton("\u53D6\u6D88");
184         button_1.addActionListener(new ActionListener()
185                 {
186                     public void actionPerformed(ActionEvent e)
187                     {
188                         textField.setText("");
189                         passwordField.setText("");
190                     }
191                 });
192         panel.add(button_1);
193         
194         JButton button_2 = new JButton("\u9000\u51FA");
195         button_2.addActionListener(new ActionListener()
196                 {
197                     public void actionPerformed(ActionEvent e)
198                     {
199                         System.exit(0);
200                     }
201                 }
202                 );
203         panel.add(button_2);
204     }
205 }

运行结果如下:

注意:在子窗口中的窗口关闭方式好像不能使用EXIT_ON_CLOSE;

3.编写一个将华氏温度转化为摄氏温度的图形界面程序

程序如下:

 1 package WindowBuilder;
 2 import java.awt.*;
 3 import java.awt.event.*;
 4 import javax.swing.*;
 5 import javax.swing.border.EmptyBorder;
 6 import java.text.*;
 7 public class TempTrans extends JFrame {
 8 
 9     private JPanel contentPane;
10     private JTextField textField;
11     private JTextField textField_1;
12     public static void main(String[] args)
13     {
14         
15         try 
16         {
17             TempTrans frame = new TempTrans();
18             frame.setVisible(true);
19         } 
20         catch (Exception e) 
21         {
22         
23             e.printStackTrace();
24         }
25         
26     }
27     public TempTrans() 
28     {
29         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
30         setBounds(100, 100, 500, 100);
31         contentPane = new JPanel();
32         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
33         setContentPane(contentPane);
34         contentPane.setLayout(null);
35         
36         JLabel label = new JLabel("\u534E\u6C0F\u6E29\u5EA6\uFF1A");
37         label.setFont(new Font("楷体", Font.PLAIN, 12));
38         label.setHorizontalAlignment(SwingConstants.RIGHT);
39         label.setBounds(35, 26, 66, 15);
40         contentPane.add(label);
41         
42         textField = new JTextField();
43         textField.setBounds(104, 22, 66, 21);
44         contentPane.add(textField);
45         textField.setColumns(10);
46         
47         JLabel label_1 = new JLabel("\u6444\u6C0F\u6E29\u5EA6\uFF1A");
48         label_1.setFont(new Font("楷体", Font.PLAIN, 12));
49         label_1.setHorizontalAlignment(SwingConstants.RIGHT);
50         label_1.setBounds(198, 26, 68, 15);
51         contentPane.add(label_1);
52         
53         textField_1 = new JTextField();
54         textField_1.setBounds(269, 23, 66, 21);
55         contentPane.add(textField_1);
56         textField_1.setColumns(10);
57         
58         JButton button = new JButton("\u8F6C\u6362");
59         button.addActionListener(new ActionListener() {
60             public void actionPerformed(ActionEvent e) 
61             {
62                 int x=Integer.parseInt(textField.getText());
63                 System.out.println(x);
64                 double y=5.0/9*x-32;
65                 System.out.println(y);
66                 DecimalFormat df=new DecimalFormat("###.##");
67                 String s=df.format(y);
68                 textField_1.setText(s);
69             }
70         });
71         button.setBounds(371, 24, 97, 23);
72         contentPane.add(button);
73     }
74 }

运行结果如下:

总结:

  在完成这些程序的过程中,想实现通过按钮将窗口关闭,查找了窗口事件的有关资料,发现可以通过适配器类不必实现接口中的所有方法。但是后面发现按钮不可以添加窗口事件,通过查找资料,可以通过以下三种方式可以解决: 

    1. System.exit(0);   2. setVisible(false);  3.dispose();  

    但是自己好像并不能完全理解。还有窗口的四种关闭模式到底有什么区别,每次运行结果感觉都一样,书上那解释也没看懂。希望知道的同学能解答一下

    

转载于:https://www.cnblogs.com/duwenze/p/10800612.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值