java:classname.java使用未经检查或不安全的操作

时间:2014-01-17 17:00:20

标签: java swing

我有一个java项目,在编译之后我得到2个警告:

java: filepath\filename.java uses unchecked or unsafe operations.
java: Recompile with -Xlint:unchecked for details.

以下是该类的代码:

public class GuiMain extends javax.swing.JFrame {

/**
 * Don't use parameters because of windows 7.
 */
public static final boolean USE_PARAMETERS = false;

/**
 * The refresh rate of the traffic screen capturing.
 */
private static int TRAFFIC_REFRESH_RATE = 0; 

private static final long serialVersionUID = -2417685564943832874L;

private static final String CLIENT_NAME = "Traffic Analysis";

private static final String OUTPUT_FILE = "last_session_results.txt";

private JPanel pMainMenu = null;
private JPanel pInput = null;

private JButton btCreate = null;
private JTextField tfInput = null;
private JTextField tfCoordinates = null;

private JPanel pObserve = null;
private JScrollPane spObserve = null;
private JTable tblObserve = null;
private TrafficObserveModel modelObserve = null;
private JButton btAddObserver = null;
private JButton btDelObserver = null;
private JButton btObserving = null;

private JPanel pPaint = null;
private ImagePanel imagePanel = null;
private JButton btTrafficObserver = null;
private JComboBox cobSections = null;
private JButton btNewSection = null;
private JButton btDelSection = null;
private JButton btUndo = null;
private JButton btSave = null;
private JLabel lTimeInterval=null;//moe
private JTextField tfInputTinter=null;//moe
private JPanel pInputTime=null; //moe
private KeyListener keyListener ; //moe

private boolean observeTrafficView = false;
private boolean currentlyObserving = false;

private String curFileName = "";
private MapSection mapSection = null;
private ScreenCaptureController screenCapturing = null;

private Vector<Section> observeTraffic = new Vector<Section>();
private BufferedImage lineImage = null;

public GuiMain() {
    super();

    try {
        // Create or delete content from the output file.
        BufferedWriter bw = new BufferedWriter(new FileWriter("." + ToolKit.fileSeparator() + OUTPUT_FILE));
        bw.close();

        // Create the "maps" directory if it doesn't exist.
        File mapsFile = new File("maps");

        if(!mapsFile.exists()) {
            if(!mapsFile.mkdir()) {
                System.out.println("Couldn't create maps directory.");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    screenCapturing = new ScreenCaptureController(this);
    screenCapturing.addScreenCaptureListener(new ScreenCaptureController.ScreenCaptureListener() {
        public void capturedScreen() {
            if(currentlyObserving) {
                ImageAnalysis analysis = new ImageAnalysis();
                int traffic[] = analysis.colorShare(screenCapturing.getMapTrafficOnly(), lineImage);

                try {
                    // Write the results to the file.
                    PrintWriter out = new PrintWriter(new BufferedWriter(
                        new FileWriter("." + ToolKit.fileSeparator() + OUTPUT_FILE, true)));
                    out.println(traffic[0] + " - " + traffic[1] + " - " + traffic[2] + " - " + traffic[3]);
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                showImage(screenCapturing.getMapTraffic());
            }
        }
    });

    initMainMenu();

    initGUI();
}

private void initGUI() {
    try {
        BorderLayout thisLayout = new BorderLayout();

        this.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                shutDown(e);
            }
        });

        getContentPane().setLayout(thisLayout);
        {
            pPaint = new JPanel();
            //getContentPane().add(pPaint, BorderLayout.NORTH);

            GridBagLayout pPaintLayout = new GridBagLayout();
            pPaintLayout.columnWidths = new int[] {7, 7, 7, 7, 7};
            pPaintLayout.rowHeights = new int[] {25};
            pPaintLayout.columnWeights = new double[] {100.0, 0.1, 0.1, 0.1, 0.1};
            pPaintLayout.rowWeights = new double[] {0.1};
            pPaint.setLayout(pPaintLayout);
            {
                cobSections = new JComboBox();
                cobSections.setRenderer(new MapSectionItemRenderer());
                pPaint.add(cobSections, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                    new Insets(0, 0, 0, 0), 0, 0));
                cobSections.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        cobSectionsActionPerformed(evt);
                    }
                });
            }
            {

                btUndo = new JButton();
                pPaint.add(btUndo, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.NONE,
                    new Insets(0, 5, 0, 0), 0, 0));
                btUndo.setText("Undo");
                btUndo.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        btUndoActionPerformed(evt);
                    }
                });
            }
            {

                btSave = new JButton();
                pPaint.add(btSave, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, 
                    GridBagConstraints.CENTER, GridBagConstraints.NONE,
                    new Insets(0, 5, 0, 0), 0, 0));
                btSave.setText("Save");
                btSave.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        btSaveActionPerformed(evt);
                    }
                });
            }

            {
                btNewSection = new JButton();
                pPaint.add(btNewSection, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.NONE, 
                    new Insets(0, 10, 0, 0), 0, 0));
                btNewSection.setText("+ Section");
                btNewSection.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        btNewSectionActionPerformed(evt);
                    }
                });
            }

            {
                lTimeInterval = new JLabel("T_interval (ms)");
                lTimeInterval.setVisible(false);
                pPaint.add(lTimeInterval, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.NONE,
                        new Insets(0, 5, 0, 0), 0, 0));
            }
            {
                btDelSection = new JButton();
                pPaint.add(btDelSection, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, 
                    GridBagConstraints.CENTER, GridBagConstraints.NONE, 
                    new Insets(0, 5, 0, 0), 0, 0));
                btDelSection.setText("- Section");
                btDelSection.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        btDelSectionActionPerformed(evt);
                    }
                });
            }
            {

                tfInputTinter = new JTextField(3);
                tfInputTinter.setVisible(false);
                pPaint.add(tfInputTinter, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.NONE,
                        new Insets(0, 5, 0, 0), 0, 0));
            }



            imagePanel = new ImagePanel(mapSection);

            getContentPane().add(pMainMenu, BorderLayout.CENTER);



            btTrafficObserver = new JButton("Traffic Observation");
            btTrafficObserver.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onTrafficObserverClicked(e);
                }
            });

            //getContentPane().add(btTrafficObserver, BorderLayout.SOUTH);


            pObserve = new JPanel();
            BorderLayout pObserveLayout = new BorderLayout();
            pObserve.setLayout(pObserveLayout);
            {
                JPanel pObserverButtons = new JPanel(new BorderLayout());
                {
                    btAddObserver = new JButton("Add");
                    pObserverButtons.add(btAddObserver, BorderLayout.WEST);
                    btAddObserver.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            btAddObserverActionPerformed(evt);
                        }
                    });
                }
                {
                    btDelObserver = new JButton("Remove");
                    pObserverButtons.add(btDelObserver, BorderLayout.EAST);
                    btDelObserver.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            btDelObserverActionPerformed(evt);
                        }
                    });
                }
                pObserve.add(pObserverButtons, BorderLayout.NORTH);

                spObserve = new JScrollPane();
                spObserve.setPreferredSize(new Dimension(150, 100));
                modelObserve = new TrafficObserveModel();
                tblObserve = new JTable(modelObserve);
                tblObserve.setDefaultRenderer(Section.class, new TrafficObserveRenderer());
                tblObserve.getColumnModel().getColumn(0).setPreferredWidth(100);
                tblObserve.setIntercellSpacing(new Dimension(0, 0));
                tblObserve.setRowSelectionAllowed(true);
                tblObserve.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); // moe bese sigle- selection
                spObserve.setViewportView(tblObserve);

                pObserve.add(spObserve, BorderLayout.CENTER);

                btObserving = new JButton("Observe Traffic");
                pObserve.add(btObserving, BorderLayout.SOUTH);
                btObserving.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        btObservingActionPerformed(evt);
                    }
                });
            }

            //getContentPane().add(pObserve, BorderLayout.EAST);
        }

        pack();
        setSize(320, 130);
        setTitle(CLIENT_NAME);
        setLocationRelativeTo(null);
        setVisible(false);
    } catch (Exception e) {
        //add your error handling code here
        e.printStackTrace();
    }
}

