简体   繁体   中英

Netbeans JDialog size

I'm creating a JDialog with netbeans as my IDE. I change the size to how I want it in netbeans, and I can see it updating the prefferredSize, but when I run my application it looks like this:

在此处输入图片说明

Is there something else I need to set to set the size? Or at least make it sized properly so I can see the controls... (theres 6 on it)

In the form designer switch to the "Code" tab of the properties window. There is a property named "Form Size Policy".

This defaults to pack() which means that the dialog is resized to be as big as the contained components require it. For more details please read the Javadocs for the pack() method. Without knowing what components you put on the dialog, it's hard to tell why pack() doesn't work for you.

If you change that to "Generate Resize Code" the dialog will stick to the size you define:

在此处输入图片说明

I guess you might be using the Netbeans GUI builder, possible solutions/suggestions,

  1. You might be missing dialog.pack();
  2. Right click jDialog > Properties > (Set) minimumSize
  3. (Suggestion) Ditch the GUI Builder, learn Java instead of learning an IDE !

btw, this works for me ,

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class DialogsTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();

                f.setSize(400, 300);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);

                JPanel message = new JPanel();
                message.add(new JLabel("This is a dialog :)"));
                JDialog dialog = new JDialog(f, "Dialog");

                dialog.setContentPane(message);
                dialog.pack();
                dialog.setLocationRelativeTo(f);
                dialog.setVisible(true);
            }
        });
    }
}

Use the setSize() method instead. For some reason this works, but setPreferredSize() doesn't (at least not as expected).

By the way, it's usually advised not to set a specific size on components.

Download latest Netbean IDE from https://netbeans.org/downloads/

And set the following property: Jframe属性

Good luck!

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