Samstag, 16. Mai 2009

 

Einfaches GUI-Beispiel ohne GUI-Builder mit Grafik

gui-demo_1_2.jar (Update) ist ein Jar-Archiv mit einem kleinen Beispiel einer einfachen Java-Applikation mit Menüs und einer Zeichenfläche.
Die Menü-Einträge wurden als anonyme Actions (AbstractAction) implementiert. Im (anonymen) Konstruktor werden die Eigenschaften des Menüeintrags definiert. Als Beispiel soll der "Öffnen"-Eintrag dienen:
    Action openAction = new AbstractAction() {
{
putValue(Action.NAME, "Öffnen");
putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY, 1);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control O"));
putValue(Action.SMALL_ICON, smallOpenIcon);
}

public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
// TODO: Action File open
}

}
};

Die Menü-Eigenschaften werden durch die putValue()-Aufrufe gesetzt. Action.NAME setzt den String für das Menü. Welches Zeichen unterstrichen wird, definiert Action.DISPLAYED_MNEMONIC_INDEX_KEY, wo der Index (hier 1 für das 'f') definiert wird. Action.ACCELERATOR_KEY setzt den Shortcut. Mit dem letzten (verwendeten) Wert wird ein Icon gesetzt, welches vorher natürlich geladen werden muss.

Hier das ganze Listing der Klasse WindowWithMenu:

package gui;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

/**
* Applikationsfenster mit Menü.
*
* @author Harald R. Haberstroh (hh)
*
*/
public class WindowWithMenu extends JFrame {
private static final long serialVersionUID = 7874149670026759869L;

private JMenuBar menuBar;

private JMenu fileMenu;

private JMenu editMenu;

private JMenu optionsMenu;

private JMenu helpMenu;

public Action openAction;

public Action newAction;

public Action aboutAction;

public Action exitAction;

public WindowWithMenu() throws HeadlessException {
this("WindowWithMenu");
}

/**
* Ein neues Fenster mit Menü - nur ein Gerüst.
*
* @param title
* Fenstertitel
* @throws HeadlessException
* keine Maus/Bildschirm/Tastatur
*/
public WindowWithMenu(String title) throws HeadlessException {
super(title);
/** ** Öffnen */
final Icon smallOpenIcon = new ImageIcon(WindowWithMenu.class
.getResource("/images/fileopen16x16.png"));
openAction = new AbstractAction() {
{
putValue(Action.NAME, "Öffnen");
putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY, 1);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control O"));
putValue(Action.SMALL_ICON, smallOpenIcon);
}

public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
// TODO: Action File open
}

}
};
/** * Neu */
newAction = new AbstractAction() {
{
putValue(Action.NAME, "Neu");
putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY, 1);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control N"));
}

public void actionPerformed(ActionEvent e) {
System.out.println("New");
}

};
/** * Exit */
exitAction = new AbstractAction() {
{
putValue(Action.NAME, "Beenden");
putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY, 0);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control Q"));
}

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

/** * Über */
aboutAction = new AbstractAction() {
{
putValue(Action.NAME, "Über das Programm");
putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY, 1);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("F1"));
}

public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "(c) 2009, Harald R. Haberstroh",
"Über das Programm", JOptionPane.INFORMATION_MESSAGE);
}
};

reInititalize();
}

/**
* stelle "Anfangswerte" her und erzeuge Menü neu (die Actions sind public und
* daher änderbar).
*/
public void reInititalize() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400);
setLocationRelativeTo(null); // zentrieren

/*
* Menü
*/
menuBar = new JMenuBar();

/*
* Dateimenü
*/
fileMenu = new JMenu("Datei");
fileMenu.setMnemonic('D');
menuBar.add(fileMenu);

/** ** Öffnen */
fileMenu.add(openAction);

fileMenu.add(newAction);
fileMenu.addSeparator();

/** ** Beenden */
fileMenu.add(exitAction);

/*
* Bearbeiten Menü
*/
editMenu = new JMenu("Bearbeiten");
editMenu.setMnemonic('B');
menuBar.add(editMenu);

/*
* Optionen Menü
*/
optionsMenu = new JMenu("Optionen");
optionsMenu.setMnemonic('O');
menuBar.add(optionsMenu);

/*
* Hilfe Menü
*/
helpMenu = new JMenu("Hilfe");
helpMenu.setMnemonic('H');
menuBar.add(helpMenu);
setJMenuBar(menuBar);

helpMenu.add(aboutAction);

}

/**
* @param args
*/
public static void main(String[] args) {
WindowWithMenu win = new WindowWithMenu();
win.setVisible(true);
}

}

Labels: , ,


Kommentare:
Können wir das bei der Matura haben?
 
ja, ich werde so etwas bereitstellen.
 

Kommentar veröffentlichen

Abonnieren Kommentare zum Post [Atom]





<< Startseite

This page is powered by Blogger. Isn't yours?

Abonnieren Posts [Atom]