Java Swing复杂控件Word文档格式.docx
《Java Swing复杂控件Word文档格式.docx》由会员分享,可在线阅读,更多相关《Java Swing复杂控件Word文档格式.docx(43页珍藏版)》请在冰豆网上搜索。
StringgetText()
返回JTextArea中的内容
intgetLineCount()
返回JTextArea中当前文本所占的行数
setLineWrap(boolean
wrap)
设置文本区的换行策略
9.1.3文本区与文本域
下面示例展示了文本区与文本域的文本显示特点,文本域只能输入和显示单行文本,且不能提供换行与滚动条功能,而文本区则既可以提供通过换行的多行显示,也能通过将文本区添加到一个JScrollPane(滚动窗格)中实现垂直和水平的滚动功能。
示例1
packageedu.learn;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassJTextAreaExampleextendsJFrameimplementsActionListener{
JLabeluserLab=newJLabel("
发信人:
"
JLabel.LEFT);
JTextFielduserText=newJTextField(20);
JLabelmailLab=newJLabel("
所发信息:
JTextFieldmailText=newJTextField(20);
JLabelinfoLabe=newJLabel("
信息记录:
JTextAreatxtArea=newJTextArea(5,20);
publicJTextAreaExample()
{
init();
setTitle("
文本域练习"
);
setBounds(200,200,250,270);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicvoidinit()
setLayout(newFlowLayout(FlowLayout.LEFT));
add(userLab);
add(userText);
add(mailLab);
add(mailText);
add(infoLabe);
//JTextArea不能自己提供滚动条,它必须借助于滚动窗格JScrollPane
JScrollPanescroll=newJScrollPane(txtArea);
txtArea.setEditable(false);
add(scroll);
mailText.addActionListener(this);
publicvoidactionPerformed(ActionEvente)
txtArea.append(userText.getText()+"
:
"
+mailText.getText()+"
\n"
mailText.setText("
publicstaticvoidmain(String[]args){
newJTextAreaExample();
}
运行结果:
9.1.4文本区上的DocumentEvent事件
JTextArea经常触发的事件是DocumentEvent事件,DocumentEvent类放在javax.swing.envent包中。
当文本区中的文本进行编辑操作,使文本区中的文本内容发生变化时,就会导致文本区中的文档模型中的数据发生变化,从而导致文本区触发DocumentEvent事件。
DocumentEvent事件实现的接口以及接口中的抽象事件处理方法如下:
事件类型
对应监听器接口
接口中的抽象事件方法
DocumentEvent
DocumentListener
publicvoidchangeUpdate(DocumentEvente)
;
publicvoidremoveUpdate(DocumentEvente)
publicvoidinsertUpdate(DocumentEvente)
以下示例展示了文本区的DocumentEvent(文档事件)的处理方法,该示例中有两个文本区,当用户朝一个文本区中输入文本时,就触发DocumentEvent事件中的文档内容改变事件,从而把修改的内容添加到另一个文本区中。
示例2
importjavax.swing.event.*;
importjava.util.*;
publicclassWindowTextSortextendsJFrameimplementsDocumentListener{
JTextAreatext1,text2;
WindowTextSort(){
init();
setLayout(newFlowLayout());
setBounds(120,100,300,240);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
voidinit(){
text1=newJTextArea(5,25);
text2=newJTextArea(5,25);
add(newJScrollPane(text1));
add(newJScrollPane(text2));
text1.setLineWrap(true);
text2.setLineWrap(true);
text2.setEditable(false);
(text1.getDocument()).addDocumentListener(this);
//向文档注册监视器
publicvoidchangedUpdate(DocumentEvente){//接口方法
Stringstr=text1.getText();
Strings=str.substring(str.length()-1);
text2.append(s);
publicvoidremoveUpdate(DocumentEvente){//接口方法
changedUpdate(e);
publicvoidinsertUpdate(DocumentEvente){//接口方法
publicstaticvoidmain(Stringargs[]){
WindowTextSortwin=newWindowTextSort();
win.setTitle("
窗口"
9.2JCheckBox(复选框)
JCheckBox复选框是一个拥有被选定状态和取消选定状态的对象,用户可以通过点击复选框对象来切换其状态
9.2.1JCheckBox的构造函数
JCheckBox()
创建一个没有文本、没有图标并且最初未被选定的复选框
JCheckBox(Stringname)
创建一个带文本的、最初未被选定的复选框
JCheckBox(Iconicon)
创建有一个图标、最初未被选定的复选框
JCheckBox(Stringname,Iconicon)
创建带有指定文本和图标的、最初未选定的复选框
9.2.2JCheckBox的常用方法
setIcon(Iconicon)
设置当前复选框的图标
setText(Stringname)
设置当前复选框的文本
获得当前复选框的文本
booleanisSeleced()
获得当前复选框的是否被选中的状态,如果被选中则返回true,否则返回false
9.2.3JCheckBox的ItemEvent事件以及ActionEvent事件
当JCheckBox(复选框)的选中状态由于用户的鼠标点击而出现改变时,复选框就会触发ItemEvent事件。
其中ItemEvent对应的监听器接口和抽象方法如下:
ItemEvent
ItemListener
publicvoidItemStateChanged(ItemEvente);
JCheckBox注册ItemEvent事件监听器对象的方法如下:
addItemListener(ItemListenerlistener)
注册ItemEvent事件监听器对象
removeItemListener(ItemListenerlistener)
移除ItemEvent事件监听器对象
以下是一个通过多个复选框选择购买电脑附件的示例,每当用户选中或者是取消复选框状态时就触发ItemEvent事件,在该事件中将被选中的复选框中的内容获取后显示在下面的文本框中。
示例3
publicclassJCheckBoxExampleextendsJFrameimplementsActionListener{
JCheckBoxcb1=newJCheckBox("
无限鼠标"
JCheckBoxcb2=newJCheckBox("
移动硬盘"
JCheckBoxcb3=newJCheckBox("
U盘"
JCheckBoxcb4=newJCheckBox("
打印机"
JPanelp1=newJPanel();
JLabellabel=newJLabel("
需要购买的附件:
publicJCheckBoxExample()
复选框练习"
p1.setLayout(newGridLayout(4,1));
p1.setBorder(BorderFa