Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Swing GridBagLayout-添加没有空格的按钮_Java_Swing_Layout Manager_Gridbaglayout - Fatal编程技术网

Java Swing GridBagLayout-添加没有空格的按钮

Java Swing GridBagLayout-添加没有空格的按钮,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,如何删除gridBagLayout造成的间距并使它们粘在一起? 下面是我的代码,只需添加3个按钮。 我读了这个问题,但没有一个完整的答案 我只想把我所有的按钮都放在JFrame的顶部 import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; impor

如何删除gridBagLayout造成的间距并使它们粘在一起?
下面是我的代码,只需添加3个按钮。 我读了这个问题,但没有一个完整的答案
我只想把我所有的按钮都放在JFrame的顶部

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyProblem {

    private JFrame frame = new JFrame();

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

    public MyProblem() {
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.weightx = 1;
        gc.weighty = 1;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.anchor = GridBagConstraints.NORTH;
        for (int i = 0; i < 3; i++) {
            JPanel jPanel = new JPanel(new BorderLayout());
            jPanel.setSize(80, 80);
            jPanel.add(new JButton("Button " + i),BorderLayout.PAGE_START);
            frame.add(jPanel, gc);
            gc.gridy++;
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

}
导入java.awt.BorderLayout;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类问题{
私有JFrame=新JFrame();
公共静态void main(字符串[]args){
新问题();
}
公共卫生问题{
frame.setLayout(新的GridBagLayout());
GridBagConstraints gc=新的GridBagConstraints();
gc.weightx=1;
gc.weighty=1;
gc.gridx=0;
gc.gridy=0;
gc.fill=GridBagConstraints.HORIZONTAL;
gc.anchor=gridbag.NORTH;
对于(int i=0;i<3;i++){
JPanel JPanel=newjpanel(newborderlayout());
jPanel.setSize(80,80);
jPanel.add(新的JButton(“按钮”+i),BorderLayout.PAGE_START);
frame.add(jPanel,gc);
gc.gridy++;
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。设置尺寸(500500);
frame.setVisible(true);
}
}
我的按钮的外观:

我希望按钮的外观:

布局由一列按钮组成,因此

更改:

        gc.fill = GridBagConstraints.HORIZONTAL;
致:

编辑1 我想把按钮保持在图片中描述的高度,并把它们放在顶部

使用组合布局将它们约束到顶部相当容易。在这种情况下,我们可以将带有按钮的面板添加到带有
边框布局的面板的
页面的开始部分。边框布局的这一部分将拉伸子组件(我们的带有
GridLayout
的面板)的宽度,以填充可用空间,但它将考虑其中任何内容的高度-仅为组件提供所需的垂直空间

编辑2 下面是一个实现上述思想的MCVE。外部面板(带青色边框)用于约束按钮面板(带橙色边框)的高度。有关其工作原理的更多详细信息,请参阅源代码中的注释

import java.awt.*;
导入javax.swing.*;
导入javax.swing.border.LineBorder;
公共类问题{
私有JFrame=新JFrame();
公共静态void main(字符串[]args){
新问题();
}
公共卫生问题{
frame.setLayout(new BorderLayout());//实际上是默认值
//我们将使用此面板约束带有按钮的面板
JPanel ui=newjpanel(newborderlayout());
frame.add(用户界面);
ui.setboorder(新的线边框(Color.CYAN,3));
//按钮的面板(带GridLayout)
JPanel toolPanel=newjpanel(newgridlayout(0,1,0,4));//添加了一些间隙
toolPanel.setBorder(新的线边框(颜色为橙色,3));
//toolPanel将显示在ui面板的顶部
添加(工具面板,边框布局,页面开始);
对于(int i=0;i<3;i++){
添加(新的JButton(“按钮”+i));
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(500500);//改为。。
frame.pack();//pack将使其尽可能小。
frame.setMinimumSize(frame.getSize());//很好的调整。。
frame.setVisible(true);
}
}

运行此代码,我想您会从中得到帮助

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

public class Think {

public static void addComponentsToPane(Container pane) {

    JButton jbnButton;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gBC = new GridBagConstraints();
    gBC.fill = GridBagConstraints.HORIZONTAL;

    jbnButton = new JButton("Button 1");
    gBC.weightx = 0.5;
    gBC.gridx = 0;
    gBC.gridy = 0;
    pane.add(jbnButton, gBC);

    jbnButton = new JButton("Button 3");
    gBC.gridx = 2;
    gBC.gridy = 0;
    pane.add(jbnButton, gBC);

    jbnButton = new JButton("Button 4");
    gBC.ipady = 40;     //This component has more breadth compared to other buttons
    gBC.weightx = 0.0;
    gBC.gridwidth = 3;
    gBC.gridx = 0;
    gBC.gridy = 1;
    pane.add(jbnButton, gBC);

}

private static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("GridBagLayout Source Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    addComponentsToPane(frame.getContentPane());

    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
 }
}

此代码将执行以下操作:

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

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class MyProblem {

    private JFrame frame = new JFrame();

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

    public MyProblem() {
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.weightx = 1;
        gc.weighty = 0;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.ipadx = 0;
        gc.ipady = 0;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.anchor = GridBagConstraints.NORTH;
        for (int i = 0; i < 3; i++) {
            JButton button = new JButton("Button " + i);
            frame.add(button, gc);
            gc.gridy++;
        }
        gc.weighty = 1;
        frame.add(Box.createGlue(), gc); //Adding a component to feel the area.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

}
导入java.awt.gridbag约束;
导入java.awt.GridBagLayout;
导入javax.swing.Box;
导入javax.swing.JButton;
导入javax.swing.JFrame;
公共类问题{
私有JFrame=新JFrame();
公共静态void main(字符串[]args){
新问题();
}
公共卫生问题{
frame.setLayout(新的GridBagLayout());
GridBagConstraints gc=新的GridBagConstraints();
gc.weightx=1;
gc.weighty=0;
gc.gridx=0;
gc.gridy=0;
gc.ipadx=0;
gc.ipady=0;
gc.fill=GridBagConstraints.HORIZONTAL;
gc.anchor=gridbag.NORTH;
对于(int i=0;i<3;i++){
JButton按钮=新JButton(“按钮”+i);
frame.add(按钮,gc);
gc.gridy++;
}
gc.weighty=1;
frame.add(Box.createGlue(),gc);//添加一个组件来感受该区域。
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。设置尺寸(500500);
frame.setVisible(true);
}
}

谢谢Borr先生的修改:)。请参见
JLabel
JButton
。谢谢,但在这个问题上,他/她想添加间距,但我想删除间距。第二个也不起作用。请检查第二个链接。此外,我相信您可以为边距值提供负值。为了更快地获得更好的帮助,请发布一个(最小完整可验证示例)或(简短、独立、正确的示例)。我真的不明白我的两个面板是什么以及它们是什么布局管理器。面板1有
GridBagLayout
或(更简单)一个
GridLayout
,将按钮包含在一列中。面板2在
页面_START
中有一个单独的组件(面板1)。您可以包括您的代码吗?我没有成功。我更新了我的问题,我想让他们看起来像。我会考虑添加我的代码,在你给我展示一个你的最佳尝试的MCVE(正如我建议你在23小时前发布)…第二个面板在哪里?在问题开始时,这是一个很好的MCVE,但我想你很清楚,我的建议需要两个小组,所以我希望看到一个尝试…包括第二个小组。这不是我想要的
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Think {

public static void addComponentsToPane(Container pane) {

    JButton jbnButton;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gBC = new GridBagConstraints();
    gBC.fill = GridBagConstraints.HORIZONTAL;

    jbnButton = new JButton("Button 1");
    gBC.weightx = 0.5;
    gBC.gridx = 0;
    gBC.gridy = 0;
    pane.add(jbnButton, gBC);

    jbnButton = new JButton("Button 3");
    gBC.gridx = 2;
    gBC.gridy = 0;
    pane.add(jbnButton, gBC);

    jbnButton = new JButton("Button 4");
    gBC.ipady = 40;     //This component has more breadth compared to other buttons
    gBC.weightx = 0.0;
    gBC.gridwidth = 3;
    gBC.gridx = 0;
    gBC.gridy = 1;
    pane.add(jbnButton, gBC);

}

private static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("GridBagLayout Source Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    addComponentsToPane(frame.getContentPane());

    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
 }
}
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class MyProblem {

    private JFrame frame = new JFrame();

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

    public MyProblem() {
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.weightx = 1;
        gc.weighty = 0;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.ipadx = 0;
        gc.ipady = 0;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.anchor = GridBagConstraints.NORTH;
        for (int i = 0; i < 3; i++) {
            JButton button = new JButton("Button " + i);
            frame.add(button, gc);
            gc.gridy++;
        }
        gc.weighty = 1;
        frame.add(Box.createGlue(), gc); //Adding a component to feel the area.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

}