java图像界面开发简单实例
JTextArea、JScrollPane、JPanel、JButton应用实例,通过‘插入文本’按钮功能,写入测试文本,换行功能可以进行换行和不换行,代码如下:
import
java.awt.BorderLayout;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
javax.swing.JButton;
import
javax.swing.JFrame;
import
javax.swing.JPanel;
import
javax.swing.JScrollPane;
import
javax.swing.JTextArea;
/**
* JTextArea、JScrollPane、JPanel、JButton应用实例
*
@author
左杰 jdk 5.0
*/
public
class
Example8Frame
extends
JFrame {
private
static
final
long
serialVersionUID
=
1L
;
private
JTextArea textArea;
//
文本域
private
JScrollPane scrollPane;
//
带滚动条面板
private
JPanel buttonPanel;
//
存放按钮面板
private
JButton wrapButton;
//
换行按钮
public
Example8Frame() {
setTitle(
"
文本编辑
"
);
//
设置窗体标题
setSize(
300
,
300
);
//
设置窗体大小
//
创建JTextArea组件
textArea
=
new
JTextArea();
//
创建JScrollPane面板,将JTextArea组件放入带滚动条面板中
scrollPane
=
new
JScrollPane(textArea);
在窗体中添加带滚动条面板
add(scrollPane, BorderLayout.CENTER);
buttonPanel
=
new
JPanel();
//
创建按钮面板
//
创建“插入文本”按钮,并添加点击事件,添加测试文本
JButton insertButton
=
new
JButton(
"
插入文本
"
);
//
创建“插入文本”按钮
buttonPanel.add(insertButton);
//
在面板中添加该按钮
//
为该按钮添加事件监听
insertButton.addActionListener(
new
ActionListener() {
public
void
actionPerformed(ActionEvent event) {
textArea.append(
"
这是一个textArea组件的简单应用实例,这为测试文本!
"
);
//
JTextArea组件添加文本信息
}
});
//
添加“换行”按钮,并添加点击事件,来控制换行和不换行
wrapButton
=
new
JButton(
"
换行
"
);
//
创建“换行文本”按钮
buttonPanel.add(wrapButton);
//
在面板中添加该按钮
//
为该按钮添加事件监听
wrapButton.addActionListener(
new
ActionListener() {
public
void
actionPerformed(ActionEvent event) {
boolean
wrap
=
!
textArea.getLineWrap();
//
获取JTextArea组件的是否换行状态,默认为false,取反
textArea.setLineWrap(wrap);
//
重新设置是否换行属性
wrapButton.setText(wrap
?
"
不换行
"
:
"
换行
"
);
//
根据属性设置按钮显示文本
}
});
//
在窗体中添加按钮面板
add(buttonPanel, BorderLayout.SOUTH);
}
public
static
void
main(String[] args) {
Example8Frame frame
=
new
Example8Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(
true
);
}
}