2015-11-15 57 views
0

我的代碼,目前工作正常,但每當我編譯,我得到的警告編譯器警告類使用未經檢查或不安全的操作

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

    } 
} 
+2

你重新編譯與-Xlint:沒有選上,它建議?什麼是輸出? – EkcenierK

+0

一個人怎麼做?我不知道如何 – Blaine

+1

您是否正在使用IDE?檢查有關如何添加編譯器參數的文檔。如果使用帶有javac的命令行,只需添加-Xlint:不選中該命令。谷歌會幫助你。 – EkcenierK

回答

1

你在這裏得到警告,我認爲:

final JComboBox b = new JComboBox(values); 

避免這種情況,你有以下幾種可能:

添加SupressWarning JComboBox時上述

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

或添加SupressWarning在你的方法之上

@SuppressWarnings("rawtypes") 
    public AddItem () 

或我的最愛通用:

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

果然,這是問題線。謝謝! – Blaine

相關問題