English 中文(简体)
How to import an image in menu bar
原标题:

I ve created a simple menu bar and I don t know how to import an image in the free space.

My code is below:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.IOException;

public class MyMenu extends JFrame {

    JMenuBar menubar;
    JMenu file, edit, contact, quit;
    JMenuItem exit, open, search, delete, registration, informations;

    public MyMenu() {
        setLayout(new FlowLayout());
        //___________________________ FILE __________________________________
        menubar = new JMenuBar();
        setJMenuBar(menubar);
        file = new JMenu("Αρχείο");
        menubar.add(file);
        open = new JMenuItem("Άνοιγμα πελατολογίου");
        file.add(open);
        event e1 = new event(); // Compiler Error
        open.addActionListener(e1);
        //__________________________________ EDIT ____________________________
        edit = new JMenu("Ενέργειες");
        menubar.add(edit);
        search = new JMenuItem("Αναζήτηση");
        edit.add(search);
        registration = new JMenuItem("Καταχώρηση");
        edit.add(registration);
        delete = new JMenuItem("Διαγραφή");
        edit.add(delete);
        //_________________________________ CONTACT __________________________
        contact = new JMenu("Επικοινωνία");
        menubar.add(contact);
        informations = new JMenuItem("Πληροφορίες");
        contact.add(informations);
        //___________________________________QUIT_____________________________
        quit = new JMenu("Έξοδος");
        menubar.add(quit);
        exit = new JMenuItem("Έξοδος");
        quit.add(exit);
        event e = new event(); // Compiler Error
        exit.addActionListener(e);
    }

    public class MyEvent implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }

        public void actionPerformed2(ActionEvent e1) {
            final SimpleTableDemo a = new SimpleTableDemo(); // Compiler Error
            javax.swing.SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    a.createAndShowGUI();
                }
            });
        }
    }

    public static void main(String[] args) throws IOException {
        MyMenu gui = new MyMenu();
        gui.getContentPane().add(panel); // Compiler Error
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(2400, 1900);
        gui.setVisible(true);
    }
}

It is related to the class Panel? How can I take advantage of the free space and use an image?

最佳回答

please post here Runnable code without Errors from Java Compilator (marked in you code with // Compiler Error) before any of your request for AddingImage / ImportImage in JMenuBar

required tutorials

1) JMenu, JMenuBar, JMenuItems

2) Laying Out Components Within a Container

3) How to Write an Action Listener

4) How to Use Icons

5) and finally for set Image as JMenuBar BackGround you have to read something about 2D Graphics

6) examples on this forum, here or here

问题回答

A JMenuItem can be constructed in order to display an image.

ImageIcon icon = new ImageIcon("path_to_your_image");
JMenuItem item = new JMenuItem(icon);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签