用JAVA编写代码,实现一个窗口,窗口上有“姓名”“性别”“年龄”等标签,标签后分别对应3个文本框。
1个回答
展开全部
这个是网格布局,教程在这里:http://www.java2s.com/CN/Tutorial/Java/0240__Swing/Usesa2columngrid.htm
下面是我改自教程的,实现了你的要求:
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridLayoutDemo {
public final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
pane.setLayout(new GridLayout(0, 2));
pane.add(new JLabel("姓名"));
pane.add(new JTextField());
pane.add(new JLabel("性别"));
pane.add(new JTextField());
pane.add(new JLabel("年龄"));
pane.add(new JTextField());
}
public static void main(String[] args) {
JFrame frame = new JFrame("GridLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
}
下面是我改自教程的,实现了你的要求:
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridLayoutDemo {
public final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
pane.setLayout(new GridLayout(0, 2));
pane.add(new JLabel("姓名"));
pane.add(new JTextField());
pane.add(new JLabel("性别"));
pane.add(new JTextField());
pane.add(new JLabel("年龄"));
pane.add(new JTextField());
}
public static void main(String[] args) {
JFrame frame = new JFrame("GridLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询