有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing Java投票程序错误

我写了一段代码告诉你是否有资格投票。。但当我编译它时,它会显示一个错误或警告 消息说:

Vote.java uses unsafe or unchecked operations. Recompile with -Xlint:unchecked for details

这是我的密码

我做了更多的研究,但没有帮助我。。。 现在我无法编译它

`

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Vote extends JFrame {
    JLabel ageEnquiry, result;
    JComboBox<String> ageList;
    JTextField results;
    JButton val;

    public Vote() {

        String[] ages = new String[] {"10-17", "18-30", "31-40", "41-50", "51-60", "61-70", "71-80", "81-90", "91-100"};

        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        ageEnquiry = new JLabel("Select your age range: ");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        add(ageEnquiry, c);

        ageList = new JComboBox<>(ages);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 5;
        c.gridy = 0;
        add(ageList, c);

        result = new JLabel("Result: ");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        add(result, c);

        results = new JTextField(10);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 5;
        c.gridy = 2;
        add(results, c);

        ageList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JComboBox<String> combo = (JComboBox<String>) event.getSource();
                String selectedAge = (String) combo.getSelectedItem();

                if(selectedAge.equals("10-17")) {
                    results.setText("Not Eligible");
                } else {
                    results.setText("Eligible");
                }
            }
        });
    }


    public static void main(String[] args) {
        Vote gui = new Vote();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.setSize(400, 400);
        gui.setTitle("Vote");
    }
}

`

请帮帮我(


共 (1) 个答案

  1. # 1 楼答案

    只需通过以下方式编译代码:

    javac -Xlint:unchecked Vote.java
    

    您将获得有关收到的警告的更详细信息。警告是因为这条线而出现的

    JComboBox<String> combo = (JComboBox<String>) event.getSource();
    

    要消除警告,请参阅以下方法:Handle generics in ActionListener

    然后,您必须按如下方式编辑代码:

    • 创建自定义ActionListener
    • 将此ActionListener用于JComboBox

    ActionListener

    import javax.swing.*;
    import java.awt.event.*;
    
    class CustomActionListener implements ActionListener
    {
      private JComboBox<String> comboBox;
      private JTextField textField;
      public CustomActionListener(JComboBox<String> comboBox, JTextField textField){
        this.comboBox = comboBox;
        this.textField = textField;
      }
      @Override
      public void actionPerformed(ActionEvent event)
      {
        // Just use the comboBox
        ComboBoxModel<String> model = comboBox.getModel();
        int index = comboBox.getSelectedIndex();
        String selectedAge = model.getElementAt(index);
                    if(selectedAge.equals("10-17")) {
                        textField.setText("Not Eligible");
                    } else {
                        textField.setText("Eligible");
                    }
      }
    }
    

    更改投票:

    ageList.addActionListener(new CustomActionListener(ageList,results));