Java: Swing: Unterschied zwischen den Versionen
Flinh1 (Diskussion | Beiträge) |
Flinh1 (Diskussion | Beiträge) (→Tipps und Tricks) |
||
| Zeile 41: | Zeile 41: | ||
== Tipps und Tricks == | == Tipps und Tricks == | ||
| − | === | + | === Frame zentrieren === |
| − | > | + | <nowiki> |
| + | import java.awt.*; | ||
| − | + | /** | |
| + | * Utility-Klasse für grafische Oberflächen. | ||
| + | * | ||
| + | * @author fmk | ||
| + | */ | ||
| + | public class SwingTools { | ||
| − | + | private SwingTools() { | |
| − | + | } | |
| − | + | /** | |
| − | + | * Zentriert den referenzierten Frame auf dem Bildschirm. | |
| − | + | * | |
| + | * @param frame | ||
| + | */ | ||
| + | public static void centerFrame(Window frame) { | ||
| + | Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); | ||
| + | int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2); | ||
| + | int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2); | ||
| + | frame.setLocation(x, y); | ||
| + | } | ||
| − | + | public static Point getCenterOnScreen(int screen, Dimension dimension) { | |
| + | GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); | ||
| + | GraphicsDevice[] gs = ge.getScreenDevices(); | ||
| + | GraphicsDevice gd = gs[screen]; | ||
| + | GraphicsConfiguration[] gc = gd.getConfigurations(); | ||
| + | Rectangle screenBounds = gc[0].getBounds(); | ||
| − | + | Point pos = new Point(); | |
| − | + | pos.x = screenBounds.x + (screenBounds.width / 2) - (dimension.width / 2); | |
| − | + | pos.y = screenBounds.y + (screenBounds.height / 2) - (dimension.height / 2); | |
| − | + | ||
| − | + | return pos; | |
| − | + | } | |
| − | + | ||
| + | public static void centerOnScreen(int screen, Window window) { | ||
| + | window.setLocation(SwingTools.getCenterOnScreen(screen, window.getSize())); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </nowiki> | ||
| + | [Dank an fmk, javafish.de] | ||
---- | ---- | ||
Version vom 3. September 2019, 07:17 Uhr
Inhaltsverzeichnis
Allgemeines
Die Infos auf dieser Seite beziehen sich auf den Einsatz von Netbeans in der Java-Programmierung.
Netbeans GUI Builder
Aufruf einer Klasse über > New > (Swing GUI Forms >) JFrame Form
Abschalten der "vollqualifizierten Klassennamen":
> Tools > Options > Java > GUI Builder >
Dann bei Generate Fully Qualified Names of Classes den Haken wegnehmen.
Umbenennen der Swing Controls im Reiter Design:
> Navigation, F2
Tipps und Tricks
Frame zentrieren
import java.awt.*;
/**
* Utility-Klasse für grafische Oberflächen.
*
* @author fmk
*/
public class SwingTools {
private SwingTools() {
}
/**
* Zentriert den referenzierten Frame auf dem Bildschirm.
*
* @param frame
*/
public static void centerFrame(Window frame) {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
}
public static Point getCenterOnScreen(int screen, Dimension dimension) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
GraphicsDevice gd = gs[screen];
GraphicsConfiguration[] gc = gd.getConfigurations();
Rectangle screenBounds = gc[0].getBounds();
Point pos = new Point();
pos.x = screenBounds.x + (screenBounds.width / 2) - (dimension.width / 2);
pos.y = screenBounds.y + (screenBounds.height / 2) - (dimension.height / 2);
return pos;
}
public static void centerOnScreen(int screen, Window window) {
window.setLocation(SwingTools.getCenterOnScreen(screen, window.getSize()));
}
}
[Dank an fmk, javafish.de]
Quellen
| |