Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

JMenuBar

Empfohlene Antworten

Veröffentlicht

Hi zusammen,

Ich bräuchte mal nen Tip - ist ja eigentlich kein Fehler, ich will das Programm nur schöner haben.

Ich hab hier einen kleinen Programmaufbau für ein JMenuBar:

class Magic

extends JFrame

{

	public Magic()

	{

		super("");

		addWindowListener(new WindowAdapter()	{

			public void windowClosing(WindowEvent e)

			{

				dispose();

				System.exit(0);

			}

		});

		setJMenuBar(new MagicMenu(this));


		setSize(700,500);

		show();

	}

}


class MagicMenu

extends JMenuBar

implements ActionListener

{

	private JFrame father;


	public MagicMenu(JFrame father)

	{

		this.father=father;


		JMenu menu=null;

		JMenuItem mi=null;


		JMenu datei=new JMenu("Datei");

		mi=new JMenuItem("Neu");

		mi.addActionListener(this);

		mi.setActionCommand("new");

		datei.add(mi);

		add(datei);

		mi=new JMenuItem("Öffnen");

		mi.addActionListener(this);

		mi.setActionCommand("open");

		datei.add(mi);

		add(datei);

		mi=new JMenuItem("Speichern");

		mi.addActionListener(this);

		mi.setActionCommand("save");

		datei.add(mi);

		add(datei);

		datei.addSeparator();

		mi=new JMenuItem("Beenden");

		mi.addActionListener(this);

		mi.setActionCommand("exit");

		datei.add(mi);

		add(datei);

	}


	public void actionPerformed(ActionEvent e)

	{

		String cmd=e.getActionCommand();

		if(cmd.equals("exit"))	{

			father.dispose();

		}

	}

}

Hier meine Frage:

Ich schleppe ja meinen JFrame in das MagicMenu mit!

Kann ich irgentwie anders in der actionPerformed-Funktion herausfinden, von welchem JFrame die Exit-Anweisung kommt, ohne daß ich den Vaterframe mitschleppen muß?

Wenn ja, wie?

Danke

Servus erstmal :-)

du könntest das ganze auch so machen:

public void progmenu()

{

jmb = new JMenuBar();

jm = new JMenu("Start");

jmi = new JMenuItem("New");

jmi.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

//Aktion

newForm();

}

}

);

jm.add(jmi);

jmi = new JMenuItem("Open");

jmi.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

//Aktion

openMyFile();

}

}

);

jm.add(jmi);

jmi = new JMenuItem("Save");

jmi.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

//Aktion

}

}

);

jm.add(jmi);

jmi = new JMenuItem("Exit");

jmi.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

//Aktion

extApp();

}

}

);

jm.add(jmi);

jmb.add(jm);

jm = new JMenu("Help");

jmi = new JMenuItem("Help");

jmi.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

//Aktion

}

}

);

jm.add(jmi);

jmi = new JMenuItem("About...");

jmi.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

//Aktion

}

}

);

jm.add(jmi);

jmb.add(jm);

}

Um ein Fenster zu schließen, sind die Methoden setVisible(false) und dispose aufzurufen.

  • Autor

Bei mir sind aber doch der JFrame und das JMenu getrennt!

ich will in der Klasse JMenu ohne, daß ich den JFrame an den Konstruktor übergebe wissen, zu welchem JFrame das JMenu später gehört.

Generell: Ist das möglich,oder wird das nur noch umständlicher, als einfach den JFrame zu übergeben?

ich denke das es einfacher wäre wenn du das einfach mal übergibst :-) oder nicht trennst !

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.