Java投票程序错误

时间:2014-12-30 11:26:32

标签: java swing

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

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 个答案:

答案 0 :(得分:6)

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

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));