最大化 JFrame 的动作?

发布于 2024-09-15 12:27:04 字数 6971 浏览 7 评论 0原文

有谁知道一旦发生特定操作如何最大化 JFrame ?我将在下面发布我的代码,我是一个不评论我的代码的恶魔(我会在某个时候这样做),但它应该是相当不言自明的。我想做的是从菜单中选择选项后使框架最大化。如果有人能告诉我如何编辑代码才能实现这一目标,我将不胜感激。

谢谢!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;

public class MainMenu implements ActionListener {
    JTextArea output;
    JScrollPane scrollPane;
    String newline = "\n";
    MapMenu maps = new MapMenu();

public JMenuBar createMenuBar() {
    JMenuBar menuBar;
    JMenu menu;
    JMenuItem menuItem;
    JMenuItem minOption, maxOption;

    //Create the menu bar.
    menuBar = new JMenuBar();

    //Build the first menu.
    menu = new JMenu("File");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription("For window operations");
    menuBar.add(menu);

    minOption = new JMenuItem("Minimize", KeyEvent.VK_T);
    minOption.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    minOption.getAccessibleContext().setAccessibleDescription("Will minimize window");
    minOption.setActionCommand("Minimize");
    minOption.addActionListener(this);
    menu.add(minOption);

    maxOption = new JMenuItem("Maximize", KeyEvent.VK_T);
    maxOption.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
    maxOption.getAccessibleContext().setAccessibleDescription("Will maximize window");
    maxOption.setActionCommand("Maximize");
    maxOption.addActionListener(this);
    menu.add(maxOption);

    menuItem = new JMenuItem("Exit", KeyEvent.VK_T);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.ALT_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription("Will close window");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //Build second menu in the menu bar.
    menu = new JMenu("Pages");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("For accessing other pages");
    menuBar.add(menu);

    menuItem = new JMenuItem("Date Calculator", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens date calculator");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Maps", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens popular maps");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Points System", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens points chart");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Foreign Language Re-targets", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens LPS Lists");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //Build third menu in the menu bar.
    menu = new JMenu("Resources");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("For external resources");
    menuBar.add(menu);

    menuItem = new JMenuItem("Helpful Sites", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens website links");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //Build fourth menu in the menu bar.
    menu = new JMenu("Help");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("For help pages");
    menuBar.add(menu);

    menuItem = new JMenuItem("How To Use", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens tutorial for AAA");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("About", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens information about AAA");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Update", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens website for AAA");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    return menuBar;
}

public Container createContentPane() {
    //Create the content-pane-to-be.
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setOpaque(true);

    //Create a scrolled text area.
    output = new JTextArea(5, 30);
    output.setEditable(false);
    scrollPane = new JScrollPane(output);

    //Add the text area to the content pane.
    contentPane.add(scrollPane, BorderLayout.CENTER);

    return contentPane;
}

// Returns just the class name -- no package info.
protected String getClassName(Object o) {
    String classString = o.getClass().getName();
    int dotIndex = classString.lastIndexOf(".");
    return classString.substring(dotIndex+1);
}

/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = MainMenu.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame.setDefaultLookAndFeelDecorated(false);
    JFrame frame = new JFrame("Account Appeal Aide");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    MainMenu demo = new MainMenu();
    frame.setJMenuBar(demo.createMenuBar());
    frame.setContentPane(demo.createContentPane());

    //Display the window.
    frame.setSize(500, 400);
    frame.setVisible(true);
    frame.setResizable(false);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    int w = frame.getSize().width;
    int h = frame.getSize().height;
    int x = (dim.width-w)/2; 
    int y = (dim.height-h)/2; 

    frame.setLocation(x, y); 
}

public void maximizeFrame(JFrame aFrame) {
    aFrame.setExtendedState(aFrame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
}

public void actionPerformed(ActionEvent e) {
    if("Minimize".equals(e.getActionCommand())) {
        maps.openMapMenu();
    }
    if("Maximize".equals(e.getActionCommand())) {
        //maximizeFrame();
    }
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

}

Does anyone know how to maximize a JFrame once a specfic action has occured? I'll post my code below, I'm a fiend for not commenting my code (I'll do it at some point) but it should be fairly self explanitory. What I was trying to do was to get the frame to maximize once the option was selected from the menu. If anyone can tell me how I need to edit my code to pull this off, it would be greatly appreciated.

Thanks!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;

public class MainMenu implements ActionListener {
    JTextArea output;
    JScrollPane scrollPane;
    String newline = "\n";
    MapMenu maps = new MapMenu();

public JMenuBar createMenuBar() {
    JMenuBar menuBar;
    JMenu menu;
    JMenuItem menuItem;
    JMenuItem minOption, maxOption;

    //Create the menu bar.
    menuBar = new JMenuBar();

    //Build the first menu.
    menu = new JMenu("File");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription("For window operations");
    menuBar.add(menu);

    minOption = new JMenuItem("Minimize", KeyEvent.VK_T);
    minOption.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    minOption.getAccessibleContext().setAccessibleDescription("Will minimize window");
    minOption.setActionCommand("Minimize");
    minOption.addActionListener(this);
    menu.add(minOption);

    maxOption = new JMenuItem("Maximize", KeyEvent.VK_T);
    maxOption.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
    maxOption.getAccessibleContext().setAccessibleDescription("Will maximize window");
    maxOption.setActionCommand("Maximize");
    maxOption.addActionListener(this);
    menu.add(maxOption);

    menuItem = new JMenuItem("Exit", KeyEvent.VK_T);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.ALT_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription("Will close window");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //Build second menu in the menu bar.
    menu = new JMenu("Pages");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("For accessing other pages");
    menuBar.add(menu);

    menuItem = new JMenuItem("Date Calculator", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens date calculator");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Maps", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens popular maps");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Points System", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens points chart");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Foreign Language Re-targets", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens LPS Lists");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //Build third menu in the menu bar.
    menu = new JMenu("Resources");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("For external resources");
    menuBar.add(menu);

    menuItem = new JMenuItem("Helpful Sites", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens website links");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //Build fourth menu in the menu bar.
    menu = new JMenu("Help");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("For help pages");
    menuBar.add(menu);

    menuItem = new JMenuItem("How To Use", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens tutorial for AAA");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("About", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens information about AAA");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Update", KeyEvent.VK_T);
    menuItem.getAccessibleContext().setAccessibleDescription("Opens website for AAA");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    return menuBar;
}

public Container createContentPane() {
    //Create the content-pane-to-be.
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setOpaque(true);

    //Create a scrolled text area.
    output = new JTextArea(5, 30);
    output.setEditable(false);
    scrollPane = new JScrollPane(output);

    //Add the text area to the content pane.
    contentPane.add(scrollPane, BorderLayout.CENTER);

    return contentPane;
}

// Returns just the class name -- no package info.
protected String getClassName(Object o) {
    String classString = o.getClass().getName();
    int dotIndex = classString.lastIndexOf(".");
    return classString.substring(dotIndex+1);
}

/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = MainMenu.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame.setDefaultLookAndFeelDecorated(false);
    JFrame frame = new JFrame("Account Appeal Aide");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    MainMenu demo = new MainMenu();
    frame.setJMenuBar(demo.createMenuBar());
    frame.setContentPane(demo.createContentPane());

    //Display the window.
    frame.setSize(500, 400);
    frame.setVisible(true);
    frame.setResizable(false);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    int w = frame.getSize().width;
    int h = frame.getSize().height;
    int x = (dim.width-w)/2; 
    int y = (dim.height-h)/2; 

    frame.setLocation(x, y); 
}

public void maximizeFrame(JFrame aFrame) {
    aFrame.setExtendedState(aFrame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
}

public void actionPerformed(ActionEvent e) {
    if("Minimize".equals(e.getActionCommand())) {
        maps.openMapMenu();
    }
    if("Maximize".equals(e.getActionCommand())) {
        //maximizeFrame();
    }
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2024-09-22 12:27:04

您不需要包含当前状态;这应该足够了:

aFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

不过,您需要在 actionPerformed 方法中访问您的框架,因此您应该将其设为私有字段。

在课程开始时,您声明一些字段。这些都应该标记为私有,并且您还应该添加框架。所以它看起来像这样:

public class MainMenu implements ActionListener {
    private JTextArea output;
    private JScrollPane scrollPane;
    private String newline = "\n";
    private MapMenu maps = new MapMenu();
    private JFrame frame;

然后,当你有 JFrame frame = new JFrame("Account Appeal Aide"); 时,只需执行 frame = new JFrame("Account Appeal Aide" ); 改为; frame 已声明。然后,您也可以在 actionPerformed 中使用它:

if("Maximize".equals(e.getActionCommand())) {
    maximizeFrame(frame);
}

You don't need to include the current state; this should be enough:

aFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

You will need access to your frame in the actionPerformed method, though, so you should probably make it a private field.

At the beginning of your class, you declare some fields. Those should all be marked private, and you should add the frame as well. So it would look like this:

public class MainMenu implements ActionListener {
    private JTextArea output;
    private JScrollPane scrollPane;
    private String newline = "\n";
    private MapMenu maps = new MapMenu();
    private JFrame frame;

Then, later, when you have JFrame frame = new JFrame("Account Appeal Aide");, just do frame = new JFrame("Account Appeal Aide"); instead; frame has already been declared. Then, you can use it in actionPerformed as well:

if("Maximize".equals(e.getActionCommand())) {
    maximizeFrame(frame);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文