Veröffentlicht 17. September 200322 j 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?
17. September 200322 j 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...
17. September 200322 j Autor Aber in dem Buch steht, dass die Toolbar aus der Action automatisch einen Button erzeugt! Und genau das wollte ich ja auch ausprobieren.
17. September 200322 j 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.