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); } } }
Date: 2011-11-20 21:21:16
HTML generated by org-mode 6.33x in emacs 23