简体   繁体   中英

How can I make JFrame resize automatically to display all buttons

I have a simple swing application which consists of a JLabel and three buttons. The three buttons are in their own JPanel which is in a JFrame along with the JLabel. The JPanel uses flowlayout manager to arrange the buttons horizontally and the JFrame uses the BorderLayout manager to arrange the JLabel and JPanel vertically.

My problem is when I launch the application, during the course of use the text on one of the buttons changes which increases its width. However, the window doesn't resize to accomdate this and one of the buttons disappears. I thought about calling pack() again, but the JFrame is a local variable in my constructor, also, I shouldn't have to tell my program to resize, right? I haven't been able to find anything on google or here to help me but there must be a simple solution, what am I missing? Code is below.

    playButton = new JButton("Play");
    pauseButton = new JButton("Pause");
    stopButton = new JButton("Stop");
    curTrackLabel = new JLabel("No Track Selected");

    JFrame myFrame = new JFrame("MediaPlayer");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setTitle("MediaPlayer");
    myFrame.setLocation(400,300);

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    myFrame.add(topPanel);
    JPanel buttonPanel = new JPanel(new FlowLayout());

    buttonPanel.add(playButton);
    buttonPanel.add(pauseButton);
    buttonPanel.add(stopButton);
    topPanel.add(buttonPanel, BorderLayout.CENTER);
    topPanel.add(curTrackLabel, BorderLayout.NORTH);

    playButton.addActionListener(new playButtonHandler());
    pauseButton.addActionListener(new pauseButtonHandler());
    stopButton.addActionListener(new stopButtonHandler());

    myFrame.pack();
    myFrame.setVisible(true);

Maybe try

((JFrame)myButton.getTopLevelAncestor()).pack();

Where myButton is the button whose text is modified during execution.

As with learning any GUI software, experimentation is best. Try messing with BorderLayouts with nested JPanels.

Ultimately, you use JPanel with a BorderLayout (Flow Layout is OK but really when resizing the window, it epically fails). See http://download.oracle.com/javase/tutorial/uiswing/layout/border.html to learn more about BorderLayouts.

Now for your layout scheme it should be something along the lines of:

  1. Top Level Container: JFrame
  2. JFrame contains a JPanel (Call this JPanel 1) with a BorderLayout.
  3. The three buttons should be in a SEPARATE jPanel (JPanel 2). JPanel 1 should add the three buttons as BorderLayout.CENTER. In this way, the window will resize if the button changes its width and/or hright.
  4. The JLabel should be added as BorderLayout.LINE_START.

The tutorial at: http://download.oracle.com/javase/tutorial/uiswing/layout/border.html should help you with this. But in general, use the following:

  • Use JPanel and nest JPanels as necessary
  • BorderLayout.CENTER will accomodate size changes---this is the key! (Experiment with this)
  • JFrame should only be used as a top level container (for more complex GUIs, this is true).

If you require more flexibility, check out JGoodies: http://www.jgoodies.com/ . This is more along the lines of creating forms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM