Java Tutorial/Swing Event/Event Adapter
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
Multiple sources: A listener can be plugged into several sources.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton o = (JButton) e.getSource();
String label = o.getText();
System.out.println(label + " button clicked");
}
}
public class MultipleSources {
public static void main(String[] args) {
JPanel panel = new JPanel();
JButton close = new JButton("Close");
close.addActionListener(new ButtonListener());
JButton open = new JButton("Open");
open.addActionListener(new ButtonListener());
JButton find = new JButton("Find");
find.addActionListener(new ButtonListener());
JButton save = new JButton("Save");
save.addActionListener(new ButtonListener());
panel.add(close);
panel.add(open);
panel.add(find);
panel.add(save);
JFrame f = new JFrame();
f.add(panel);
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Register several listeners for one event.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
public class MultipleListeners {
JLabel statusbar = new JLabel("0");
JSpinner spinner = new JSpinner();
static int count = 0;
class ButtonListener1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
Integer val = (Integer) spinner.getValue();
spinner.setValue(++val);
}
}
class ButtonListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
statusbar.setText(Integer.toString(++count));
}
}
public MultipleListeners() {
JPanel panel = new JPanel();
JButton add = new JButton("+");
add.addActionListener(new ButtonListener1());
add.addActionListener(new ButtonListener2());
panel.add(add);
panel.add(spinner);
JFrame f = new JFrame();
f.add(panel);
f.add(statusbar, BorderLayout.SOUTH);
f.setSize(300, 200);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) {
new MultipleListeners();
}
}
Removing listeners: remove the registered listeners.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RemoveListener {
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println(++count);
}
}
JButton add = new JButton("+");
JCheckBox active = new JCheckBox("Active listener");
ButtonListener buttonlistener = new ButtonListener();
int count = 0;
public RemoveListener() {
JPanel panel = new JPanel();
active.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if (active.isSelected()) {
add.addActionListener(buttonlistener);
} else {
add.removeActionListener(buttonlistener);
}
}
});
panel.add(add);
panel.add(active);
JFrame f = new JFrame();
f.add(panel);
f.setSize(310, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) {
new RemoveListener();
}
}
void java.awt.Toolkit.addAWTEventListener(AWTEventListener listener, long eventMask)
import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ruponentEvent;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class Main implements AWTEventListener {
public void eventDispatched(AWTEvent evt) {
if (evt.getID() == WindowEvent.WINDOW_OPENED) {
ComponentEvent cev = (ComponentEvent) evt;
if (cev.getComponent() instanceof JFrame) {
System.out.println("event: " + evt);
JFrame frame = (JFrame) cev.getComponent();
loadSettings(frame);
}
}
}
public static void loadSettings(JFrame frame) {
System.out.println("loading");
}
public static void saveSettings() {
System.out.println("saving");
}
public static void main(String[] args) throws Exception {
Toolkit tk = Toolkit.getDefaultToolkit();
final Main main = new Main();
tk.addAWTEventListener(main, AWTEvent.WINDOW_EVENT_MASK);
final JFrame frame = new JFrame("");
frame.setName("your frame");
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("File");
menu.add(new AbstractAction("Quit") {
public void actionPerformed(ActionEvent evt) {
try {
main.saveSettings();
System.exit(0);
} catch (Exception ex) {
System.out.println(ex);
}
}
});
mb.add(menu);
frame.setJMenuBar(mb);
frame.pack();
frame.setVisible(true);
}
}