Java: AWT-App.

Also hab ne AWT geschrieben, ist soweit fertig wenn es da nicht noch ein kleines Problem gebe. Habe auch schon auf etlichen Internetseiten gestöbert und gesucht. Bin oft immer auf die selben Quelltexten gekommen die mir aber nicht geholfen haben.

Im grunde genommen geht es um JToolBar und AbstractAction. Wenn man auf ein Element der JToolBar klickt wird der Wert null an AbstractAction gesendet, statt einem Schlüsselwort. Vieleicht findet ja jemand den Fehler. Dieser hält mich nämlich schon den ganzen Abend und die Halbe Nacht schon hin.

/////////////
/// EDIT

Hab das Problem gelößt, war doch einfacher als ich dachte.
Einfach in der Abgeleiteten AbstractAction, anstatt der "event.getActionCommand()" - die "getValue(NAME)" - Methode nutzen.

Wen es halt interessieren sollte :)

/////////////

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class MDI_APP extends JFrame implements ActionListener {

	private static final long serialVersionUID = 4341260073660204771L;

	public static void main(String[] args) {
	
		MDI_APP frame = new MDI_APP(500,400);
	    frame.setVisible(true);
	}

	JPanel statusleiste;
	JDesktopPane desktop;
	JInternalFrame frame;
	JToolBar toolBar;
	JLabel statusL;
	
	/** Erstellung der Actions **/
	SampleAction beendenAction = new SampleAction("Beenden", new ImageIcon(
			"iconExit.gif"), "Programm beenden", 'B');
	SampleAction aboutAction = new SampleAction("About", new ImageIcon(
			"iconAbout.gif"), "About anschauen", 'A');
	
	public MDI_APP( int x, int y ) {
		
		super(" My MDI Application");
		
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		setResizable(false);
		setVisible( true );
		setSize( x, y );
		 
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation( ( d.width  - getSize().width ) / 2,
	                 ( d.height - getSize().height) / 2 );
		
		/** Erstellung der Menübar **/
		JMenuBar menubar = new JMenuBar();

		/** Menü anlegen **/
		JMenu menu1 = new JMenu("Datei");	menubar.add(menu1);  menu1.add(beendenAction);
		JMenu menu2 = createViewMenu(); 	menubar.add(menu2);
		JMenu menu3 = new JMenu("Extras");	menubar.add(menu3); menu3.add(aboutAction);

		/** Menü inc. Menübar initalisieren **/
		setJMenuBar(menubar);

		/** Neues Panel (CENTER) **/
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createEtchedBorder());
		
		/** Panel für Toolbar erstellen **/
		JPanel toolBarPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		toolBarPanel.setBorder(BorderFactory.createEtchedBorder());
		getContentPane().add(toolBarPanel, BorderLayout.NORTH);
		
		/** Toolbar erstellen **/
		toolBarPanel.add(createToolBar());
		
		// CENTER
		JDesktopPane desktop = new JDesktopPane();
		desktop.setBorder(BorderFactory.createEtchedBorder());
		getContentPane().add(desktop, BorderLayout.CENTER);
		JLabel aboutName = new JLabel("Created by minzel");

		frame = new JInternalFrame( "About", false, false, false, false );
		frame.setLayout(new FlowLayout(FlowLayout.CENTER));
		frame.setLocation(100,70);
		frame.setSize( 300, 60 );
		frame.add(aboutName);
		frame.setVisible(false);
		desktop.add(frame);
		
		// SOUTH
		statusleiste = new JPanel(new FlowLayout(FlowLayout.LEFT));
		statusleiste.setBorder(BorderFactory.createEtchedBorder());
		getContentPane().add(statusleiste, BorderLayout.SOUTH);
		
		statusL = new JLabel(" Status: Bereit",
				new ImageIcon("duke.gif"),
				SwingConstants.LEFT);
		statusL.setFont( new Font("Arial", Font.CENTER_BASELINE, 10));
		statusleiste.add(statusL);
	}
	
	private JToolBar createToolBar() {
		
		JToolBar bufferToolBar = new JToolBar("Die Toolbar");

		bufferToolBar.add(beendenAction);
		bufferToolBar.add(aboutAction);

		bufferToolBar.setBorder(new EtchedBorder());
		bufferToolBar.setFloatable(true);
		bufferToolBar.setRollover(true);
		
		return bufferToolBar;
	}
	
	private JMenu createViewMenu( ) {
		
		JMenu ret = new JMenu("Ansicht");
		ret.setMnemonic('A');
		JMenuItem mi;

		// Windows
		mi = new JMenuItem("Windows", 'i');
		setCtrlAccelerator(mi, 'i');
		mi.addActionListener(this);
		ret.add(mi);
		
		// Motif
		mi = new JMenuItem("Motif", 'o');
		setCtrlAccelerator(mi, 'o');
		mi.addActionListener(this);
		ret.add(mi);
		
		// Java/Metal
		mi = new JMenuItem("Java/Metal", 'a');
		setCtrlAccelerator(mi, 'a');
		mi.addActionListener(this);
		ret.add(mi);
		
		// Separator
		ret.addSeparator();
		mi = new JCheckBoxMenuItem("Statuszeile",true);
		mi.addActionListener(this);
		ret.add(mi);
		
		return ret;		
	}
	
	private void setCtrlAccelerator(JMenuItem mi, char acc) {
		KeyStroke ks = KeyStroke.getKeyStroke(acc, Event.CTRL_MASK);
		mi.setAccelerator(ks);
	}
	
	private void setLookAndFeel( String view ) {
		try {
			UIManager.setLookAndFeel(view);
			SwingUtilities.updateComponentTreeUI(this);
		} catch (UnsupportedLookAndFeelException e) {
			System.err.println(e.toString());
		} catch (ClassNotFoundException e) {
		    System.err.println(e.toString());
		} catch (InstantiationException e) {
		    System.err.println(e.toString());
		} catch (IllegalAccessException e) {
		    System.err.println(e.toString());
		}		
	}

	public void actionPerformed(ActionEvent event) {

		System.out.println(event.getActionCommand());
		
		// Ansicht-Menü
		if ( event.getActionCommand() == "Windows" ) {
			setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			statusL.setText(" Status: Windows - LookAndFeel aktiviert...");
		}
		
		if ( event.getActionCommand() == "Motif" ) {
			setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
			statusL.setText(" Status: Motif - LookAndFeel aktiviert...");
		}
		
		if ( event.getActionCommand() == "Java/Metal" ) {
			setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
			statusL.setText(" Status: Java/Metal - LookAndFeel aktiviert...");
		}
		
		if ( event.getActionCommand() == "Statuszeile" ) {
			if ( statusleiste.isVisible() == true ) {
				statusleiste.setVisible(false);
				statusL.setText(" Status: Statusleiste - AUS");
			} else {
				statusleiste.setVisible(true);
				statusL.setText(" Status: Statusleiste - EIN");
			}
		}
	}
	
	class SampleAction extends AbstractAction {
		
		private static final long serialVersionUID = 4341260073660204771L;

	    public SampleAction(String text, Icon icon, String description,
	    		char accelerator) {
	    	
	    	super(text, icon);
	        putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(accelerator,
	        		Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
	        putValue(SHORT_DESCRIPTION, description);
	    }

        public void actionPerformed(ActionEvent event) {
              
        	// System.out.print(event.getActionCommand());
        	
        	// Datei-Menü
    		if ( event.getActionCommand() == "Beenden" ) {
    			
    			// Angepasster JOptionPane-Dialog
    			String option[] = { "Ja", "Nein" };
    			
    			int n = JOptionPane.showOptionDialog( null,
    					"Programm wirklich beenden?",	// Fragetext
    					"Beenden",						// Titel
    					JOptionPane.YES_NO_OPTION,
    					JOptionPane.QUESTION_MESSAGE,	// Icon
    					null, option,option[0]);
    			
    			if ( n == JOptionPane.YES_OPTION )
    				System.exit(0);
    		}
    		
    		// Extras - Menü
    		if ( event.getActionCommand() == "About" ) {
    			if ( frame.isVisible() == true ) {
    				frame.setVisible(false);
    				statusL.setText(" Status: About - AUS");
    			} else {
    				frame.setVisible(true);
    				statusL.setText(" Status: About - EIN");
    			}
    		}
        }
	}
}
 
Zurück
Oben