Java 组建用法大全.docx
- 文档编号:8396996
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:77
- 大小:55.12KB
Java 组建用法大全.docx
《Java 组建用法大全.docx》由会员分享,可在线阅读,更多相关《Java 组建用法大全.docx(77页珍藏版)》请在冰豆网上搜索。
Java组建用法大全
学习swing组件,主要有三个内容
一是组件的显示,二是对组件支持的事件进行侦听,三是是自定义组件
1.JFrame
JFrame是主窗口,它和JDialog,JApplet的地位并列.但是,一个JFrame可以添加JDialog和JApplet进去它的内容面板,而反过来就不行
下面来看JFrame的例子
=================================================
Java代码
1packageblog.swing;
2importjavax.swing.*;
3importjava.awt.event.*;
4
5classJFrameDemo
6{
7JFramemainFrame;
8publicJFrameDemo(){
9mainFrame=newJFrame("JFrameDemoTitle");//创建一个JFrame
10mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭动作
11mainFrame.setSize(300,300);//设置窗口大小
12mainFrame.setLocationRelativeTo(null);//使窗口显示在屏幕中央
13
14mainFrame.addWindowListener(newWindowListener(){
15publicvoidwindowOpened(WindowEvente){System.out.println("windowopened");}
16publicvoidwindowClosing(WindowEvente){System.out.println("windowclosing");}
17publicvoidwindowClosed(WindowEvente){System.out.println("windowclosed");}
18publicvoidwindowIconified(WindowEvente){System.out.println("windowiconified");}
19publicvoidwindowDeiconified(WindowEvente){System.out.println("windowdeiconified");}
20publicvoidwindowActivated(WindowEvente){System.out.println("windowactivated");}
21publicvoidwindowDeactivated(WindowEvente){System.out.println("windowdeactivated");}
22});
23mainFrame.addWindowFocusListener(newWindowFocusListener(){
24publicvoidwindowGainedFocus(WindowEvente){System.out.println("gainedfocus");}
25publicvoidwindowLostFocus(WindowEvente){System.out.println("lostfocus");}
26});
27mainFrame.addWindowStateListener(newWindowStateListener(){
28publicvoidwindowStateChanged(WindowEvente){System.out.println("statechanged");}
29});
30
31mainFrame.setVisible(true);
32}
33publicstaticvoidmain(String[]args)
34{
35newJFrameDemo();
36}
37}
==========================================================
这里出现了三个不同的窗口事件侦听器,并实现了它们所有的方法
WindowListener和WindowFocusListener都可以对窗口失去,获得焦点进行侦听,不同的是,非帧窗口和对话框窗口不能接收WindowListener的windowActivated和windodwDeactivated事件
虽然可以用WindowListener对窗口的一些状态进行侦听,但是WindowStateListener提供了更多的支持.例如,WindowStateListener可以处理还原窗口的事件,可以判断一个窗口是不是在垂直和水平两个方向都可以最大化(命令提示符窗口只可以在垂直方向上最大化),而这些都是WindowListener都无能为力
2.JLabel
JLabel是一标签.在它的文本里嵌入html标签,可以简单实现一个超链接组件
Java代码
38packageblog.swing;
39importjava.awt.*;
40importjavax.swing.*;
41importjava.awt.event.*;
42importjava.io.*;
43classJLabelDemo
44{
45JFramemainFrame;
46JLabelsimpleLabel;
47publicJLabelDemo(){
48mainFrame=newJFrame("JLabelDemo");
49simpleLabel=newJLabel("
50simpleLabel.addMouseListener(newMouseAdapter(){//添加鼠标事件侦听器,当单击标签时,打开网页
51publicvoidmouseClicked(MouseEvente){
52try{
53Runtime.getRuntime().exec("cmd/cstart");
54}catch(IOExceptionee){
55ee.printStackTrace();
56}
57}
58});
59simpleLabel.setCursor(newCursor(Cursor.HAND_CURSOR));//设置手形鼠标
60mainFrame.getContentPane().add(simpleLabel);//将标签添加到窗口
61mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
62mainFrame.pack();//使窗口自动根据添加了的组件调整大小
63mainFrame.setLocationRelativeTo(null);
64mainFrame.setVisible(true);
65}
66publicstaticvoidmain(String[]args)
67{
68newJLabelDemo();
69}
70}
3.JButton
JButton是一个按钮.它和JLabel一样的简单
Java代码
71packageblog.swing;
72importjava.awt.*;
73importjavax.swing.*;
74importjava.awt.event.*;
75importjava.io.*;
76
77classJButtonDemo
78{
79JFramemainFrame;
80JButtonsimpleButton;
81publicJButtonDemo(){
82mainFrame=newJFrame("JButtonDemo");
83simpleButton=newJButton("XX搜索");
84mainFrame.getContentPane().add(simpleButton);
85simpleButton.addActionListener(newActionListener(){//添加动作侦听器,当按钮被按下时执行这里的代码以打开网页
86publicvoidactionPerformed(ActionEvente){
87try{
88Runtime.getRuntime().exec("cmd/cstart");
89}catch(IOExceptionee){
90ee.printStackTrace();
91}
92}
93});
94simpleButton.setCursor(newCursor(Cursor.HAND_CURSOR));
95mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
96mainFrame.pack();
97mainFrame.setLocationRelativeTo(null);
98mainFrame.setVisible(true);
99}
100publicstaticvoidmain(String[]args)
101{
102newJButtonDemo();
103}
104}
4.JTextField
一个文本框
Java代码
105packageblog.swing;
106importjavax.swing.*;
107importjava.awt.event.*;
108
109classJTextFieldDemo
110{
111JFramemainFrame;
112JTextFieldsimpleTextField;
113publicJTextFieldDemo(){
114mainFrame=newJFrame("JTextFieldDemo");
115simpleTextField=newJTextField(20);//构造宽度为20个字符的文本框
116mainFrame.getContentPane().add(simpleTextField);
117simpleTextField.addActionListener(newActionListener(){//在输入字符后按回车执行行代码,在标准输出窗口输出它的内容
118publicvoidactionPerformed(ActionEvente){
119System.out.println(simpleTextField.getText());
120}
121});
122mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
123mainFrame.pack();
124mainFrame.setLocationRelativeTo(null);
125mainFrame.setVisible(true);
126}
127publicstaticvoidmain(String[]args)
128{
129newJTextFieldDemo();
130}
131}
5.JTextArea
文本区域,与文本框不同的是它是多行的
Java代码
132packageblog.swing;
133importjava.awt.*;
134importjavax.swing.*;
135importjava.awt.event.*;
136
137classJTextAreaDemo
138{
139JFramemainFrame;
140JTextAreasimpleTextArea;
141JButtonappendButton;
142publicJTextAreaDemo(){
143mainFrame=newJFrame("JTextAreaDemo");
144simpleTextArea=newJTextArea(10,20);//创建一个显示10行20列的文本域
145simpleTextArea.setLineWrap(true);//设置它自动换行
146simpleTextArea.setWrapStyleWord(true);//设置它自动换行时根据单词换行,而不是根据字符
147appendButton=newJButton("appendtexttothetextarea");
148mainFrame.getContentPane().add(simpleTextArea,BorderLayout.PAGE_START);
149mainFrame.getContentPane().add(appendButton,BorderLayout.PAGE_END);
150appendButton.addActionListener(newActionListener(){
151publicvoidactionPerformed(ActionEvente){
152simpleTextArea.append("buttonappendedtexthere");
153System.out.println(simpleTextArea.getText());
154}
155});
156mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
157mainFrame.pack();
158mainFrame.setLocationRelativeTo(null);
159mainFrame.setVisible(true);
160}
161publicstaticvoidmain(String[]args)
162{
163newJTextAreaDemo();
164}
165}
6.JPasswordField
Java代码
166packageblog.swing;
167importjava.awt.*;
168importjavax.swing.*;
169importjava.awt.event.*;
170
171classJPasswordFieldDemo
172{
173JFramemainFrame;
174JPasswordFieldsimplePasswordField;
175publicJPasswordFieldDemo(){
176mainFrame=newJFrame("JPasswordFieldDemo");
177simplePasswordField=newJPasswordField(10);
178simplePasswordField.setEchoChar('*');//设定要显示的字符
179mainFrame.getContentPane().add(simplePasswordField);
180simplePasswordField.addActionListener(newActionListener(){//回车时执行的动作
181publicvoidactionPerformed(ActionEvente){
182char[]input=simplePasswordField.getPassword();
183for(charc:
input)
184System.out.print(c);
185}
186});
187mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
188mainFrame.pack();
189mainFrame.setLocationRelativeTo(null);
190mainFrame.setVisible(true);
191}
192publicstaticvoidmain(String[]args)
193{
194newJPasswordFieldDemo();
195}
196}
7.JPanel
一个面板.一般用作控制组件的布局.
Java代码
197packageblog.swing;
198importjavax.swing.*;
199
200classJPanelDemo
201{
202JFramemainFrame;
203JPanelsimplePanel;
204JButtonsimpleButton;
205JLabelsimpleLabel;
206publicJPanelDemo(){
207mainFrame=newJFrame("JPanelDemo");
208simplePanel=newJPanel();
209simpleButton=newJButton("button");
210simpleLabel=newJLabel("label");
211simplePanel.add(simpleLabel);
212simplePanel.add(simpleButton);
213mainFrame.getContentPane().add(simplePanel);
214mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
215mainFrame.pack();
216mainFrame.setLocationRelativeTo(null);
217mainFrame.setVisible(true);
218}
219publicstaticvoidmain(String[]args)
220{
221newJPanelDemo();
222}
223}
8.JCheckBox
复选框
Java代码
224packageblog.swing;
225importjavax.swing.*;
226importjava.awt.event.*;
227
228classJCheckBoxDemoimplementsItemListener
229{
230JFramemainFrame;
231JPanelmainPanel;
232JCheckBoxsimpleCheckBox1;
233JCheckBoxsimpleCheckBox2;
234publicJCheckBoxDemo(){
235mainFrame=newJFrame("JCheckBoxDemo");
236mainPanel=newJPanel();
237simpleCheckBox1=newJCheckBox("checkbox1");
238simpleCheckBox1.setMnemonic('1');
239simpleCheckBox1.addItemListener(this);
240simpleCheckBox2=newJCheckBox("checkbox2");
241simpleCheckBox2.setMnemonic('2');
242simpleCheckBox2.addItemListener(this);
243mainPanel.add(simpleCheckBox1);
244mainPanel.add(simpleCheckBox2);
245mainFrame.getContentPane().add(mainPanel);
246mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
247mainFrame.pack();
248mainFrame.setLocationRelativeTo(null);
249mainFrame.setVisible(true);
250}
251publicvoiditemStateChanged(ItemEvente){
252JCheckBoxcb=(JCheckBox)e.getSource();
253if(cb==simpleCheckBox1)
254System.out.println("simpleCheckBox1");
255else
256System.out.println("simpleCheckBox2");
257}
258publicstaticvoidmain(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 组建用法大全 组建 用法 大全