Java - AWT/Swing

Tutorial/Einfürung in Java-Swing - Teil 3 - JToolBar

javax.swing.*

package com.sowas.javawiki.swingtutorial;
 
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
 
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
 
public class SwingTutorial extends JFrame {
   public SwingTutorial() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // Damit beendet der Exit-Button die Anwendung tatsächlich
      setSize(600, 400);
      setLocationRelativeTo(null);  // Auf dem Bildschirm zentrieren
 
      Container cp = getContentPane();
      cp.setLayout(new BorderLayout());
 
      cp.add(createToolBar(), BorderLayout.NORTH);
      JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JLabel("Tree"), new JLabel("Table"));
      cp.add(sp, BorderLayout.CENTER);
   }
 
   private JToolBar createToolBar() {
   	JToolBar tb = new JToolBar();
   	tb.add(new ActionExit());
   	return tb;
   }
 
   private static void setNativLookAndFeel() {
   	String nativeLookAndFeel = UIManager.getSystemLookAndFeelClassName();
   	try {
   	   UIManager.setLookAndFeel(nativeLookAndFeel);
   	}catch (Exception e) {
   	   e.printStackTrace();
   	}
   }
 
   public static void main(String[] args) {
   	setNativLookAndFeel();
 
      SwingTutorial swingTutorial = new SwingTutorial();
      swingTutorial.setVisible(true);
    }
}
 
class ActionExit extends AbstractAction {
 
	public ActionExit() {
		super("Beenden");
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		JFrame frame = (JFrame)SwingUtilities.getRoot((Component)e.getSource());
		frame.dispose();
		System.exit(0);
	}
 
}

Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007