Swing – gridbaglayout multiple button borders

I'm trying to use @ R_ 711_ 2419@Layout Using gridbaglayout 10 (3) in JPanel × 3 1)JButton.

However, if I use a glued box or similar gridbaglayout JPanel, it will occupy @ R_ 711_ 2419@Layout All extra space in the I may have missed something, or is it impossible?

One solution I use is to use the extension element in gridbaglayout to push the button up This places the button in the correct position, but the border seems large

Here is my sample code:

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.@R_711_2419@;
import javax.swing.@R_711_2419@Layout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GridBagLayoutTest extends JFrame {

    public GridBagLayouttest(){
        super();
        this.setTitle("JVectorView");
        this.setSize(300,300);
        this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        Container content = this.getContentPane();
        content.setLayout(new @R_711_2419@Layout(content,@R_711_2419@Layout.Y_AXIS));
        content.add(new JLabel("Hello!"));
        content.add(new Controls());
        content.add(@R_711_2419@.createGlue());
        this.setVisible(true);
    }


    private class Controls extends JPanel{
        private static final int WIDTH = 3,HEIGHT = 3;

        public Controls(){
            GridBagConstraints constraints = new GridBagConstraints();

            //this.setBorder(BorderFactory.createLineBorder(Color.red));
            this.setBorder(BorderFactory.createTitledBorder("Some stuff"));
            constraints.fill = GridBagConstraints.NONE;
            this.setLayout(new GridBagLayout());
            for(int row = 0; row < HEIGHT; row++){
                for(int col = 0; col < WIDTH; coL++){
                    constraints.gridx = col;
                    constraints.gridy = row;
                    this.add(new JButton("B"+(col+row*WIDTH)),constraints);

                }
            }
            constraints.gridx = 1;
            constraints.gridy = 3;
            this.add(new JButton("B"+(10)),constraints);
        }
    }

    public static void main(String[] args) {
        new GridBagLayouttest();
    }

}

I want the border to stick to the button Can I have gridbaglayout crash on its content, or always force the panel to fill?

Solution

JPanel p=new JPanel(new FlowLayout());
JPanel p=new JPanel(new FlowLayout());
p.add(new Controls());
content.add(p);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>