编译器警告类使用未经检查或不安全的操作

时间:2015-11-15 01:20:01

标签: java compiler-warnings

我的代码目前工作正常,但每当我编译时,我都会收到警告

C:\Users\etc\etc\FinalProject\AddItem.java uses unchecked or unsafe operations. Recompile with -Xlint:unchecked for details.

我应该为此做些什么,如果是,那该怎么办?如果我的调试是正确的,它似乎是由内容窗格属性引起的。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.io.*;

class AddItem extends JFrame implements ActionListener 
{
    //text
    private static JLabel TechforTeachingTitle;
    private static JLabel TechforTeachingSubTitle;

    //images
    private static JLabel Logo;

    //buttons
    private JButton submitButton;

    private static final int BUTTON_WIDTH = 100;
    private static final int BUTTON_HEIGHT = 30;

    //variables
    public static void main(String[] args) 
    {

        AddItem ProjectFrame = new AddItem();  
        ProjectFrame.setVisible(true); // Display the frame

        //for combobox
        //new AddItem().setVisible(true);
    }

    public AddItem ( ) 
    {
        //font properties
        Font TitleFont = new Font("Serif", Font.BOLD, 80);
        Font subFont = new Font("Serif", Font.BOLD, 40);

        // set the frame properties
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Inventory system");
        setSize(900, 700);
        setLocationRelativeTo(null);
        setResizable(true);

        // set the content pane properties
        Container contentPane = getContentPane();   
        contentPane.setLayout(null);
        contentPane.setBackground(Color.decode("#FDF3E7"));

        //set text properties
        TechforTeachingTitle = new JLabel();     
        TechforTeachingTitle.setText("Tech For Teaching"); 
        TechforTeachingTitle.setBounds(200, 30, 900, 95); 
        TechforTeachingTitle.setForeground(Color.decode("#8f8f8f"));
        TechforTeachingTitle.setFont(TitleFont);
        contentPane.add(TechforTeachingTitle);

        TechforTeachingSubTitle = new JLabel();     
        TechforTeachingSubTitle.setText("Add an item"); 
        TechforTeachingSubTitle.setBounds(400, 90, 900, 95); //left, top, length, height
        TechforTeachingSubTitle.setForeground(Color.decode("#799177"));   //7E8F7C (slightly less green)
        TechforTeachingSubTitle.setFont(subFont);
        contentPane.add(TechforTeachingSubTitle);

        //set image properties
        Logo = new JLabel(new ImageIcon("SmallLogo.png")); 
        Logo.setBounds(10,20,179,178); // //left, top, width, height
        contentPane.add(Logo);
        Logo.setVisible(true);
        Logo.addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent e) {
                    setVisible(false);

                    FinalProject FP = new FinalProject(); 
                    FP.setVisible(true); 
                }
            });
        //set button properties


        //for combobox
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //setLayout(null); //ONLY PROBLEM WITH THIS IS GETTING THE SCREEN TO APPEAR AT CORRECT SIZE

        String[] values = {"Computer", "Monitor", "Keyboard"};
        final JComboBox b = new JComboBox(values);
        b.setEditable(true);

        add(b);

        b.setBounds(300, 300, 100, 50);   //this works
        add(b);

        submitButton = new JButton("Submit");
        submitButton.setBounds(700,600, BUTTON_WIDTH, BUTTON_HEIGHT);
        submitButton.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    String item=b.getSelectedItem().toString();

                }
            });
        contentPane.add(submitButton);

        //pack();  //not quite sure what this is for, but it makes the window tiny, so i commented it out
        setLocationRelativeTo(null);  //positions window center on screen

    }

    public void actionPerformed(ActionEvent event) // Actions
    {
        JButton clickedButton = (JButton) event.getSource();        
        String buttonText = clickedButton.getText();

    }
}

1 个答案:

答案 0 :(得分:1)

我在这里得到警告:

final JComboBox b = new JComboBox(values);

为避免这种情况,您有以下可能性:

在ZComboBox上面添加SupressWarning

    @SuppressWarnings("unchecked")
    final JComboBox b = new JComboBox(values);

或在您的方法之上添加SupressWarning

   @SuppressWarnings("rawtypes")
   public AddItem ( ) 

或我最喜欢的添加通用:

final JComboBox<Object> b = new JComboBox<Object>(values);