1、制作一个如图所示的界面(使用FlowLayout布局),不要求实现功能。(图略)
程序如下:
1 package WindowBuilder;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.EmptyBorder;
7 import java.util.Date;
8 import java.awt.Window.Type;
9 import java.awt.Dialog.ModalExclusionType;
10
11 public class MyClock extends JFrame {
12
13 private JPanel contentPane;
14 private JTextField textField;
15 private JTextField textField_1;
16 private JTextField textField_2;
17 private JTextField textField_3;
18 private JTextField textField_4;
19 private JTextField textField_5;
20 Date date=new Date();
21 String s1=String.format("%tH", date);
22 String s2=String.format("%tM", date);
23 String s3=String.format("%tS", date);
24 public static void main(String[] args)
25 {
26 EventQueue.invokeLater(new Runnable()
27 {
28 public void run() {
29 try {
30 MyClock frame = new MyClock();
31 frame.setVisible(true);
32 } catch (Exception e) {
33 e.printStackTrace();
34 }
35 }
36 });
37 }
38
39 /**
40 * Create the frame.
41 */
42 public MyClock() {
43 setBackground(Color.GREEN);
44 setAlwaysOnTop(true);
45 setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
46 setType(Type.UTILITY);
47 setForeground(Color.GREEN);
48 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
49 setBounds(100, 100, 359, 337);
50 contentPane = new JPanel();
51 contentPane.setForeground(Color.GREEN);
52 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
53 setContentPane(contentPane);
54 contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
55
56 JLabel label = new JLabel("\u5F53\u524D\u65F6\u95F4\uFF1A");
57 contentPane.add(label);
58
59 textField = new JTextField();
60 textField.setEditable(false);
61 textField.setHorizontalAlignment(SwingConstants.CENTER);
62 contentPane.add(textField);
63 textField.setColumns(5);
64 textField.setText(s1);
65
66 JLabel label_1 = new JLabel("\u65F6");
67 contentPane.add(label_1);
68
69 textField_1 = new JTextField();
70 textField_1.setEditable(false);
71 textField_1.setHorizontalAlignment(SwingConstants.CENTER);
72 contentPane.add(textField_1);
73 textField_1.setColumns(5);
74 textField_1.setText(s2);
75
76 JLabel label_2 = new JLabel("\u5206");
77 contentPane.add(label_2);
78
79 textField_2 = new JTextField();
80 textField_2.setEditable(false);
81 textField_2.setHorizontalAlignment(SwingConstants.CENTER);
82 contentPane.add(textField_2);
83 textField_2.setColumns(5);
84 textField_2.setText(s3);
85
86 JLabel label_3 = new JLabel("\u79D2");
87 contentPane.add(label_3);
88
89 JLabel label_5 = new JLabel("\u95F9\u949F\u65F6\u95F4\uFF1A");
90 contentPane.add(label_5);
91
92 textField_3 = new JTextField();
93 contentPane.add(textField_3);
94 textField_3.setColumns(5);
95
96 JLabel label_6 = new JLabel("\u65F6");
97 contentPane.add(label_6);
98
99 textField_4 = new JTextField();
100 contentPane.add(textField_4);
101 textField_4.setColumns(5);
102
103 JLabel label_7 = new JLabel("\u5206");
104 contentPane.add(label_7);
105
106 textField_5 = new JTextField();
107 contentPane.add(textField_5);
108 textField_5.setColumns(5);
109
110 JLabel label_8 = new JLabel("\u79D2");
111 contentPane.add(label_8);
112
113 JLabel label_9 = new JLabel("\u95F9\u949F\u8BBE\u7F6E\uFF1A");
114 contentPane.add(label_9);
115
116 JButton button = new JButton("\u5F00");
117 contentPane.add(button);
118
119 JButton button_1 = new JButton("\u5173");
120 contentPane.add(button_1);
121
122 JLabel label_4 = new JLabel("");
123 contentPane.add(label_4);
124 }
125
126 }
运行结果如下:
2、设计一个用标签、文本行与按钮来登录的界面(用GridLayout布局方式)。如图所示。(图略)
程序如下:
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 Identfiant extends JFrame {
8
9 private JPanel contentPane;
10 private JTextField textField;
11 private JPasswordField passwordField;
12 public static void main(String[] args)
13 {
14 EventQueue.invokeLater(new Runnable() {
15 public void run() {
16 try {
17 Identfiant frame = new Identfiant();
18 frame.setVisible(true);
19 } catch (Exception e) {
20 e.printStackTrace();
21 }
22 }
23 });
24 }
25 public Identfiant() {
26 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27 setBounds(100, 100, 450, 300);
28 contentPane = new JPanel();
29 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
30 setContentPane(contentPane);
31 contentPane.setLayout(new GridLayout(2, 1, 0, 0));
32
33 JPanel panel = new JPanel();
34 contentPane.add(panel);
35 panel.setLayout(new GridLayout(2, 2, 0, 0));
36
37 JLabel label = new JLabel("\u7528\u6237\u540D\uFF1A");
38 label.setHorizontalAlignment(SwingConstants.CENTER);
39 panel.add(label);
40
41 textField = new JTextField();
42 panel.add(textField);
43 textField.setColumns(10);
44
45 JLabel label_1 = new JLabel("\u53E3\u4EE4\uFF1A");
46 label_1.setHorizontalAlignment(SwingConstants.CENTER);
47 panel.add(label_1);
48
49 passwordField = new JPasswordField();
50 panel.add(passwordField);
51
52 JPanel panel_1 = new JPanel();
53 contentPane.add(panel_1);
54 panel_1.setLayout(null);
55
56 JButton button = new JButton("\u786E\u5B9A");
57 button.setBounds(0, 55, 207, 61);
58 panel_1.add(button);
59
60 JButton button_1 = new JButton("\u53D6\u6D88");
61 button_1.addActionListener(new ActionListener() {
62 public void actionPerformed(ActionEvent arg0) {
63 }
64 });
65 button_1.setBounds(217, 56, 197, 60);
66 panel_1.add(button_1);
67 }
68
69 }
运行结果如下:
来源:oschina
链接:https://my.oschina.net/u/4340047/blog/3537029