可能重复:
GridBagLayout中的对齐问题
请查看以下代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestForm extends JFrame
{
private JLabel firstLabel, secondLabel, thirdLabel, fourthLabel, fifthLabel;
private JTextField firstTxt, secondTxt, thirdTxt, fourthTxt, fifthTxt;
private JPanel centerPanel;
private JPanel southPanel;
private JLabel comboLabel;
private JComboBox percentageCombo;
private JLabel endTargetLabel;
private JLabel mustLoseLabel;
private GridBagLayout mainLayout = new GridBagLayout();
private GridBagConstraints mainCons = new GridBagConstraints();
public TestForm()
{
//Declaring instance variables
firstLabel = new JLabel("First: ");
secondLabel = new JLabel("Second: ");
thirdLabel = new JLabel("Third: "); …Run Code Online (Sandbox Code Playgroud) 我研究过这个错误,似乎无法找到解决方案.我正在尝试创建一个包含40列和20行的800 JButtons网格.这将最终用于控制我正在制作的多米诺设置机器人将提示多米诺骨牌.我已经使用GridLayout成功创建了一个网格,但是由于项目的性质,我希望每一行都能被半个按钮偏移.我的意思是如何设置计算机键盘上的键.(我本来会添加一张有用的图片,说明我要解释的内容,但显然无法解释事物的初学者不允许添加图片,无论如何).
我尝试通过创建一个包含20个面板的JPanel数组来完成此操作.然后我在面板上添加了40个JButtons.然后我使用GridBagConstraints来抵消每隔一行.我读到你不应该混合awt和swing这样可能是问题,但我不知道.这是代码,我从youtube教程中找到了这个,因为我是一个非常初学者.如果我说的话没有意义,请原谅我.码:
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
public class OffsetGrid {
public static void main (String [] args){
JFrame Frame = new JFrame();
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout grid= new GridLayout();
GridBagConstraints gbca= new GridBagConstraints();
GridBagConstraints gbcb= new GridBagConstraints();
JPanel[] panel=new JPanel[20];
for (int row=0;row<20; row++){
panel[row]=new JPanel(new GridBagLayout());
gbca.gridx=1;
gbca.gridy=row;
gbcb.gridx=0;
gbcb.gridy=row;
for (int y=0; y<40;y++){
grid=new GridLayout(1,40);
panel[row].setLayout(grid);
JButton[] button = new JButton[40];
button[y]=new JButton();
button[y].setOpaque(true);
panel[row].add(button[y]);
}
if (row%2==0){
Frame.add(panel[row], gbcb);
} …Run Code Online (Sandbox Code Playgroud) 我使用了绝对定位(setBounds 和 null 布局),现在开始练习布局管理器,这段代码是 gridbag 布局,但很少有组件没有显示,要么是单元格有问题,要么是其他东西请帮忙!
导入 java.util.StringTokenizer;
导入 java.awt.event.*;
导入 java.awt.*;
导入 javax.swing.*;
类计算器扩展 JFrame
{
JButton add,sub,mul,div,sin,cos,tan,clear,negate,inverse,zero,one,2,3,4,5,6,7,8,9,equalTo,percentage,sqrt;
JTextField 输入;
GridBagLayout gbl;
private void addComponent(组件组件,int gridx,int gridy,int gridwidth,int gridheight,Insets insets)
{
add(component, new GridBagConstraints(gridx, gridy,gridwidth, gridheight, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0,0));
}
计算器()
{
//setSize(500,400);
设置可见(真);
setLayout(gbl=new GridBagLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add= new JButton("+");
sub= new JButton("-");
mul= new JButton("*");
div= new JButton("/");
sin = new JButton("sin");
cos= new JButton("cos");
tan= new JButton("tan");
clear= new JButton("C");
negate= new JButton("neg");
inverse= … 我已经编写了以下代码来添加一个JLabela JPanel但它显示在中心,而我预计它将被放置在顶部JPanel.
这是我所指的那段代码:
JPanel pnlProjects = new JPanel();
pnlProjects.setMinimumSize(new Dimension(10, 300));
GridBagLayout gridBagLayout = new GridBagLayout();
pnlProjects.setLayout(gridBagLayout);
GridBagConstraints gridBagConstraints = new GridBagConstraints();
// add multiple label dynamically;
for (int count = 0; count < project.length; count++) {
lblProjects[count] = new JLabel("Project"+count );
lblProjects[count].setHorizontalAlignment(SwingConstants.LEFT);
lblProjects[count].setHorizontalTextPosition(SwingConstants.LEFT);
lblProjects[count].setBorder(BorderFactory.createBevelBorder(0));
lblProjects[count].setPreferredSize(new Dimension(100, 20));
gridBagConstraints.fill = GridBagConstraints.VERTICAL;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = count;
pnlProjects.add(lblProjects[count], gridBagConstraints);
}
// Add project panel in to the scorllPan
JScrollPane jspProjectlist = new JScrollPane(pnlProjects);
Run Code Online (Sandbox Code Playgroud)

有人会根据我的要求向我解释如何改变它吗?
我正在尝试创建一个工具栏,它将位于我的java swing应用程序中所有页面的顶部.
我正在创建一个JPanel,里面有一系列单独的JPanel(容器).每个JPanel(容器)都有一个北部和南部组件,或者只是一个北部组件,使用GridLayout设置.
我的问题是,我想要南北组成部分之间的小差距,但我看不出怎么做,也无法在互联网上找到任何帮助.
下面是其中一个容器的代码的工作示例:
public static void CustomersGui(){
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container1 = new JPanel();
container1.setLayout(new GridLayout(2,1));
JButton buttonDiary = new JButton("Diary");
buttonDiary.setPreferredSize(new Dimension(140, 25));
JButton buttonCars = new JButton("Cars");
buttonCars.setPreferredSize(new Dimension(140, 25));
container1.add(buttonDiary, BorderLayout.NORTH);
container1.add(buttonCars, BorderLayout.SOUTH);
frame.setContentPane(container1);
frame.pack();
frame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud) 根据我的要求,UI应该显示如下图所示。
我试图通过使用下面的代码来达到相同的目的。
package com.samples;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class MyFrame extends JFrame {
public MyFrame() {
}
public static void main(String[] args) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("My Sample Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 800);
frame.setVisible(true);
JPanel jpOPanel = new JPanel();
jpOPanel.setBorder(new CompoundBorder(new …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用GridBagLayout.我需要一个垂直和水平居中的JLabel - 这很容易,我甚至不需要创建任何GridBagConstraints.我还想把JButton放在右下角,当我尝试这样做时,我的中心面板向左移动或按钮向上移动.
EXPECTING GETTING THIS OR THIS
+-----------+ +-----------+ +-----------+
| | | | | |
| | | | | |
| | | | | |
| +---+ | | +---+ | | +---+ |
| | | | | | | | | | | |
| +---+ | | +---+ | | +---++---+|
| | | | | | ||
| | | | | +---+|
| +---+ | +---+ | |
| | | …Run Code Online (Sandbox Code Playgroud)