0
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");

** //当弹出菜单(表格)被点击// * ** * ** * ** * ** * ** * ** * *

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

}

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

4

1 回答 1

2

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

label.setIcon(new ImageIcon(new URL(
        "http://duke.kenai.com/iconSized/duke.gif")));

另请注意,在向容器添加控件后,需要重新验证容器,即调用validate();and repaint();

编辑:

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

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

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

File file = new File("C:/Users/User/Desktop/1.png");
label.setIcon(new ImageIcon(file.toURI().toURL()));

您也可以使用反斜杠,只是将它们转义,即:C:\\Users\\User\\Desktop\\1.png

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

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

编辑

试试这个简单的例子来显示你的图像:

import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class DisplayImage {
    public static void main(String[] args) {
        Image image;
        try {
            image = ImageIO.read(new File("C:/Users/User/Desktop/1.png"));
            JOptionPane.showMessageDialog(null, new ImageIcon(image));
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Failed to load image");
        }
    }
}
于 2013-03-28T16:41:48.603 回答