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());
}
});
}
}
看看星星之间的代码部分!那部分工作不正常。因为它没有在点击时将任何图像添加到面板