private void initMainMenu() {
    try {
        BorderLayout thisLayout = new BorderLayout();

        pMainMenu = new JPanel();

        pMainMenu.setLayout(thisLayout);
        {
            JPanel pButtons = new JPanel();
            pMainMenu.add(pButtons, BorderLayout.NORTH);

            GridBagLayout pButtonsLayout = new GridBagLayout();
            pButtonsLayout.columnWidths = new int[] {7, 7, 7};
            pButtonsLayout.rowHeights = new int[] {25};
            pButtonsLayout.columnWeights = new double[] {0.1, 0.1, 0.1};
            pButtonsLayout.rowWeights = new double[] {0.1};
            pButtons.setLayout(pButtonsLayout);
            {
                JButton btLoad = new JButton();
                pButtons.add(btLoad, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
                    GridBagConstraints.CENTER, GridBagConstraints.NONE, 
                    new Insets(0, 5, 0, 0), 0, 0));
                btLoad.setText("Load");
                btLoad.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        btLoadActionPerformed(evt);
                    }
                });
            }
            {

                JButton btView = new JButton();
                pButtons.add(btView, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 
                    GridBagConstraints.CENTER, GridBagConstraints.NONE, 
                    new Insets(0, 5, 0, 0), 0, 0));
                btView.setText("View");
                btView.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        btViewActionPerformed(evt);
                    }
                });
            }
            {
                JButton btNew = new JButton();
                pButtons.add(btNew, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, 
                    GridBagConstraints.CENTER, GridBagConstraints.NONE, 
                    new Insets(0, 5, 0, 0), 0, 0));
                btNew.setText("New");
                btNew.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        btNewActionPerformed(evt);
                    }
                });
            }


            pInput = new JPanel();
            pInput.setVisible(false);
            pMainMenu.add(pInput, BorderLayout.CENTER);

            GridBagLayout pInputLayout = new GridBagLayout();
            pInputLayout.columnWidths = new int[] {7, 7};
            pInputLayout.rowHeights = new int[] {25};
            pInputLayout.columnWeights = new double[] {0.1, 100.0};
            pInputLayout.rowWeights = new double[] {0.1, 100.0};
            pInput.setLayout(pInputLayout);
            {
                JLabel lName = new JLabel("Name");
                pInput.add(lName, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
                    GridBagConstraints.EAST, GridBagConstraints.NONE, 
                    new Insets(0, 5, 0, 0), 0, 0));
            }
            {

                tfInput = new JTextField();
                pInput.add(tfInput, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 
                    GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 
                    new Insets(0, 5, 0, 0), 0, 0));
            }
            {
                JLabel lCoordinates = new JLabel("Coordinates");
                pInput.add(lCoordinates, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 
                    GridBagConstraints.EAST, GridBagConstraints.NONE, 
                    new Insets(0, 5, 0, 0), 0, 0));
            }
            {

                tfCoordinates = new JTextField();
                pInput.add(tfCoordinates, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, 
                    GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 
                    new Insets(0, 5, 0, 0), 0, 0));
            }


            btCreate = new JButton("Create");
            btCreate.setVisible(false);
            btCreate.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    btCreateActionPerformed(evt);
                }
            });

            pMainMenu.add(btCreate, BorderLayout.SOUTH);
        }
    } catch (Exception e) {
        //add your error handling code here
        e.printStackTrace();
    }
}

