2010-08-17 6 views
1

특정 동작이 발생하면 JFrame을 최대화하는 방법을 아는 사람이 있습니까? 나는 아래 코드를 게시 할 것이고, 필자는 코드를 주석 처리하지 않은 (나는 언젠가는 그것을 할 것이다) 악마 다.하지만 그것은 꽤 설명 적이어야한다. 내가하려는 것은 메뉴에서 옵션이 선택되면 프레임을 최대화하는 것입니다. 누구든지 내 코드를 편집하여이 코드를 편집해야 할 필요가 있다고 판단되면 크게 환영 할 것입니다.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(); 
     } 
    }); 
} 

}

답변

4

당신은 현재 상태를 포함 할 필요가 없습니다; 이 충분해야한다 :

aFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

당신은 비록 actionPerformed 방법에있는 당신의 프레임에 대한 액세스가 필요합니다, 그래서 당신은 아마 개인 필드해야한다.

수업 시작에 일부 입력란을 선언하십시오. 모두 비공개로 표시해야하며 프레임을 추가해야합니다. 그래서 다음과 같이 보일 것이다 : 당신이 JFrame frame = new JFrame("Account Appeal Aide");이있을 때

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

가, 나중에, 단지 대신 frame = new JFrame("Account Appeal Aide");을; frame은 이미 선언되었습니다. 그런 다음 actionPerformed에서도 사용할 수 있습니다.

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