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.

Swing und AbstractAction

Empfohlene Antworten

Veröffentlicht

Hallo zusammen,

Ich habe folgendes Beispiel aus einem Buch:


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;


public class ActionExample extends JPanel

{

	public JMenuBar menuBar;

	public JToolBar toolBar;


	public ActionExample()

	{

		super(true);


		menuBar = new JMenuBar();

		menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));


		JMenu menu = new JMenu("Menü");

		menuBar.add(menu);


		toolBar = new JToolBar();

		toolBar.setBorder(new EtchedBorder());


		SampleAction exampleAction = new SampleAction

("Download",new ImageIcon("download.gif"));


		menu.add(exampleAction);

		toolBar.add(exampleAction);

	}


	class SampleAction extends AbstractAction

	{

		public SampleAction(String text, Icon icon)

		{

			super(text,icon);

		}


		public void actionPerformed(ActionEvent e)

		{

			System.out.println("Action ["+e.getActionCommand()+"]");

		}

	}


	public static void main(String[] args)

	{

		ActionExample exam = new ActionExample();


		JFrame frame = new JFrame("Action Example");

		frame.addWindowListener(new BasicWindowMonitor());

		frame.setJMenuBar(exam.menuBar);

		frame.getContentPane().add(exam.toolBar,BorderLayout.NORTH);

		frame.setSize(200,200);

		frame.setVisible(true);

	}

}

Das Programm soll mir als Ausgabe beim Drücken des Menüs "Download" oder

auf dem Knopf download in der Toolbar immer die selbe ausgabe bringen, nämlich:

Action [Download]

Auf dem Menüpunkt gibt er die Zeile richtig aus - aber auf der Toolbar gibt er

folgendes aus:

Action [null]

Kann mir einer sagen wo mein Fehler liegt?

hi,

imho sollte man die action an einen button binden, nicht an die toolbar, also in etwa so:

toolBar.add(new JButton(exampleAction));
und nicht
toolBar.add(exampleAction);

damit sollte in der toolbar ein button mit der action exampleAction erscheinen.

hth...

hi,

zum thema JToolbar.add(Action a):

As of 1.3, this is no longer the preferred method for adding Actions to a container. Instead it is recommended to configure a control with an action using using setAction, and then add that control directly to the Container

quelle: http://java.sun.com/j2se/1.4.1/docs/api/

evtl ist das buch etwas älter??

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.