关于监听器的用法

//关于单行文本
   textField = new JTextField(15);
   /* 监听文本光标移动事件 */
   textField.addCaretListener(new CaretListener() {
    public void caretUpdate(CaretEvent e) {
     // 假如改变了内容,就可以即时更新 label 显示的内容
     label.setText(textField.getText());
    }
   })
       getContentPane().add(textField);
//这个就很全面拉
//可以不看的################################import javax.swing.*;
                          import java.awt.event.*;

                         public class TestButtons {

                       JFrame frame = new JFrame("Test Buttons");
                       JButton jButton = new JButton("JButton"); //按钮
                       JToggleButton toggle = new JToggleButton("Toggle Button"); //切换按钮
                       JCheckBox checkBox = new JCheckBox("Check Box"); //复选按钮
                       JRadioButton radio1 = new JRadioButton("Radio Button 1"); //单选按钮
                       JRadioButton radio2 = new JRadioButton("Radio Button 2");
                       JRadioButton radio3 = new JRadioButton("Radio Button 3");
                       JLabel label = new JLabel("Here is Status, look here."); //不是按钮,是静态                                                                                      文本

                       public TestButtons() {
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      frame.getContentPane().setLayout(new java.awt.FlowLayout());#######//

  /* 为一般按钮添加动作监听器 */
  jButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent ae) {
   label.setText("You clicked jButton");
  }
 });

 /* 为切换按钮添加动作监听器 */
 toggle.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent ae) {
   JToggleButton toggle = (JToggleButton) ae.getSource();
   if (toggle.isSelected()) {
    label.setText("You selected Toggle Button");
   } else {
    label.setText("You deselected Toggle Button");
   }
  }
 });

 /* 为复选按钮添加条目监听器 */
 checkBox.addItemListener(new ItemListener() {
 public void itemStateChanged(ItemEvent e) {
  JCheckBox cb = (JCheckBox) e.getSource();
  label.setText("Selected Check Box is " + cb.isSelected());
 }
});

 /* 用一个按钮组对象包容一组单选按钮 */
 ButtonGroup group = new ButtonGroup();
 /* 生成一个新的动作监听器对象,备用 */
 ActionListener al = new ActionListener() {
 public void actionPerformed(ActionEvent ae) {
  JRadioButton radio = (JRadioButton) ae.getSource();
  if (radio == radio1) {
   label.setText("You selected Radio Button 1");
  } else if (radio == radio2) {
   label.setText("You selected Radio Button 2");
  } else {
   label.setText("You selected Radio Button 3");
  }
 }
};
 /* 为各单选按钮添加动作监听器 */
 radio1.addActionListener(al);
 radio2.addActionListener(al);
 radio3.addActionListener(al);
 /* 将单选按钮添加到按钮组中 */
 group.add(radio1);

 group.add(radio2);
 group.add(radio3);

 frame.getContentPane().add(jButton);
 frame.getContentPane().add(toggle);
 frame.getContentPane().add(checkBox);
 frame.getContentPane().add(radio1);
 frame.getContentPane().add(radio2);
 frame.getContentPane().add(radio3);
 frame.getContentPane().add(label);

 frame.setSize(200, 250);
}

 public void show() {
  frame.show();
 }

 public static void main(String[] args) {
  TestButtons tb = new TestButtons();
  tb.show();
 }


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值