import javax.swing.*;
import javax.swing.text.*;
import java.awt.*; //为了实现布局管理
import java.awt.event.*; //为了处理事件处理
import java.net.URL;
import java.io.IOException;
public class TextSamplerDemo extends JFrame
implements ActionListener {
private String newline = "\n";
protected static final String textFieldString = "请输入用户名";
protected static final String passwordFieldString = "请输入密码";
protected JLabel actionLabel;
public TextSamplerDemo() {
super("TextSamplerDemo");
//创建一个单行文本区组件.
JTextField textField = new JTextField(10);
textField.setActionCommand(textFieldString);
textField.addActionListener(this);
//创建一个密码文本框.
JPasswordField passwordField = new JPasswordField(10);
passwordField.setActionCommand(passwordFieldString);
passwordField.addActionListener(this);
//创建一些标签.
JLabel textFieldLabel = new JLabel(textFieldString + ":");
textFieldLabel.setLabelFor(textField);
JLabel passwordFieldLabel = new JLabel(passwordFieldString + ":");
passwordFieldLabel.setLabelFor(passwordField);
//创建一个标签用来存放事件处理消息.
actionLabel = new JLabel("请在文本区组件中输入文字,然后按下回车键.");
actionLabel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
//对文本组件和标签进行布局管理.
JPanel textControlsPane = new JPanel();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
textControlsPane.setLayout(gridbag);
JLabel[] labels = {textFieldLabel, passwordFieldLabel};
JTextField[] textFields = {textField, passwordField};
addLabelTextRows(labels, textFields, gridbag, textControlsPane);
c.gridwidth = GridBagConstraints.REMAINDER; //最后
c.anchor = GridBagConstraints.WEST;
c.weightx = 1.0;
gridbag.setConstraints(actionLabel, c);
textControlsPane.add(actionLabel);
textControlsPane.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("文本区组件"),
BorderFactory.createEmptyBorder(5,5,5,5)));
//创建一个文本区域.
JTextArea textArea = new JTextArea(
"This is an editable JTextArea " +
"that has been initialized with the setText method. " +
"A text area is a \"plain\" text component, " +
"which means that although it can display text " +
"in any font, all of the text is in the same font."
);
textArea.setFont(new Font("Serif", Font.ITALIC, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane areaScrollPane = new JScrollPane(textArea);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 250));
areaScrollPane.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("普通文字"),
BorderFactory.createEmptyBorder(5,5,5,5)),
areaScrollPane.getBorder()));
//创建一个编辑面板.
JEditorPane editorPane = createEditorPane();
JScrollPane editorScrollPane = new JScrollPane(editorPane);
editorScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
editorScrollPane.setPreferredSize(new Dimension(250, 145));
editorScrollPane.setMinimumSize(new Dimension(10, 10));
//创建一个文本面板.
JTextPane textPane = createTextPane();
JScrollPane paneScrollPane = new JScrollPane(textPane);
paneScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
paneScrollPane.setPreferredSize(new Dimension(250, 155));
paneScrollPane.setMinimumSize(new Dimension(10, 10));
//将可编辑文本框和文本框放在一个split pane组件中.
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
editorScrollPane,
paneScrollPane);
splitPane.setOneTouchExpandable(true);
JPanel rightPane = new JPanel();
rightPane.add(splitPane);
rightPane.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("多风格文字"),
BorderFactory.createEmptyBorder(5,5,5,5)));
//将所有的东西都放入applet中.
JPanel leftPane = new JPanel();
BoxLayout leftBox = new BoxLayout(leftPane, BoxLayout.Y_AXIS);
leftPane.setLayout(leftBox);
leftPane.add(textControlsPane);
leftPane.add(areaScrollPane);
JPanel contentPane = new JPanel();
BoxLayout box = new BoxLayout(contentPane, BoxLayout.X_AXIS);
contentPane.setLayout(box);
contentPane.add(leftPane);
contentPane.add(rightPane);
setContentPane(contentPane);
}
private void addLabelTextRows(JLabel[] labels,
JTextField[] textFields,
GridBagLayout gridbag,
Container container) {
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
int numLabels = labels.length;
for (int i = 0; i < numLabels; i++) {
c.gridwidth = GridBagConstraints.RELATIVE; //倒数第二
c.fill = GridBagConstraints.NONE; //重新设置为默认值
c.weightx = 0.0; //重新设置为默认值
gridbag.setConstraints(labels[i], c);
container.add(labels[i]);
c.gridwidth = GridBagConstraints.REMAINDER; //最后一行
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
gridbag.setConstraints(textFields[i], c);
container.add(textFields[i]);
}
}
public void actionPerformed(ActionEvent e) {
String prefix = "您输入了 \"";
if (e.getActionCommand().equals(textFieldString)) {
JTextField source = (JTextField)e.getSource();
actionLabel.setText(prefix + source.getText() + "\"");
} else {
JPasswordField source = (JPasswordField)e.getSource();
actionLabel.setText(prefix + new String(source.getPassword())
+ "\"");
}
}
private JEditorPane createEditorPane() {
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
String s = null;
try {
s = "file:"
+ System.getProperty("user.dir")
+ System.getProperty("file.separator")
+ "TextSamplerDemoHelp.html";
URL helpURL = new URL(s);
displayURL(helpURL, editorPane);
} catch (Exception e) {
System.err.println("Couldn't create help URL: " + s);
}
return editorPane;
}
private void displayURL(URL url, JEditorPane editorPane) {
try {
editorPane.setPage(url);
} catch (IOException e) {
System.err.println("Attempted to read a bad URL: " + url);
}
}
private JTextPane createTextPane() {
JTextPane textPane = new JTextPane();
String[] initString =
{ "This is an editable JTextPane, ", //正常
"another ", //斜体
"styled ", //粗体
"text ", //小
"component, ", //大
"which supports embedded components..." + newline,//正常
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
JAVA SWING图形界面编程 (114个子文件)
TextSamplerDemo.class 8KB
TumbleItem.class 4KB
EventDemo.class 4KB
Converter.class 4KB
LunarPhases.class 4KB
ConverterRangeModel.class 3KB
VoteDialog.class 3KB
MenuLookDemo.class 3KB
CelsiusConverter2.class 3KB
ConversionPanel.class 3KB
HtmlDemo.class 3KB
CelsiusConverter.class 3KB
VoteDialog$1.class 2KB
ClickCounter.class 2KB
ImagePreview.class 2KB
FormattedDocument.class 2KB
SwingWorker.class 2KB
FileChooserDemo2$1.class 2KB
FileChooserDemo2.class 1KB
FollowerRangeModel.class 1KB
DecimalField.class 1KB
ImageFileView.class 1KB
TumbleItem$2.class 1KB
ClickCounter$1.class 996B
ConversionPanel$2.class 907B
HtmlDemo$1.class 904B
SwingWorker$2.class 901B
TumbleItem$1.class 758B
ImageFilter.class 710B
Utils.class 708B
ConversionPanel$3.class 700B
ConversionPanel$1.class 697B
HelloWorldSwing.class 677B
ConversionPanel$4.class 663B
HtmlDemo$2.class 655B
Unit.class 604B
TextSamplerDemo$1.class 563B
MenuLookDemo$1.class 504B
FileChooserDemo2$2.class 484B
ClickCounter$2.class 472B
SwingWorker$ThreadVar.class 467B
EventDemo$1.class 463B
Converter$1.class 463B
HtmlDemo$3.class 460B
SwingWorker$1.class 443B
TextSamplerDemo$2.class 395B
Thumbs.db 35KB
Thumbs.db 16KB
Thumbs.db 7KB
Thumbs.db 6KB
Thumbs.db 4KB
Thumbs.db 4KB
Pig.gif 8KB
T13.GIF 2KB
T12.GIF 2KB
T17.GIF 2KB
T14.GIF 2KB
T10.GIF 2KB
T7.GIF 2KB
T9.GIF 2KB
T6.GIF 2KB
T16.GIF 2KB
T11.GIF 2KB
T4.GIF 2KB
T15.GIF 2KB
T8.GIF 2KB
T5.GIF 2KB
T1.GIF 2KB
T2.GIF 2KB
T3.GIF 2KB
dukeWaveRed.gif 2KB
CONVERT.GIF 1KB
tiffIcon.gif 565B
gifIcon.gif 557B
jpgIcon.gif 481B
SOUND.GIF 248B
MIDDLE.GIF 235B
MIDDLE.GIF 235B
LEFT.GIF 172B
RIGHT.GIF 172B
TextSamplerDemoHelp.html 498B
TextSamplerDemo.java 11KB
VoteDialog.java 7KB
Converter.java 6KB
TumbleItem.java 6KB
ConverterRangeModel.java 6KB
EventDemo.java 4KB
MenuLookDemo.java 4KB
ConversionPanel.java 4KB
LunarPhases.java 4KB
HtmlDemo.java 3KB
CelsiusConverter2.java 3KB
SwingWorker.java 2KB
FollowerRangeModel.java 2KB
CelsiusConverter.java 2KB
ClickCounter.java 2KB
FileChooserDemo2.java 2KB
FormattedDocument.java 2KB
ImageFileView.java 2KB
ImagePreview.java 2KB
共 114 条
- 1
- 2
chengjunhudie
- 粉丝: 2
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- date0425111111111111111111111
- 包含贪心算法的定义及python代码部分实现
- 自动驾驶-状态估计和定位之扩展卡尔曼滤波.pdf
- csdn之x-ca-key,x-ca-nonce,x-ca-signature与x-ca-signature-headers探索
- 基于TM1620数码显示芯片STM32单片机驱动程序软件源代码.zip
- 【tomcat6使用redis配置session共享】
- 包含杨辉三角的说明及java代码实现
- FDN371N-NL-VB一款SOT23封装N-Channel场效应MOS管
- AutomotiveSPICE-V4.0 中文版
- Java实现杨辉三角的生成和打印代码示例.md
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页