public void showImage(BufferedImage curImage) {
    imagePanel.setImage(curImage, mapSection);

    setSize(curImage.getWidth(), curImage.getHeight() + 80);
    setLocationRelativeTo(null);
}

private void showTrafficMap() {
    if(mapSection != null) {
        screenCapturing.setMapSection(mapSection);

        if(screenCapturing.captureBaseMap()) {
            try {
                getContentPane().remove(pMainMenu);
                getContentPane().add(pPaint, BorderLayout.NORTH);
                getContentPane().add(imagePanel, BorderLayout.CENTER);
                getContentPane().add(btTrafficObserver, BorderLayout.SOUTH);

                cobSections.setModel(new DefaultComboBoxModel(mapSection.getModel()));

                if(mapSection.getModel().size() > 0) {
                    cobSections.setSelectedIndex(0);
                }

                validate();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}


private void shutDown(WindowEvent e) {
    if(mapSection != null && curFileName != "") {
        XMLManager.writeFile(curFileName, mapSection);
    }

    System.exit(0);
}

private void btSaveActionPerformed(ActionEvent evt) {
    if(mapSection != null && curFileName != "") {
        XMLManager.writeFile(curFileName, mapSection);
    }
}

private void btLoadActionPerformed(ActionEvent evt) {
    JFileChooser chooser = new JFileChooser(); 

    chooser.setCurrentDirectory(new java.io.File("maps"));
    chooser.setDialogTitle("Select Map");
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

    // disable the "All files" option.
    chooser.setAcceptAllFileFilterUsed(false);
    //    
    if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
        curFileName = chooser.getSelectedFile().toString();

        mapSection = XMLManager.parseFile(curFileName);

        showTrafficMap();
    } else {
        System.out.println("No Selection ");
    }
}

private void btViewActionPerformed(ActionEvent evt) {
    if(USE_PARAMETERS) {
        String htmlFilePath = "html" + ToolKit.fileSeparator() + "trafficPrototype.html";
        File htmlFile = new File(htmlFilePath);

        String[] params = {"coord=(26.370628,-80.104281),11", "view=1"};
        ToolKit.openBrowser(htmlFile.toURI(), params);
    } else {
        String htmlFilePath = "html" + ToolKit.fileSeparator() + "trafficPrototype2.html";
        File htmlFile = new File(htmlFilePath);

        HtmlController.writeParameterScript(26.370628, -80.104281, 11, 1);
        ToolKit.openBrowser(htmlFile.toURI());
    }
}

private void btNewActionPerformed(ActionEvent evt) {
    pInput.setVisible(true);
    btCreate.setVisible(true);
}

private void btCreateActionPerformed(ActionEvent evt) {
    String fileName = tfInput.getText();
    String coords = tfCoordinates.getText();

    String coord = coords.replace("(", "").replace(")", "");
    String[] params = coord.split(",");

    boolean invalidCoord = false;
    double lat = 0.0;
    double lng = 0.0;
    int zoom = 0;

    try {
        lat = Double.parseDouble(params[0].trim());
        lng = Double.parseDouble(params[1].trim());
        zoom = Integer.parseInt(params[2].trim());
    } catch(Exception e) {
        invalidCoord = true;
    }

    if(!invalidCoord) {
        mapSection = new MapSection(fileName, lat, lng, zoom);

        fileName = fileName.replace(" ", "_");

        curFileName = "." + ToolKit.fileSeparator() + "maps" + ToolKit.fileSeparator() + fileName + ".xml";
        XMLManager.writeFile(curFileName, mapSection);

        showTrafficMap();
    } else {
        JOptionPane.showMessageDialog(this, "Invalid Coordinates!", "Coordinates", JOptionPane.ERROR_MESSAGE);
    }
}

private void cobSectionsActionPerformed(ActionEvent evt) {
    imagePanel.setCurrentSection((Section)cobSections.getSelectedItem());

    imagePanel.repaint();
}

private void btUndoActionPerformed(ActionEvent evt) {
    if(cobSections.getSelectedIndex() != -1) {
        Section selectedSection = (Section)cobSections.getSelectedItem();

        if(selectedSection.removeLastAddedItem()) {
            imagePanel.repaint();
        }
    }
}

private void btNewSectionActionPerformed(ActionEvent evt) {
    String newSectionName = "";

    newSectionName = JOptionPane.showInputDialog(this, "Input your new section name.", 
        "New Section", JOptionPane.QUESTION_MESSAGE);

    if(newSectionName != null && !newSectionName.equals("")) {
        Section section = new Section(newSectionName);

        if(mapSection.addSection(section)) {
            cobSections.setSelectedIndex(cobSections.getItemCount() - 1);
        } else {
            JOptionPane.showMessageDialog(this, "Section " + newSectionName + " already exists!", 
                "New Section", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}

private void btDelSectionActionPerformed(ActionEvent evt) {
    if(cobSections.getSelectedIndex() != -1) {
        int selectedOption = JOptionPane.showConfirmDialog(this, "Delete " + 
            ((Section)cobSections.getSelectedItem()).getId() + " section?", 
            "Delete Section", JOptionPane.YES_NO_CANCEL_OPTION);

        if(selectedOption == JOptionPane.YES_OPTION) {
            mapSection.getSections().remove(cobSections.getSelectedIndex());
            cobSections.setSelectedIndex(0);
        }
    }
}

private void btAddObserverActionPerformed(ActionEvent evt) {
    if(cobSections.getSelectedIndex() != -1) {
        Section sectionToAdd = (Section)cobSections.getSelectedItem();
        boolean duplicate = false;

        for(Section section : observeTraffic) {
            if(section.getId().equals(sectionToAdd.getId())) {
                duplicate = true;
            }
        }

        if(!duplicate) {
            observeTraffic.add(sectionToAdd);

            modelObserve.setData(observeTraffic);

            imagePanel.repaint();
        }
    }
}

private void btDelObserverActionPerformed(ActionEvent evt) {
    if(tblObserve.getSelectedRow() != -1) {
        observeTraffic.remove(tblObserve.getSelectedRow());

        modelObserve.setData(observeTraffic);

        imagePanel.repaint();
    }
}

private void btObservingActionPerformed(ActionEvent evt) {

    String timeInterval = tfInputTinter.getText();//moe

    if(modelObserve.getRowCount() > 0) {
        if(currentlyObserving) {
            currentlyObserving = false;

            btObserving.setText("Observe Traffic");

            screenCapturing.stopCapturing();
        } else {
            currentlyObserving = true;

            btObserving.setText("Stop Observing");

            lineImage = imagePanel.getLineImage();

            if(!timeInterval.trim().isEmpty()) //moe
            {
                tfInputTinter.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        //btObserving.setText("Stop Observing");
                    }
                });


                 TRAFFIC_REFRESH_RATE=Integer.parseInt(timeInterval);//moe
                 screenCapturing.captureAutomatically(TRAFFIC_REFRESH_RATE);
            }

        }
    } else {
        JOptionPane.showMessageDialog(this, "There are no sections selected.", 
            "Observe Traffic", JOptionPane.INFORMATION_MESSAGE);
    }


}


private void onTrafficObserverClicked(ActionEvent e) {
    if(observeTrafficView) {
        observeTrafficView = false;

        btTrafficObserver.setText("Traffic Observation");

        btUndo.setVisible(true);
        btSave.setVisible(true);
        btNewSection.setVisible(true);
        btDelSection.setVisible(true);

        lTimeInterval.setVisible(false);
        tfInputTinter.setVisible(false);

        getContentPane().remove(pObserve);
    } else {
        observeTrafficView = true;

        btTrafficObserver.setText("Traffic Manipulation");

        btUndo.setVisible(false);
        btSave.setVisible(false);
        btNewSection.setVisible(false);
        btDelSection.setVisible(false);

        lTimeInterval.setVisible(true);
         tfInputTinter.setVisible(true);



        getContentPane().add(pObserve, BorderLayout.EAST);
    }

    imagePanel.setObserveTraffic(observeTraffic, observeTrafficView);
    validate();
    }
}

有人可以帮我解决这个问题吗?我想创建一个新的jar文件,但我想我应该首先重建项目而不会发出警告。

提前致谢。

2 个答案:

答案 0 :(得分:0)

任何人都可以帮我解决这个问题吗?我想创建一个新的jar文件,但我想我应该首先重建项目而不发出警告。

当然,如果没有错误,您可以使用警告进行编译,但是您必须小心应用程序的逻辑而不是“0警告”。

请参阅此question,它可能会对您有所帮助。

答案 1 :(得分:0)

JComboBox是一种泛型类型,需要一个类型参数来指示JComboBox所呈现的项的类型。您尚未提供此类型参数,因此编译器会发出您看到的警告。在您的情况下,您需要将其声明为JComboBox<Section>,并对DefaultComboBoxModel进行类似的更改,因为它也需要此类型参数。完成后,您还可以删除当您从组合框中获取项目时正在执行的演员表。