📄 windowutilities.java
字号:
package com.component;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXStatusBar;
public class WindowUtilities {
protected static Point frameLocation = new Point(0,0);
/** Tell system to use native look and feel.
* Metal (Java) LAF is the default otherwise.
*/
public static void setNativeLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
System.out.println("Error setting native LAF: " + e);
}
}
public static void setJavaLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {
System.out.println("Error setting Java LAF: " + e);
}
}
public static void setMotifLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch(Exception e) {
System.out.println("Error setting Motif LAF: " + e);
}
}
/** A simplified way to see a JPanel or other Container.
* Pops up a JFrame with specified Container as the content pane.
*/
public static JFrame openInJFrame(Container content,
int width,
int height,
String title,
Color bgColor) {
JFrame frame = new JFrame(title);
frame.setBackground(bgColor);
content.setBackground(bgColor);
frame.setSize(width, height);
frame.setContentPane(content);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
return(frame);
}
/** Uses Color.white as the background color. */
public static JFrame openInJFrame(Container content,
int width,
int height,
String title) {
return(openInJFrame(content, width, height, title, Color.white));
}
/** Uses Color.white as the background color, and the
* name of the Container's class as the JFrame title.
*/
public static JFrame openInJFrame(Container content,
int width,
int height) {
return(openInJFrame(content, width, height,
content.getClass().getName(),
Color.white));
}
// ******************* JXFrame *********************
/**
* Creates and returns a JXFrame with the specified title, containing
* the component wrapped into a JScrollPane.
*
* @param component the JComponent to wrap
* @param title the title to show in the frame
* @return a configured, packed and located JXFrame.
*/
public static JXFrame wrapWithScrollingInFrame(JComponent component, String title) {
JScrollPane scroller = new JScrollPane(component);
return wrapInFrame(scroller, title);
}
/**
* Creates and returns a JXFrame with the specified title, containing
* two components individually wrapped into a JScrollPane.
*
* @param leftComp the left JComponent to wrap
* @param rightComp the right JComponent to wrap
* @param title the title to show in the frame
* @return a configured, packed and located JXFrame
*/
public static JXFrame wrapWithScrollingInFrame(JComponent leftComp, JComponent rightComp, String title) {
JComponent comp = Box.createHorizontalBox();
comp.add(new JScrollPane(leftComp));
comp.add(new JScrollPane(rightComp));
JXFrame frame = wrapInFrame(comp, title);
return frame;
}
/**
* Creates and returns a JXFrame with the specified title, containing
* the component.
*
* @param component the JComponent to wrap
* @param title the title to show in the frame
* @return a configured, packed and located JXFrame.
*/
public static JXFrame wrapInFrame(JComponent component, String title) {
JXFrame frame = new JXFrame(title, false);
JToolBar toolbar = new JToolBar();
frame.getRootPaneExt().setToolBar(toolbar);
frame.getContentPane().add(BorderLayout.CENTER, component);
// frame.getContentPane().add(BorderLayout.NORTH, toolbar);
frame.pack();
frame.setLocation(frameLocation);
if (frameLocation.x == 0) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle(title);
}
frameLocation.x += 30;
frameLocation.y += 30;
return frame;
}
/**
* Creates, shows and returns a JXFrame with the specified title, containing
* the component wrapped into a JScrollPane.
*
* @param component the JComponent to wrap
* @param title the title to show in the frame
* @return a configured, packed and located JXFrame.
* @see #wrapWithScrollingInFrame(JComponent, String)
*/
public static JXFrame showWithScrollingInFrame(JComponent component, String title) {
JXFrame frame = wrapWithScrollingInFrame(component, title);
frame.setVisible(true);
return frame;
}
/**
* Creates and returns a JXFrame with the specified title, containing
* two components individually wrapped into a JScrollPane.
*
* @param leftComp the left JComponent to wrap
* @param rightComp the right JComponent to wrap
* @param title the title to show in the frame
* @return a configured, packed and located JXFrame
*/
public static JXFrame showWithScrollingInFrame(JComponent leftComp, JComponent rightComp, String title) {
JXFrame frame = wrapWithScrollingInFrame(leftComp, rightComp, title);
frame.setVisible(true);
return frame;
}
/**
* Creates, shows and returns a JXFrame with the specified title, containing
* the component.
*
* @param component the JComponent to wrap
* @param title the title to show in the frame
* @return a configured, packed and located JXFrame.
*/
public static JXFrame showInFrame(JComponent component, String title) {
JXFrame frame = wrapInFrame(component, title);
frame.setVisible(true);
return frame;
}
public static void addAction(JXFrame frame, Action action) {
JToolBar toolbar = frame.getRootPaneExt().getToolBar();
if (toolbar != null) {
AbstractButton button = toolbar.add(action);
button.setFocusable(false);
}
}
public static void addMessage(JXFrame frame, String message) {
JXStatusBar statusBar = getStatusBar(frame);
statusBar.add(new JLabel(message), JXStatusBar.Constraint.ResizeBehavior.FILL);
}
/**
* Returns the <code>JXFrame</code>'s status bar. Lazily creates and
* sets an instance if necessary.
* @param frame the target frame
* @return the frame's statusbar
*/
public static JXStatusBar getStatusBar(JXFrame frame) {
JXStatusBar statusBar = frame.getRootPaneExt().getStatusBar();
if (statusBar == null) {
statusBar = new JXStatusBar();
frame.getRootPaneExt().setStatusBar(statusBar);
}
return statusBar;
}
public static void centerFrame(JFrame frame)
{
// Center frame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = frame.getSize();
screenSize.height = screenSize.height/2;
screenSize.width = screenSize.width/2;
size.height = size.height/2;
size.width = size.width/2;
int y = screenSize.height - size.height;
int x = screenSize.width - size.width;
frame.setLocation(x, y);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -