简体   繁体   English

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

[英]Compiler Warning class uses unchecked or unsafe operations

My code is currently working fine, but whenever i compile, i get the Warning 我的代码当前工作正常,但是每当我编译时,我都会收到警告

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

Should I do something about it, and if so, what? 我应该为此做些什么吗? if my debugging is right, it appears to be caused by the content pane properties. 如果我的调试正确,则似乎是由内容窗格的属性引起的。

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

    }
}

you get the warning here I think: 您在这里得到警告,我认为:

final JComboBox b = new JComboBox(values);

to avoid this you have the following possibilities: 为避免这种情况,您有以下几种可能性:

add SupressWarning above the JComboBox 在JComboBox上方添加SupressWarning

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

or add SupressWarning above your method 或在您的方法上方添加SupressWarning

   @SuppressWarnings("rawtypes")
   public AddItem ( ) 

or my favourite add the generic: 或我最喜欢的添加泛型:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM