Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

Space between Layout and top of the Frame. Java Swing

I am trying to create for a first time a GUI with Swing. I want to create it responsive so I am using layouts.

The problem comes with the space between the panel and the top of the frame because there is a big blank space:

enter image description here

Is my first time using Swing and I am trying to find why happens this but I can't find a solution. This is my simple code:

public class GUI2{

    public static void main(String[] args){
        // Create frame with title Registration Demo
        JFrame frame= new JFrame(); 
        //frame.setLocationRelativeTo(null);
        frame.setTitle("HUPA Project ML Algorithm Selection");
         
        // Panel to define the layout. We are using GridBagLayout
        JPanel mainPanel = new JPanel();
        BoxLayout bl = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
        mainPanel.setLayout(bl);
 
        JPanel headingPanel = new JPanel();
        JLabel headingLabel = new JLabel("Choose the path of the files, the output and the ML Algorithm");
        headingPanel.add(headingLabel);
         
        // Panel to define the layout. We are using GridBagLayout
        JPanel panel = new JPanel(new GridBagLayout());
        // Constraints for the layout
        GridBagConstraints constr = new GridBagConstraints();

        //natural height, maximum width
        constr.fill = GridBagConstraints.HORIZONTAL;
        constr.insets = new Insets(5, 3, 3, 3);     
        //constr.anchor = GridBagConstraints.WEST;

        // Declare the required Labels
        JLabel userNameLabel = new JLabel("Enter your name :");
        JLabel pwdLabel = new JLabel("Enter your password :");
        JLabel emailLabel = new JLabel("Enter email :");
         
        // Declare Text fields
        JTextField userNameTxt = new JTextField(20);
        JPasswordField pwdTxt = new JPasswordField(20);
        JTextField emailTxt = new JTextField(20);
        
        //navegadores de directorio
        JButton navCSV = new JButton("CSV File");
        navCSV.setPreferredSize(new Dimension(18, 18)); //ancho alto
        JButton navArff = new JButton("ARFF");
        navArff.setPreferredSize(new Dimension(18, 18));
        JButton navOutput = new JButton("Output");
        navOutput.setPreferredSize(new Dimension(18, 18));
        
        //Botones de ML
        JButton RT_weights = new JButton("Random Tree Weights");
        RT_weights.setPreferredSize(new Dimension(125, 100));
        JButton SA = new JButton("Random Tree No Weights");
        SA.setPreferredSize(new Dimension(125, 100));
        JButton NB = new JButton("Naive Bayes");
        NB.setPreferredSize(new Dimension(125, 100));
        
        constr.gridx=0; constr.gridy=0;
        panel.add(RT_weights, constr);
        constr.gridx=1; constr.gridy=0;
        panel.add(SA, constr);
        constr.gridx=2; constr.gridy=0;
        panel.add(NB, constr);

        // Set the initial grid values to 0,0
        constr.gridx=0; constr.gridy=1;
        panel.add(userNameLabel, constr);
        constr.gridx=1; constr.gridy=1;
        panel.add(userNameTxt, constr);
        constr.gridx=2; constr.gridy=1;
        panel.add(navCSV, constr);
        
        constr.gridx=0; constr.gridy=2;
        panel.add(pwdLabel, constr);
        constr.gridx=1; constr.gridy=2;
        panel.add(pwdTxt, constr);
        constr.gridx=2; constr.gridy=2;
        panel.add(navArff, constr);
         
        constr.gridx=0; constr.gridy=3;
        panel.add(emailLabel, constr);
        constr.gridx=1; constr.gridy=3;
        panel.add(emailTxt, constr);
        constr.gridx=2; constr.gridy=3;
        panel.add(navOutput, constr);
        
        /*constr.gridwidth = 2;
        constr.anchor = GridBagConstraints.CENTER;*/
  
        // Button with text "Register"
        JButton button = new JButton("Register");
        // add a listener to button
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                headingLabel.setText("Thanks for registering. We'll get back to you shortly.");
                userNameTxt.setText("");
                pwdTxt.setText("");
                emailTxt.setText("");
            }
        });

        // Add label and button to panel
        constr.gridx=0; constr.gridy=4; 
        constr.gridwidth = 3; //gridwith cuantas casillas ocupa del layout, aquí 3
        panel.add(button, constr);
  
        mainPanel.add(headingPanel);
        mainPanel.add(panel);
 
        // Add panel to frame
        frame.add(mainPanel);
        frame.pack();
        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
       //panel.setBorder(BorderFactory.createRaisedBevelBorder());     
       // panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    }
}

If you need something more to know, just tell me it. Thanks in advance!

question from:https://stackoverflow.com/questions/65858488/space-between-layout-and-top-of-the-frame-java-swing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a couple of issues. By default:

  1. a GridBagLayout will center the components in the space available
  2. a BoxLayout will allocate space proportionally to each component (up to the maximum size of the component) when extra space is available.

In your code you use:

    frame.pack();
    frame.setSize(600, 600);

The setSize(...) will override the pack() calculations causing the frame to be too large. So extra space is allocated to both the "headingPanel" and the "panel".

Remove the setSize() statement to see the difference.

However, if you drag the frame larger you will now see the space reappear.

The solution is to use:

  1. the default BorderLayout of the JFrame and
  2. a wrapper panel for the GridBagLayout to prevent the components from being vertically centered

Get rid of your "mainPanel" and your code would be something like:

//mainPanel.add(headingPanel);
//mainPanel.add(panel);
//frame.add(mainPanel);

JPanel wrapper = new JPanel();
wrapper.add( panel );

frame.add(headingPanel, BorderLayout.PAGE_START);
frame.add(wrapper, BorderLayout.CENTER);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...