在不使用 swing 的情况下使用 GridBagLayout 在 java 中创建一个简单的设计
Creating a simple design in java using GrigBagLayout without using swing
我想在此代码中仅使用 GrigBagLayout 而不使用 swing 和面板来创建此设计。
为了完成我的作业问题,我只是准备了这个设计的代码,但它的安排并不完美。
import java.awt.*;
class GridBagLayout {
public static void main(String args[]) {
Frame f = new Frame();
f.setSize(400,600);
f.setLayout(new GridLayout());
GridBagConstraints gbc = new GridBagConstraints();
Label l = new Label("Name ");
gbc.gridx=1;
gbc.gridy=1;
f.add(l,gbc);
TextField t = new TextField();
gbc.gridx=1;
gbc.gridy=0;
f.add(t,gbc);
Label l2 = new Label("Password ");
gbc.gridx=0;
gbc.gridy=1;
f.add(l2,gbc);
TextField t2 = new TextField();
gbc.gridx=1;
gbc.gridy=1;
f.add(t2,gbc);
Button b = new Button("OK");
gbc.gridx=1;
gbc.gridy=1;
f.add(b,gbc);
f.setVisible(true);
}
}
所以任何人都可以告诉我我的代码缺少完美安排的地方。
I hope your Assignment work should be based on GridBagLayout
GridBagLayout
so why you are using
- f.setLayout(new GridLayout());
而不是使用正确的布局
f.setLayout(new GridBagLayout);
import java.awt.*;
class GridBagLayout
{
public static void main(String[] args) {
Frame f=new Frame();
f.setSize(300,200);
GridBagLayout gl = new GridBagLayout();
f.setLayout(gl);
GridBagConstraints g=new GridBagConstraints();
g.gridx=0;
g.gridy=0;
f.add(new Label("Name") , g);
g.gridy=1;
f.add(new Label("Password") , g);
g.gridx=1;
g.gridy=0;
f.add(new TextField(15) , g);
g.gridy=1;
g.gridx=1;
f.add(new TextField(15) , g);
g.gridx=0;
g.gridy=2;
g.gridwidth=2;
g.insets = new Insets(30,0,0,0);
f.add(new Button("OK"),g);
f.setVisible(true);
}
}
As GridBagLayout divides a container into a grid of equally sizwd cells and it requires lot of information to know where to put a
component that's why i used gridx/y ,insets etc for proper
arrangements of your labels and texfields.
[让你的 java awt 概念基础扎实][1]
[1]:
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
我想在此代码中仅使用 GrigBagLayout 而不使用 swing 和面板来创建此设计。
为了完成我的作业问题,我只是准备了这个设计的代码,但它的安排并不完美。
import java.awt.*;
class GridBagLayout {
public static void main(String args[]) {
Frame f = new Frame();
f.setSize(400,600);
f.setLayout(new GridLayout());
GridBagConstraints gbc = new GridBagConstraints();
Label l = new Label("Name ");
gbc.gridx=1;
gbc.gridy=1;
f.add(l,gbc);
TextField t = new TextField();
gbc.gridx=1;
gbc.gridy=0;
f.add(t,gbc);
Label l2 = new Label("Password ");
gbc.gridx=0;
gbc.gridy=1;
f.add(l2,gbc);
TextField t2 = new TextField();
gbc.gridx=1;
gbc.gridy=1;
f.add(t2,gbc);
Button b = new Button("OK");
gbc.gridx=1;
gbc.gridy=1;
f.add(b,gbc);
f.setVisible(true);
}
}
所以任何人都可以告诉我我的代码缺少完美安排的地方。
I hope your Assignment work should be based on GridBagLayout
GridBagLayout
so why you are using
- f.setLayout(new GridLayout());
而不是使用正确的布局
f.setLayout(new GridBagLayout);
import java.awt.*;
class GridBagLayout
{
public static void main(String[] args) {
Frame f=new Frame();
f.setSize(300,200);
GridBagLayout gl = new GridBagLayout();
f.setLayout(gl);
GridBagConstraints g=new GridBagConstraints();
g.gridx=0;
g.gridy=0;
f.add(new Label("Name") , g);
g.gridy=1;
f.add(new Label("Password") , g);
g.gridx=1;
g.gridy=0;
f.add(new TextField(15) , g);
g.gridy=1;
g.gridx=1;
f.add(new TextField(15) , g);
g.gridx=0;
g.gridy=2;
g.gridwidth=2;
g.insets = new Insets(30,0,0,0);
f.add(new Button("OK"),g);
f.setVisible(true);
}
}
As GridBagLayout divides a container into a grid of equally sizwd cells and it requires lot of information to know where to put a component that's why i used gridx/y ,insets etc for proper arrangements of your labels and texfields.
[让你的 java awt 概念基础扎实][1]
[1]: https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html