Java Tutorial/Swing/ButtonModel

Материал из Java эксперт
Перейти к: навигация, поиск

Add change listener to Button model

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main extends JFrame {
  JButton b = new JButton("Test");
  ButtonModel model = b.getModel();
  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    model.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        System.out.println("Armed: " + model.isArmed() + " Enabled: "
            + model.isEnabled() + " Pressed: " + model.isPressed());
      }
    });
    getContentPane().add(b, BorderLayout.CENTER);
    pack();
    setVisible(true);
  }
  public static void main(String arg[]) {
    new Main();
  }
}





A default button model

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonModel;
import javax.swing.DefaultButtonModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] args) {
    final JButton ok = new JButton("ok");
    JCheckBox cb = new JCheckBox("Enabled", true);
    cb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (ok.isEnabled())
          ok.setEnabled(false);
        else
          ok.setEnabled(true);
      }
    });
    ButtonModel model = new DefaultButtonModel() {
      public void setEnabled(boolean b) {
        if (b)
          System.out.println("Pressed: true");
        else
          System.out.println("Pressed: false");
        super.setEnabled(b);
      }
      public void setArmed(boolean b) {
        if (b)
          System.out.println("Armed: true");
        else
          System.out.println("Armed: false");
        super.setArmed(b);
      }
      public void setPressed(boolean b) {
        if (b)
          System.out.println("Pressed: true");
        else
          System.out.println("Pressed: false");
        super.setPressed(b);
      }
    };
    ok.setModel(model);
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(ok);
    f.add(cb);
    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}





Model for a JButton: manage only the state of the button

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultButtonModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ButtonModel {
  public static void main(String[] args) {
    final JButton ok = new JButton("ok");
    JCheckBox cb = new JCheckBox("Enabled", true);
    ok.setBounds(40, 30, 80, 25);
    ok.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        DefaultButtonModel model = (DefaultButtonModel) ok.getModel();
        if (model.isEnabled())
          System.out.println("Enabled: true");
        else
          System.out.println("Enabled: false");
        if (model.isArmed())
          System.out.println("Armed: true");
        else
          System.out.println("Armed: false");
        if (model.isPressed())
          System.out.println("Pressed: true");
        else
          System.out.println("Pressed: false");
      }
    });
    cb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (ok.isEnabled())
          ok.setEnabled(false);
        else
          ok.setEnabled(true);
      }
    });
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(ok);
    f.add(cb);
    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}