将动作侦听器添加到 RadioPanel
Add Action Listener to RadioPanel
我目前正在尝试为我的 JCheckBox
实现一个动作侦听器,这样当它被选中时,它将打开一个 JFileChooser
供用户选择他们希望 GUI 使用的文件.对于初学者,当用户选中该框时,我如何让控制台打印出 "Box clicked!"?
我已经有一段时间没有使用 Swing 编程了,所以任何建议都有帮助!
public class RadioPanel extends JPanel implements ActionListener
{
private static final long serialVersionUID = -1890379016551779953L;
private JCheckBox box;
private JLabel label;
public RadioPanel(String message)
{
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 0;
box = new JCheckBox();
this.add(box,c);
c.gridx = 1;
c.gridy = 0;
label = new JLabel(message);
this.add(label, c);
}
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
System.out.println("Is selected :" + selected);
}
};
box.addActionListener(actionListener);
我认为这是因为代码没有事件侦听器。
请参阅下面的代码。
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class RadioPanel extends JPanel implements ActionListener {
private static final long serialVersionUID = -1890379016551779953L;
private JCheckBox box;
private JLabel label;
public RadioPanel(String message) {
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 0;
box = new JCheckBox();
// here
box.addActionListener(event -> {
JCheckBox checkBox = (JCheckBox) event.getSource();
if (checkBox.isSelected()) {
System.out.println("Box clicked!");
}
});
this.add(box, c);
c.gridx = 1;
c.gridy = 0;
label = new JLabel(message);
this.add(label, c);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
我目前正在尝试为我的 JCheckBox
实现一个动作侦听器,这样当它被选中时,它将打开一个 JFileChooser
供用户选择他们希望 GUI 使用的文件.对于初学者,当用户选中该框时,我如何让控制台打印出 "Box clicked!"?
我已经有一段时间没有使用 Swing 编程了,所以任何建议都有帮助!
public class RadioPanel extends JPanel implements ActionListener
{
private static final long serialVersionUID = -1890379016551779953L;
private JCheckBox box;
private JLabel label;
public RadioPanel(String message)
{
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 0;
box = new JCheckBox();
this.add(box,c);
c.gridx = 1;
c.gridy = 0;
label = new JLabel(message);
this.add(label, c);
}
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
System.out.println("Is selected :" + selected);
}
};
box.addActionListener(actionListener);
我认为这是因为代码没有事件侦听器。 请参阅下面的代码。
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class RadioPanel extends JPanel implements ActionListener {
private static final long serialVersionUID = -1890379016551779953L;
private JCheckBox box;
private JLabel label;
public RadioPanel(String message) {
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 0;
box = new JCheckBox();
// here
box.addActionListener(event -> {
JCheckBox checkBox = (JCheckBox) event.getSource();
if (checkBox.isSelected()) {
System.out.println("Box clicked!");
}
});
this.add(box, c);
c.gridx = 1;
c.gridy = 0;
label = new JLabel(message);
this.add(label, c);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}