博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Java】 JButton、JTextField、javax.swing.text.Document
阅读量:6710 次
发布时间:2019-06-25

本文共 4930 字,大约阅读时间需要 16 分钟。

 

Table of Contents

1 JButton

 

1.1 JToggleButton

按下后不起来,再按才会起来

1.2 JCheckBox

复选框

1.3 JRadioButton

单选按钮

1.4 BasicArrowButton

带箭头的按钮,方向由BasicArrowButton.NORTH SOUTH EAST WEST指定

1.5 setPressedIcon(Icon pressIcon)

当按钮按下时显示的图片

1.6 setDisabledIcon(Icon disableIcon)

当JButton不可用时显示的图片

1.7 setToolTipText(String text)

显示提示信息

package xjtu.vf.swing;import java.awt.FlowLayout;import javax.swing.*;import javax.swing.plaf.basic.*;import javax.swing.border.*;public class Buttons extends JFrame {    private JButton jb = new JButton("JButton");    private BasicArrowButton        up = new BasicArrowButton(SwingConstants.NORTH),        down = new BasicArrowButton(BasicArrowButton.SOUTH),        left = new BasicArrowButton(BasicArrowButton.WEST),        right = new BasicArrowButton(BasicArrowButton.EAST);    public Buttons(){        setLayout(new FlowLayout());        add(jb);        add(new JToggleButton("JToggleButton"));        add(new JCheckBox("JCheckButton"));        add(new JRadioButton("JRadioButton"));        JPanel jp = new JPanel();        jp.setBorder(new TitledBorder("Directions"));        jp.add(up);        jp.add(down);        jp.add(left);        jp.add(right);        add(jp);    }    public static void main(String[] args){        SwingConsole.run(new Buttons(), 350, 200);    }}

  

2 JTextField

 

2.1 addActionListener(ActionEvent t)

文本域的事件监听器,当在文本区域内输入回车时响应。

2.2 setDocument(Document doc)

关联当前文本区一document

3 Document

在javax.swing.text中 The Document is a container for text that serves as the model for swing text components. The goal for this interface is to scale from very simple needs (a plain text textfield) to complex needs (an HTML or XML document, for example). document充当swing文本组件的包含文本的容器,

3.1 addDocumentListener(DocumentListener listener)

添加事件监听器

3.1.1 changeUpdate(DocumentEvent e)

Gives notification that an attribute or set of attributes changed.

3.1.2 insertUpdate(DocumentEvent e)

Gives notification that there was an insert into the document.

3.1.3 removeUpdate(DocumentEvent e)

Gives notification that a portion of the document has been removed.

3.2 insertString(int offset, String str, AttributeSet a)

插入字符串,当DocumentEvent的insertUpdate(ActionEvent e)事件发生的时候会调用此函数。

package xjtu.vf.swing;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.text.*;public class TextFields extends JFrame{	private JButton b1 = new JButton("Get Text"), b2 = new JButton("Set Text");	private JTextField t1 = new JTextField(30), t2 = new JTextField(30),        t3 = new JTextField(30);	private String s = "";	private UpperCaseDocument ucd = new UpperCaseDocument();	public TextFields() {		t1.setDocument(ucd);		ucd.addDocumentListener(new T1());		b1.addActionListener(new B1());		b2.addActionListener(new B2());		t1.addActionListener(new T1A());		setLayout(new FlowLayout());		add(b1);		add(b2);		add(t1);		add(t2);		add(t3);	}	class B1 implements ActionListener {		// B1被按下时,若此时t1中有选中文本则把s赋为选中文本否则s为t1中的所有文本		public void actionPerformed(ActionEvent e) {			if (t1.getSelectedText() == null)				s = t1.getText();			else				s = t1.getSelectedText();			t1.setEditable(true);		}	}	class B2 implements ActionListener {		public void actionPerformed(ActionEvent e) {			// 当B2被按下时,先把ucd的大写标记设为假,所以下面当改变t1文本内容时不会改变文本的大小写			// 若没有设置为false则"Inserted by Button2: "在t1中显示的时候会变成大写			ucd.setUpperCase(false);			t1.setText("Inserted by Button2: " + s);			ucd.setUpperCase(true);			t1.setEditable(false);		}	}	/*	 * t1的事件监听器,在t1中输入回车时在t3上显示文本	 */	class T1A implements ActionListener {        private int count = 0;		public void actionPerformed(ActionEvent e) {			t3.setText("t1 Action Event: " + count++);		}	}	/**	 * 	 * @author Administrator	 *	changeUpdate当属性发生改变时	 *	isnertUpdate 当在文本区内插入文本时调用:插入文本时会调用insertString函数	 *	removeUpdate 当文本区内删除内容时调用	 */	class T1 implements DocumentListener {		@Override		public void changedUpdate(DocumentEvent arg0) {		}		@Override		public void insertUpdate(DocumentEvent arg0) {			//这里设置t1与t2同时显示t1中输入内容,t3中比t1,t2多"TExt: "			t2.setText(t1.getText());			t3.setText("Text: " + t1.getText());		}		@Override		public void removeUpdate(DocumentEvent arg0) {			// t1中文本发生删除时,t2与t1同步			t2.setText(t1.getText());		}	}		public static void main(String[] args) {		SwingConsole.run(new TextFields(), 375, 200);	}	@SuppressWarnings("serial")	class UpperCaseDocument extends PlainDocument {		private boolean upperCase = true;		public void setUpperCase(boolean flag) {			upperCase = flag;		}		public void insertString(int offset, String str, AttributeSet attset)				throws BadLocationException {			if (upperCase)				str = str.toUpperCase();			super.insertString(offset, str, attset);		}	}}

  

 

 

 

Author: visaya fan

Date: 2011-11-20 21:21:16

HTML generated by org-mode 6.33x in emacs 23

转载于:https://www.cnblogs.com/visayafan/archive/2011/11/20/2256475.html

你可能感兴趣的文章
Linux 启动、关闭、重启网络服务
查看>>
Sublime Text 相关
查看>>
深入理解css优先级
查看>>
android的armeabi和armeabi-v7a
查看>>
android自己定义控件系列教程-----仿新版优酷评论剧集卡片滑动控件
查看>>
lvs之 lvs+nginx+tomcat_1、tomcat_2+redis(lvs dr 模式)
查看>>
让“是男人就下到100层”在Android平台上跑起来
查看>>
hdu4292Food(最大流Dinic算法)
查看>>
webdriver API study
查看>>
【Machine Learning in Action --4】朴素贝叶斯过滤网站的恶意留言
查看>>
Ubuntu+Eclipse+ADT+Genymotion+VirtualBox开发环境搭建
查看>>
Android 学习之 开源项目PullToRefresh的使用
查看>>
Matplot中文乱码完美解决方式
查看>>
tomcat的webappclassloader中一个奇怪的异常信息
查看>>
漫谈程序猿系列:群星闪耀的黄金时代
查看>>
2016百度编程题:蘑菇阵
查看>>
webpack系列之一总览
查看>>
如何打造BCH使用的刚性需求?
查看>>
一个小需求引发的思考
查看>>
慎用System.nanoTime()
查看>>