我正在为程序创建一个自定义窗口,我似乎无法弄清楚如何添加可恢复性。 我想出了可移动性,但似乎似乎无法获得更多的可恢复性,任何帮助都将是惊人的!先感谢您。我试图创建一个比标准JFrame具有更高可定制性的GUI库。 有没有办法让我的窗口在Java中可调整大小?
/** *Code Updated*
* NOTE the frame is meant to be called from another class where the
* contents will be added (buttons, etc.).
*/
package krypto.desktop.guiWindow;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
@SuppressWarnings("unused")
public class KFrame extends JFrame {
private static final long serialVersionUID = 1L;
private static JButton button = null;
private static JCheckBox checkBox = null;
private static JCheckBoxMenuItem checkBoxMenuItem = null;
private static JColorChooser colorChooser = null;
private static JComponent component = null;
private static JDesktopPane desktopPane = null;
private static JDialog dialog = null;
private static JEditorPane editorPane = null;
private static JFileChooser fileChooser = null;
private static JFormattedTextField formattedTextField = null;
private static JInternalFrame internalFrame = null;
private static JLabel label = null;
private static JLayeredPane layeredPane = null;
private static JMenu menu = null;
private static JMenuBar menuBar = null;
private static JMenuItem menuItem = null;
private static JOptionPane optionPane = null;
private static JPanel panel = null;
private static JPasswordField passwordField = null;
private static JPopupMenu popupMenu = null;
private static JProgressBar progressBar = null;
private static JRadioButton radioButton = null;
private static JRadioButtonMenuItem radioButtonMenuItem = null;
private static JRootPane rootPane = null;
private static JScrollBar scrollBar = null;
private static JScrollPane scrollPane = null;
private static JSeparator separator = null;
private static JSlider slider = null;
private static JSpinner spinner = null;
private static JSplitPane splitPane = null;
private static JTabbedPane tabbedPane = null;
private static JTable table = null;
private static JTextArea textArea = null;
private static JTextField textField = null;
private static JTextPane textPane = null;
private static JToggleButton toggleButton = null;
private static JToolBar toolBar = null;
private static JToolTip toolTip = null;
private static JTree tree = null;
private static JViewport viewport = null;
private static JWindow window = null;
private static boolean frameMovable = true;
private static boolean frameVisible = true;
private static boolean frameCenter = false;
private static boolean frameResizeable = true;
private static boolean exitMessage = false;
private static int frameSizeWidth = 400;
private static int frameSizeHeight = 300;
private static int frameMaxSizeWidth = 400;
private static int frameMaxSizeHeight = 300;
private static int frameMinSizeWidth = 400;
private static int frameMinSizeHeight = 300;
private static int frameLocationX = 50;
private static int frameLocationY = 50;
private static String minimizeIcon = "⎼";
private static String maximizeIcon = "▢";
private static String exitIcon = "X";
private static Color minimizeColor = Color.WHITE;
private static Color maximizeColor = Color.WHITE;
private static Color exitColor = Color.RED;
private static Color frameBackgroundColor = Color.DARK_GRAY;
private final javax.swing.JFrame frame = new JFrame();
public KFrame() {
JButton buttonMinimize = createButtonMinimize();
JButton buttonMaximize = createButtonMaximize();
JButton buttonExit = createButtonExit();
//if (frameCenter = true) {
//TODO add Center to Screen
//}
//Exit Button
buttonExit.addActionListener(e -> {
if (exitMessage = false) {
System.exit(0);
} else if (exitMessage = true) {
int confirm = javax.swing.JOptionPane.showOptionDialog(frame,
"Are You Sure to Close Application?", "Exit Confirmation",
javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.QUESTION_MESSAGE,
null, null, null);
if (confirm == javax.swing.JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
//Window Maximize/Normalize
buttonMaximize.addActionListener(e -> {
if (frame.getExtendedState() == JFrame.NORMAL) {
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
} else {
frame.setExtendedState(JFrame.NORMAL);
}
});
//Minimize
buttonMinimize.addActionListener(e -> frame.setState(Frame.ICONIFIED));
javax.swing.JLabel buttonPanel = new JLabel();
buttonPanel.setLayout(new GridLayout(0, 3, 5, 0));
buttonPanel.setPreferredSize(new Dimension(160, 25));
buttonPanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
buttonPanel.add(buttonMinimize);
buttonPanel.add(buttonMaximize);
buttonPanel.add(buttonExit);
GradientPanel titlePanel = new GradientPanel(Color.DARK_GRAY);
if (frameMovable = true) {
FrameDragListener frameDragListener = new FrameDragListener(frame);
titlePanel.addMouseListener(frameDragListener);
titlePanel.addMouseMotionListener(frameDragListener);
}
titlePanel.setLayout(new BorderLayout());
titlePanel.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
titlePanel.add(new JLabel(BorderLayout.WEST));
titlePanel.add(new JLabel(), BorderLayout.CENTER);
titlePanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
titlePanel.add(buttonPanel, BorderLayout.EAST);
javax.swing.JPanel contentPanel = new JPanel();
contentPanel.setBackground(frameBackgroundColor);
try {
contentPanel.add(button);
contentPanel.add(checkBox);
contentPanel.add(checkBoxMenuItem);
contentPanel.add(colorChooser);
contentPanel.add(component);
contentPanel.add(desktopPane);
contentPanel.add(dialog);
contentPanel.add(editorPane);
contentPanel.add(fileChooser);
contentPanel.add(formattedTextField);
contentPanel.add(internalFrame);
contentPanel.add(label);
contentPanel.add(layeredPane);
contentPanel.add(menu);
contentPanel.add(menuBar);
contentPanel.add(menuItem);
contentPanel.add(optionPane);
contentPanel.add(panel);
contentPanel.add(passwordField);
contentPanel.add(popupMenu);
contentPanel.add(progressBar);
contentPanel.add(radioButton);
contentPanel.add(radioButtonMenuItem);
contentPanel.add(scrollBar);
contentPanel.add(scrollPane);
contentPanel.add(separator);
contentPanel.add(slider);
contentPanel.add(spinner);
contentPanel.add(splitPane);
contentPanel.add(tabbedPane);
contentPanel.add(table);
contentPanel.add(textArea);
contentPanel.add(textField);
contentPanel.add(textPane);
contentPanel.add(toggleButton);
contentPanel.add(toolBar);
contentPanel.add(toolTip);
contentPanel.add(tree);
contentPanel.add(viewport);
contentPanel.add(window);
} catch (Exception ignored) {
}
javax.swing.JPanel fatherPanel = new JPanel();
fatherPanel.setLayout(new BorderLayout());
fatherPanel.add(titlePanel, BorderLayout.NORTH);
fatherPanel.add(contentPanel, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(frameSizeWidth, frameSizeHeight));
frame.setMaximumSize(new Dimension(frameMaxSizeWidth, frameMaxSizeHeight));
frame.setMinimumSize(new Dimension(frameMinSizeWidth, frameMinSizeHeight));
frame.setUndecorated(true);
//if (frameResizeable = true) {
//
//}
frame.add(fatherPanel);
frame.setLocation(frameLocationX, frameLocationY);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(frameVisible);
}
public static Color getMinimizeColor() {
return minimizeColor;
}
public static void setMinimizeColor(Color minimizeColor) {
KFrame.minimizeColor = minimizeColor;
}
public static Color getMaximizeColor() {
return maximizeColor;
}
public static void setMaximizeColor(Color maximizeColor) {
KFrame.maximizeColor = maximizeColor;
}
public static Color getExitColor() {
return exitColor;
}
public static void setExitColor(Color exitColor) {
KFrame.exitColor = exitColor;
}
public static int getFrameSizeWidth() {
return frameSizeWidth;
}
public static void setFrameSizeWidth(int frameSizeWidth) {
KFrame.frameSizeWidth = frameSizeWidth;
}
public static int getFrameSizeHeight() {
return frameSizeHeight;
}
public static void setFrameSizeHeight(int frameSizeHeight) {
KFrame.frameSizeHeight = frameSizeHeight;
}
public static boolean isFrameCenter() {
return frameCenter;
}
public static void setFrameCenter(boolean frameCenter) {
KFrame.frameCenter = frameCenter;
}
public static int getFrameLocationX() {
return frameLocationX;
}
public static void setFrameLocationX(int frameLocationX) {
KFrame.frameLocationX = frameLocationX;
}
public static int getFrameLocationY() {
return frameLocationY;
}
public static void setFrameLocationY(int frameLocationY) {
KFrame.frameLocationY = frameLocationY;
}
public static boolean isFrameResizeable() {
return frameResizeable;
}
public static void setFrameResizeable(boolean frameResizeable) {
KFrame.frameResizeable = frameResizeable;
}
public static boolean isFrameMovable() {
return frameMovable;
}
public static void setFrameMovable(boolean frameMovable) {
KFrame.frameMovable = frameMovable;
}
public static boolean isFrameVisible() {
return frameVisible;
}
public static void setFrameVisible(boolean frameVisible) {
KFrame.frameVisible = frameVisible;
}
public static Color getFrameBackgroundColor() {
return frameBackgroundColor;
}
public static void setFrameBackgroundColor(Color frameBackgroundColor) {
KFrame.frameBackgroundColor = frameBackgroundColor;
}
public static void JCheckBox(javax.swing.JCheckBox JCheckBox) {
KFrame.checkBox = JCheckBox;
}
public static void JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem JCheckBoxMenuItem) {
KFrame.checkBoxMenuItem = JCheckBoxMenuItem;
}
public static void JColorChooser(javax.swing.JColorChooser JColorChooser) {
KFrame.colorChooser = JColorChooser;
}
public static void JComponent(javax.swing.JComponent JComponent) {
KFrame.component = JComponent;
}
public static void JDesktopPane(javax.swing.JDesktopPane JDesktopPane) {
KFrame.desktopPane = JDesktopPane;
}
public static void JDialog(javax.swing.JDialog JDialog) {
KFrame.dialog = JDialog;
}
public static void JEditorPane(javax.swing.JEditorPane JEditorPane) {
KFrame.editorPane = JEditorPane;
}
public static void JFileChooser(javax.swing.JFileChooser JFileChooser) {
KFrame.fileChooser = JFileChooser;
}
public static void JFormattedTextField(javax.swing.JFormattedTextField JFormattedTextField) {
KFrame.formattedTextField = JFormattedTextField;
}
public static void JInternalFrame(javax.swing.JInternalFrame JInternalFrame) {
KFrame.internalFrame = JInternalFrame;
}
public static void JLabel(javax.swing.JLabel JLabel) {
KFrame.label = JLabel;
}
public static void JLayeredPane(javax.swing.JLayeredPane JLayeredPane) {
KFrame.layeredPane = JLayeredPane;
}
public static void JMenu(javax.swing.JMenu JMenu) {
KFrame.menu = JMenu;
}
public static void JMenuBar(javax.swing.JMenuBar JMenuBar) {
KFrame.menuBar = JMenuBar;
}
public static void JMenuItem(javax.swing.JMenuItem JMenuItem) {
KFrame.menuItem = JMenuItem;
}
public static void JOptionPane(javax.swing.JOptionPane JOptionPane) {
KFrame.optionPane = JOptionPane;
}
public static void JPanel(javax.swing.JPanel JPanel) {
KFrame.panel = JPanel;
}
public static void JPasswordField(javax.swing.JPasswordField JPasswordField) {
KFrame.passwordField = JPasswordField;
}
public static void JPopupMenu(javax.swing.JPopupMenu JPopupMenu) {
KFrame.popupMenu = JPopupMenu;
}
public static void JProgressBar(javax.swing.JProgressBar JProgressBar) {
KFrame.progressBar = JProgressBar;
}
public static void JRadioButton(javax.swing.JRadioButton JRadioButton) {
KFrame.radioButton = JRadioButton;
}
public static void JRadioButtonMenuItem(javax.swing.JRadioButtonMenuItem JRadioButtonMenuItem) {
KFrame.radioButtonMenuItem = JRadioButtonMenuItem;
}
public static void JRootPane(javax.swing.JRootPane JRootPane) {
KFrame.rootPane = JRootPane;
}
public static void JScrollBar(javax.swing.JScrollBar JScrollBar) {
KFrame.scrollBar = JScrollBar;
}
public static void JScrollPane(javax.swing.JScrollPane JScrollPane) {
KFrame.scrollPane = JScrollPane;
}
public static void JSeparator(javax.swing.JSeparator JSeparator) {
KFrame.separator = JSeparator;
}
public static void JSlider(javax.swing.JSlider JSlider) {
KFrame.slider = JSlider;
}
public static void JSpinner(javax.swing.JSpinner JSpinner) {
KFrame.spinner = JSpinner;
}
public static void JSplitPane(javax.swing.JSplitPane JSplitPane) {
KFrame.splitPane = JSplitPane;
}
public static void JTabbedPane(javax.swing.JTabbedPane JTabbedPane) {
KFrame.tabbedPane = JTabbedPane;
}
public static void JTable(javax.swing.JTable JTable) {
KFrame.table = JTable;
}
public static void JTextArea(javax.swing.JTextArea JTextArea) {
KFrame.textArea = JTextArea;
}
public static void JTextField(javax.swing.JTextField JTextField) {
KFrame.textField = JTextField;
}
public static void JTextPane(javax.swing.JTextPane JTextPane) {
KFrame.textPane = JTextPane;
}
public static void JToggleButton(javax.swing.JToggleButton JToggleButton) {
KFrame.toggleButton = JToggleButton;
}
public static void JToolBar(javax.swing.JToolBar JToolBar) {
KFrame.toolBar = JToolBar;
}
public static void JToolTip(javax.swing.JToolTip JToolTip) {
KFrame.toolTip = JToolTip;
}
public static void JTree(javax.swing.JTree JTree) {
KFrame.tree = JTree;
}
public static void JViewport(javax.swing.JViewport JViewport) {
KFrame.viewport = JViewport;
}
public static void JWindow(javax.swing.JWindow JWindow) {
KFrame.window = JWindow;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public static boolean isExitMessage() {
return exitMessage;
}
public static void setExitMessage(boolean exitMessage) {
KFrame.exitMessage = exitMessage;
}
public static int getFrameMaxSizeWidth() {
return frameMaxSizeWidth;
}
public static void setFrameMaxSizeWidth(int frameMaxSizeWidth) {
KFrame.frameMaxSizeWidth = frameMaxSizeWidth;
}
public static int getFrameMaxSizeHeight() {
return frameMaxSizeHeight;
}
public static void setFrameMaxSizeHeight(int frameMaxSizeHeight) {
KFrame.frameMaxSizeHeight = frameMaxSizeHeight;
}
public static int getFrameMinSizeWidth() {
return frameMinSizeWidth;
}
public static void setFrameMinSizeWidth(int frameMinSizeWidth) {
KFrame.frameMinSizeWidth = frameMinSizeWidth;
}
public static int getFrameMinSizeHeight() {
return frameMinSizeHeight;
}
public static void setFrameMinSizeHeight(int frameMinSizeHeight) {
KFrame.frameMinSizeHeight = frameMinSizeHeight;
}
public void JButton(javax.swing.JButton JButton) {
KFrame.button = JButton;
}
@SuppressWarnings("DuplicatedCode")
private javax.swing.JButton createButtonMinimize() {
javax.swing.JButton button = new JButton(minimizeIcon);
button.setForeground(minimizeColor);
button.setBorderPainted(false);
button.setBorder(null);
button.setFocusable(false);
button.setMargin(new Insets(0, 0, 0, 0));
button.setContentAreaFilled(false);
return button;
}
@SuppressWarnings("DuplicatedCode")
private javax.swing.JButton createButtonMaximize() {
javax.swing.JButton button = new JButton(maximizeIcon);
button.setForeground(maximizeColor);
button.setBorderPainted(false);
button.setBorder(null);
button.setFocusable(false);
button.setMargin(new Insets(0, 0, 0, 0));
button.setContentAreaFilled(false);
return button;
}
@SuppressWarnings("DuplicatedCode")
private javax.swing.JButton createButtonExit() {
javax.swing.JButton button = new JButton(exitIcon);
button.setForeground(exitColor);
button.setBorderPainted(false);
button.setBorder(null);
button.setFocusable(false);
button.setMargin(new Insets(0, 0, 0, 0));
button.setContentAreaFilled(false);
return button;
}
public String getMinimizeIcon() {
return minimizeIcon;
}
public static void setMinimizeIcon(String minimizeIcon) {
KFrame.minimizeIcon = minimizeIcon;
}
public String getMaximizeIcon() {
return maximizeIcon;
}
public static void setMaximizeIcon(String maximizeIcon) {
KFrame.maximizeIcon = maximizeIcon;
}
public String getExitIcon() {
return exitIcon;
}
public static void setExitIcon(String exitIcon) {
KFrame.exitIcon = exitIcon;
}
private static class GradientPanel extends JPanel {
private static final long serialVersionUID = 1L;
public GradientPanel(Color background) {
setBackground(background);
}
//Gradient Support
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (isOpaque()) {
Color background = new Color(49, 46, 46);
Color controlColor = new Color(49, 46, 46);
int width = getWidth();
int height = getHeight();
Graphics2D g2 = (Graphics2D) g;
Paint oldPaint = g2.getPaint();
g2.setPaint(new GradientPaint(0, 0, background, width, 0, controlColor));
g2.fillRect(0, 0, width, height);
g2.setPaint(oldPaint);
}
}
}
public static class FrameDragListener extends MouseAdapter {
private final JFrame frame;
private Point mouseDownCompCoords = null;
public FrameDragListener(JFrame frame) {
this.frame = frame;
}
public void mouseReleased(MouseEvent e) {
mouseDownCompCoords = null;
}
public void mousePressed(MouseEvent e) {
assert e != null;
mouseDownCompCoords = e.getPoint();
}
public void mouseDragged(MouseEvent e) {
assert e != null;
Point currCoords = e.getLocationOnScreen();
frame.setLocation(currCoords.x - mouseDownCompCoords.x, currCoords.y - mouseDownCompCoords.y);
}
}
}