通过URL导入图像并将其添加到面板上时出错

时间:2013-03-28 16:13:01

标签: java swing url jlabel imageicon

public class Interface 
{ 

    private static JMenuItem mntmNewMenuItem;
final JPanel panel = new JPanel();
private JTable table;

 public Interface()
 {

   JFrame f= new JFrame("Content Provider Generator");
   f.setFont(new Font("Aharoni", Font.BOLD, 12));
   f.setForeground(Color.GRAY);
   f.getContentPane().setBackground(Color.WHITE);


   //JToolbar//

   JToolBar toolBar = new JToolBar();
      f.getContentPane().add(toolBar, BorderLayout.EAST);

  //MenuButton//

      JButton button = new JButton();
      button.setIcon(new ImageIcon
        (Interface.class.getResource("/javax/swing/plaf/metal/icons/ocean/menu.gif")));
      toolBar.add(button);   //added to JToolbar

      JPopupMenu popupMenu = new JPopupMenu();
      addPopup(button, popupMenu);

      //popupmenu
      mntmNewMenuItem = new JMenuItem("Table");

** //点击POPUP菜单(表格)// * * * * ** * ** * ** * ** * * * * *

 mntmNewMenuItem.addActionListener(new ActionListener() {
            //This method will be called whenever you click the button.
            public void actionPerformed(ActionEvent e) {
              JLabel label = new JLabel();
            try {
label.setIcon(new ImageIcon(new URL
                     ("file:/C:/Users/Ashad/JunoWorkspace/FYP1/table.png")));
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }   

                panel.add(label); 

            }
        });

** * ** * ** * 的** * ** * ** // ** * ** * ** * ** * ** * ** * ** * ** * **

      popupMenu.add(mntmNewMenuItem);


       //Image Import on click to panel

       mntmNewMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, 
                                                              InputEvent.CTRL_MASK));
       mntmNewMenuItem.setIcon(newImageIcon(Interface.class.getResource
            ("/javax/swing/plaf/metal/icons/ocean/menu.gif")));


      //////////////////MenuBar/////////////////////////   
      JMenuBar menuBar = new JMenuBar();
      f.getContentPane().add(menuBar, BorderLayout.NORTH);



      //Menus added/initialized in MENUBAR//


      JMenu mnNewMenu = new JMenu("File");
      menuBar.add(mnNewMenu);

      JMenuItem mntmNew = new JMenuItem("New");  //mntmNew
      mntmNew.setIcon(new   ImageIcon(Interface.class.getResource
                              ("/javax/swing/plaf/metal/icons/ocean/file.gif")));
          mnNewMenu.add(mntmNew);

      JMenuItem mntmSaveAs = new JMenuItem("Save As...");
      mntmSaveAs.setIcon(new ImageIcon(Interface.class.getResource
                               ("/javax/swing/plaf/metal/icons/ocean/floppy.gif")));
      mnNewMenu.add(mntmSaveAs);

      JMenuItem mntmSave = new JMenuItem("Save");
      mntmSave.setIcon(new ImageIcon
      (Interface.class.getResource("/javax/swing/plaf/metal/icons/ocean/floppy.gif")));
      mnNewMenu.add(mntmSave);

      //M_Exit
      JMenuItem mntmExit = new JMenuItem("Exit");


      //When exit of the menuBar  is clicked//
      mntmExit.addActionListener(new ActionListener() 
      {
        public void actionPerformed(ActionEvent e) {System.exit(0);}
      });


      mnNewMenu.add(mntmExit);



      JMenu mnEdit = new JMenu("Edit");
      menuBar.add(mnEdit);

      JMenuItem mntmUndo = new JMenuItem("Undo");
      mnEdit.add(mntmUndo);

      JMenuItem mntmRedo = new JMenuItem("Redo");
      mnEdit.add(mntmRedo);

      JMenuItem mntmCut = new JMenuItem("Cut");
      mnEdit.add(mntmCut);

      JMenuItem mntmCopy = new JMenuItem("Copy");
      mnEdit.add(mntmCopy);

      JMenuItem mntmDelete = new JMenuItem("Delete");
      mnEdit.add(mntmDelete);

      JMenu mnSearch = new JMenu("Search");
      menuBar.add(mnSearch);

      JMenuItem mntmSearch = new JMenuItem("Search");
      mnSearch.add(mntmSearch);

      JMenuItem mntmFile = new JMenuItem("File");
      mnSearch.add(mntmFile);

      JMenuItem mntmJava = new JMenuItem("Java");
      mnSearch.add(mntmJava);

      JMenu mnHelp = new JMenu("Help");
      mnHelp.setFont(UIManager.getFont("OptionPane.messageFont"));
      menuBar.add(mnHelp);

      JMenuItem mntmWelcome = new JMenuItem("Welcome");
      mnHelp.add(mntmWelcome);

      JMenuItem mntmHelpContents = new JMenuItem("Help Contents");
      mnHelp.add(mntmHelpContents);

      //Panel (Drawing board)//  panel
      panel.setBackground(Color.WHITE);



      f.getContentPane().add(panel, BorderLayout.CENTER);

      Label label = new Label();
      label.setForeground(Color.WHITE);
      panel.add(label);

      table = new JTable();


      table.setFillsViewportHeight(true);
      table.setVisible(false);
      table.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
      table.setAutoscrolls(false);
      table.setAlignmentY(2.0f);
      panel.add(table);
      table.setModel(new DefaultTableModel(
        new Object[][] {
            {null},
            {null},
            {null},
            {null},
        },
        new String[] {
            "Table"
        }
      ));
      table.getColumnModel().getColumn(0).setResizable(false);
      table.getColumnModel().getColumn(0).setPreferredWidth(145);
      f.setVisible(true);   //set visible  
      f.setSize(600,400);    //set size
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      f.setLocationRelativeTo(null);


 }

 public void addtoPanel(Component c)
 {
   panel.add(c);
 }

 public static void main(String[] args)

{
    new Interface();


}

private static void addPopup(Component component, final JPopupMenu popup) {
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }
        private void showMenu(MouseEvent e) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    });
}

}

看看明星之间的代码部分!!那部分工作不正常。因为它没有在面板上添加任何图像

1 个答案:

答案 0 :(得分:2)

如果我对问题的解释是正确的,您可以使用接受ImageIcon的{​​{1}}构造函数,即:

URL

另请注意,在向容器添加控件后,需要重新验证容器,即调用label.setIcon(new ImageIcon(new URL( "http://duke.kenai.com/iconSized/duke.gif"))); validate();

编辑:

对于本地文件,有一个带有文件名的构造函数,即:

repaint();

如果需要,您还可以使用网址:

label.setIcon(new ImageIcon("C:/Users/User/Desktop/1.png"));

你也可以使用反斜杠,只是逃避它们,即:File file = new File("C:/Users/User/Desktop/1.png"); label.setIcon(new ImageIcon(file.toURI().toURL()));

另一种创建本地文件URL的方法:

C:\\Users\\User\\Desktop\\1.png

修改

尝试使用此简单示例来显示图像:

label.setIcon(new ImageIcon(new URL("file:/C:/Users/User/Desktop/1.png")));