Java - GridBagLayout错误比率

m3eecexj  于 2023-05-12  发布在  Java
关注(0)|答案(1)|浏览(146)

我是用Java创建GUI的新手。我试图使用GridBagLayout的一个项目,但我被困在这里。
我想让按钮和两个JLabel的大小相同,但按钮更大。

public class Menu extends JPanel {
    
    public Menu() {
        
        
        
        this.setBackground(Color.black);
        
        
        GridBagLayout gb = new GridBagLayout();
        setLayout(gb);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        
        
        JButton b1 = new JButton();
        b1.setText("Start");
        
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty= 1;
        gb.setConstraints(b1, gbc);
        add(b1);
        
        
        JPanel Gap = new JPanel();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty= 1;
        
        gb.setConstraints(Gap, gbc);
        add(Gap);
        
        
        RadioPanel radio = new RadioPanel();
        
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty = 1;
        
        gb.setConstraints(radio, gbc);
        add(radio);
        
        
        
        
        
        
        
        
        
        
    }

}

Click here to see my GUI
这是我的代码,它看起来是这样的。

s4n0splo

s4n0splo1#

GridLayout,不是GridBagLayout

对于三个大小相等的小部件,使用GridLayout而不是GridBagLayout。请参见Oracle教程How to Use GridLayout
下面是一个带有两个标签的按钮的示例,所有标签的大小都相同。

package work.basil.example.swing;

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

// An abbreviated version of example code provided by Oracle from:
// https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/layout/GridBagLayoutDemoProject/src/layout/GridBagLayoutDemo.java
public class ThreeWidgets
{
    private static void createAndShowGUI ( )
    {
        JFrame.setDefaultLookAndFeelDecorated( true );

        // Establish the window, represented by a `JFrame` object.
        JFrame frame = new JFrame( "Three Widgets App" );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        makeContent( frame.getContentPane() );

        // Show window.
        frame.pack();
        frame.setVisible( true );
    }

    private static void makeContent ( Container container )
    {
        container.setLayout( new GridLayout() );

        JButton widget1 = new JButton( "Widget 1" );

        JLabel widget2 = new JLabel( "Widget 2" );
        widget2.setBorder( BorderFactory.createLineBorder( Color.GREEN ) );

        JLabel widget3 = new JLabel( "Widget 3 with long title" );
        widget3.setBorder( BorderFactory.createLineBorder( Color.MAGENTA ) );

        container.add( widget1 );
        container.add( widget2 );
        container.add( widget3 );
    }

    public static void main ( String[] args )
    {
        // Assign a task to event-dispatching thread: Create and show our GUI.
        SwingUtilities.invokeLater( ThreeWidgets :: createAndShowGUI );
    }
}

相关问题