Question

The problem and question is in line 50-51:

package exercise1;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class MainFrame extends JFrame {

    // -------------------------------------------------------------------------

    public static void main(String[] args) {
        MainFrame frame = new MainFrame();
        frame.pack();
        frame.setVisible(true);
    }

    public MainFrame() {
        this.setTitle("Exercise 1");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation(300, 200);
        this.getContentPane().setPreferredSize(new Dimension(400, 150));
        this.getContentPane().setLayout(null);
        this.initContent();
    }

    // -------------------------------------------------------------------------

    private final JButton btnCombine = new JButton();

    private void initContent() {
        this.add(btnTest);
        btnTest.setText("Change Size");
        btnTest.setSize(100, 25);
        btnTest.setLocation(150, 100);
        btnTest.addActionListener(action);
    }

    // -------------------------------------------------------------------------

    private final Controller action = new Controller();

    private class Controller implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
                if (e.getSource() == btnTest) {
                    //CHANGE THE WINDOWS SIZE HERE;
                                        MainFrame.getContentPane().setPreferredSize(new Dimension(100, 200));
                }
        }
    }
}

How I can change the size of windows, if I have code like this?

There is one condition: I want to change the size of it on the button click. In this example: I click on button btnTest and the size of windows changes to no matter what else size.

Thanks! :)

Was it helpful?

Solution 2

Change the line

MainFrame.getContentPane().setPreferredSize(new Dimension(100, 200));

to

setSize(new Dimension(100, 200));

You need to call the setSize method on your outer class object (which is the main frame).

OTHER TIPS

I decided that instead of further critique on the accepted answer, it would be better to show what I mean ('put your code where your mouth is'). See comments in source for more details.

Big Small

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class MainFrame extends JFrame {

    JButton btnTest;
    JPanel btnContainer;

    // ---------------------------------------------------------------

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                JFrame frame = new MainFrame();
                frame.pack();
                frame.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }

    public MainFrame() {
        this.setTitle("Exercise 1");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //this.setLocation(300, 200);  BETTER TO..
        this.setLocationByPlatform(true);
        // THESE NUMBERS ARE NOT MUCH BETTER THAN 'MAGIC NUMBERS'
        //this.getContentPane().setPreferredSize(new Dimension(400, 150));
        // DON'T DO THIS - IT WILL CAUSE MORE PROBLEMS THAN IT FIXES
        //this.getContentPane().setLayout(null);
        this.initContent();
    }

    // ---------------------------------------------------------------

    private final JButton btnCombine = new JButton();

    private void initContent() {
        btnTest = new JButton("Change Size");
        //btnTest.setSize(100, 25);  DON'T GUESS COMPONENT SIZES!
        //btnTest.setLocation(150, 100); ADD A BORDER INSTEAD!
        // WE ACTUALLY ADD THE BORDER TO A CONTAINER THAT HOLDS THE BUTTON.
        // THIS WAY IT RETAINS THE USUAL BORDERS ON FOCUS, PRESS ETC.
        btnContainer = new JPanel(new GridLayout());
        btnContainer.add(btnTest);
        btnContainer.setBorder(new EmptyBorder(100,150,100,150));
        btnTest.addActionListener(action);
        add(btnContainer);
    }

    // ---------------------------------------------------------------

    private final Controller action = new Controller();

    private class Controller implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btnTest) {
                //CHANGE THE WINDOWS SIZE HERE;
                btnContainer.setBorder(new EmptyBorder(30,20,30,20));
                MainFrame.this.pack();
            }
        }
    }
}

General tips

The 2nd point is implemented in the above source. The 1st is left as an exercise for the reader.

  1. Don't extend frame or other top level containers. Instead create & use an instance of one.
  2. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space.

    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.



Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top