Gag*_*n93 0 java layout swing gridbaglayout
我使用了绝对定位(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= new JButton("1/x");
零 = 新 JButton("0");
one= new JButton("1");
二 = new JButton("2");
三 = 新 JButton("3");
四 = new JButton("4");
五 = 新 JButton("5");
六 = 新 JButton("6");
七 = 新 JButton("7");
八 = 新 JButton("8");
九 = new JButton("9");
equalTo= new JButton("=");
百分比 = new JButton("%");
sqrt= new JButton("sqrt");
输入 = 新 JTextField(20);
addComponent(input,0,0,0,1,new Insets(10,10,100,4)); //tldr
addComponent(add,0,1,1,1,new Insets(4,4,4,4));
addComponent(sub,1,1,2,1,new Insets(4,4,4,4));
addComponent(mul,2,1,3,1,new Insets(4,4,4,4)); // 这个不显示
addComponent(div,3,1,4,1,new Insets(4,4,4,4));
addComponent(sin,0,2,1,2,new Insets(4,4,4,4));
addComponent(cos,1,2,2,2,new Insets(4,4,4,4));
addComponent(tan,2,2,3,2,new Insets(4,4,4,4)); // 这个不显示
addComponent(clear,3,2,4,2,new Insets(4,4,4,4));
addComponent(negate,0,3,1,3,new Insets(4,4,4,4)); // 这4个不可见
addComponent(inverse,1,3,2,3,new Insets(4,4,4,4));
addComponent(zero,2,3,3,3,new Insets(4,4,4,4));
addComponent(one,3,3,4,3,new Insets(4,4,4,4));
盒();
}
public static void main(String...args)
{
SwingUtilities.invokeLater(new Runnable()
{
公共无效运行()
{
新计算器();
}
});
}
}
首先,在使用布局方面做得很好!
立即跳到我身上的是值传递给gridwidth和gridheight
addComponent(add,0,1,1,1,new Insets(4,4,4,4));
addComponent(sub,1,1,2,1,new Insets(4,4,4,4));
addComponent(mul,2,1,3,1,new Insets(4,4,4,4)); // this is not displayed
addComponent(div,3,1,4,1,new Insets(4,4,4,4));
Run Code Online (Sandbox Code Playgroud)
基本上,这是说,加入add到网格X / Y位置为0x1,有gridwidth/gridheight的1x1,好为止。
然后sub在 x/y 位置1x1添加一个gridwidth/ gridheightof 1x2,好吧...
然后mul在 x/y 位置2x1添加一个gridwidth/ gridheightof 1x3,现在我们开始遇到问题......基本上,sub实际上是扩展到我们的单元格中,覆盖了我们的一部分!
gridwidth并gridheight允许您定义组件将扩展到多少个单元格,大多数情况下,您希望这是1x1
一旦我将所有gridwidth和gridheight值更正为1x1,我就能够得到这个

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
class Calculator extends JFrame {
JButton add, sub, mul, div, sin, cos, tan, clear, negate, inverse, zero, one, two, three, four, five, six, seven, eight, nine, equalTo, percentage, sqrt;
JTextField input;
GridBagLayout gbl;
private void addComponent(Component component, 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));
}
Calculator() {
//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 = new JButton("1/x");
zero = new JButton("0");
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
equalTo = new JButton("=");
percentage = new JButton("%");
sqrt = new JButton("sqrt");
input = new JTextField(20);
addComponent(input, 0, 0, 0, 1, new Insets(10, 10, 100, 4)); //tldr
addComponent(add, 0, 1, 1, 1, new Insets(4, 4, 4, 4));
addComponent(sub, 1, 1, 1, 1, new Insets(4, 4, 4, 4));
addComponent(mul, 2, 1, 1, 1, new Insets(4, 4, 4, 4)); // this is not displayed
addComponent(div, 3, 1, 1, 1, new Insets(4, 4, 4, 4));
addComponent(sin, 0, 2, 1, 1, new Insets(4, 4, 4, 4));
addComponent(cos, 1, 2, 1, 1, new Insets(4, 4, 4, 4));
addComponent(tan, 2, 2, 1, 1, new Insets(4, 4, 4, 4)); // this is not displayed
addComponent(clear, 3, 2, 1, 1, new Insets(4, 4, 4, 4));
addComponent(negate, 0, 3, 1, 1, new Insets(4, 4, 4, 4)); // these 4 are not visible
addComponent(inverse, 1, 3, 1, 1, new Insets(4, 4, 4, 4));
addComponent(zero, 2, 3, 1, 1, new Insets(4, 4, 4, 4));
addComponent(one, 3, 3, 1, 1, new Insets(4, 4, 4, 4));
pack();
setVisible(true);
}
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Calculator();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
721 次 |
| 最近记录: |