java - JRadiobutton 结合 JButtons 实现。错误方法不是抽象的并且不会覆盖抽象方法

标签 java swing jbutton jradiobutton

我正在尝试实现一组使用 3 个 ItemListener 检测到的 3 个 JRadiobutton 以及由 2 个 ActionListener 选择的一组 2 个 JButton 。

当按下“OK”JButton 时,应检测到当前的 JRadioButton 选择。

我尝试通过将 ActionEventItemEvent 组合到一个方法中来实现此目的,其中 if 语句可用于验证按下“OK”时选择了哪个 JRadioButton

产生的错误消息是方法不是抽象的,并且不会覆盖actionListener中的抽象方法actionPerformed(ActionEvent)

当主方法被抽象时,它不会实例化。

我尝试在 public void actionPerformed 方法之前放置 @Override 并使用 alt+ins 插入覆盖,但这不起作用。

代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

class MainMenu extends JFrame implements ActionListener, ItemListener {

  JPanel pnl = new JPanel();
  JPanel grid = new JPanel(new GridLayout(3, 1));
  JRadioButton input1 = new JRadioButton("Enter New Information", true);
  JRadioButton input2 = new JRadioButton("Load from a CSV file");
  JRadioButton input3 = new JRadioButton("Save to a CSV file");
  ButtonGroup inputs = new ButtonGroup();
  JButton b1 = new JButton("OK");
  JButton b2 = new JButton("Cancel");
  JTextArea txtArea = new JTextArea(5, 25);

  public void actionPerformed(ActionEvent event2, ItemEvent event1) {

    txtArea.setText(event2.getActionCommand() + " Clicked");

    if (event2.getSource() == b1 && event1.getItemSelectable() == input1) {
        System.exit(0);
    }
    if (event2.getSource() == b1 && event1.getItemSelectable() == input2) {

        final JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(null);
        System.exit(0);
    }
    if (event2.getSource() == b1 && event1.getItemSelectable() == input3) {
        System.exit(0);
    }
    if (event2.getSource() == b2) {
        System.exit(0);
    }
  }

  public MainMenu() {

    super("Main Menu");
    setSize(300, 200);
    Container contentPane = getContentPane();
    ButtonGroup buttonGroup = new javax.swing.ButtonGroup();
    inputs.add(input1);
    inputs.add(input2);
    inputs.add(input3);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    add(grid);
    add(pnl);
    grid.add(input1);
    grid.add(input2);
    grid.add(input3);
    grid.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    pnl.add(b1);
    pnl.add(b2);
    pnl.add(txtArea);

    b1.addActionListener(this);
    b2.addActionListener(this);
    input1.addItemListener(this);
    input2.addItemListener(this);
    input3.addItemListener(this);
    contentPane.add("North", grid);
    contentPane.add("South", pnl);
    setVisible(true);
  }
}

最佳答案

基本上,您违反了 ActionListenerJRadioButton 的契约(Contract)要求(因为它们希望您向它们传递对实现 ItemListener 接口(interface)的类的引用)...

你不能简单地以这种方式“组成”回调,你必须满足你实现的接口(interface)的要求。

事实上,您并不真正关心 JRadioButton 何时发生变化,您只关心按下 OkCancel 按钮时的变化,因此您可以完全摆脱 ItemListener 要求,只需检查您感兴趣的 JRadioButtonselected 状态,例如...

@Override
public void actionPerformed(ActionEvent event2) {

    txtArea.setText(event2.getActionCommand() + " Clicked");
    Object source = event2.getSource();

    if (source == b1 && input1.isSelected()) {
        System.exit(0);
    }
    if (source == b1 && input2.isSelected()) {

        final JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(null);
        System.exit(0);
    }
    if (event2.getSource() == b1 && input3.isSelected()) {
        System.exit(0);
    }
    if (event2.getSource() == b2) {
        System.exit(0);
    }
}

作为一个可运行的示例......

import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

class MainMenu extends JFrame implements ActionListener {

    JPanel pnl = new JPanel();
    JPanel grid = new JPanel(new GridLayout(3, 1));
    JRadioButton input1 = new JRadioButton("Enter New Information", true);
    JRadioButton input2 = new JRadioButton("Load from a CSV file");
    JRadioButton input3 = new JRadioButton("Save to a CSV file");
    ButtonGroup inputs = new ButtonGroup();
    JButton b1 = new JButton("OK");
    JButton b2 = new JButton("Cancel");
    JTextArea txtArea = new JTextArea(5, 25);

    @Override
    public void actionPerformed(ActionEvent event2) {

        txtArea.setText(event2.getActionCommand() + " Clicked");
        Object source = event2.getSource();

        if (source == b1 && input1.isSelected()) {
            System.exit(0);
        }
        if (source == b1 && input2.isSelected()) {

            final JFileChooser fc = new JFileChooser();
            int returnVal = fc.showOpenDialog(null);
            System.exit(0);
        }
        if (event2.getSource() == b1 && input3.isSelected()) {
            System.exit(0);
        }
        if (event2.getSource() == b2) {
            System.exit(0);
        }
    }

    public MainMenu() {

        super("Main Menu");
        setSize(300, 200);
        Container contentPane = getContentPane();
        ButtonGroup buttonGroup = new javax.swing.ButtonGroup();
        inputs.add(input1);
        inputs.add(input2);
        inputs.add(input3);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(grid);
        add(pnl);
        grid.add(input1);
        grid.add(input2);
        grid.add(input3);
        grid.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        pnl.add(b1);
        pnl.add(b2);
        pnl.add(txtArea);

        b1.addActionListener(this);
        b2.addActionListener(this);
        input1.addActionListener(this);
        input2.addActionListener(this);
        input3.addActionListener(this);
        contentPane.add("North", grid);
        contentPane.add("South", pnl);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                MainMenu frame = new MainMenu();
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }

}

您可能想仔细看看 the Interfaces trailHow to Use Buttons, Check Boxes, and Radio Buttons

关于java - JRadiobutton 结合 JButtons 实现。错误方法不是抽象的并且不会覆盖抽象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25117214/

相关文章:

java - 使用 JButtons 编码数字用户输入的最佳方法?

java - 如何使 JToolbar 中的按钮默认选中

java - Iterable#iterator() 返回一个新的迭代器还是一个现有的迭代器?

java - 在 Magnolia 中使用模型

java - 安卓 SQlite : Insert data into multiple tables with sharing ids

Java Swing - 设置背景和按钮

java - 无法调整 JButton 的大小

Java:Junit4:异常测试不起作用,坚持使用 try-catch block :

java - 用图像替换 JTextField?

java - 为什么我的 JTable 对整数列的排序不正确?