大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
写的时候仔细点,setLyaout方法里面有错
网站制作、网站设计的开发,更需要了解用户,从用户角度来建设网站,获得较好的用户体验。创新互联公司多年互联网经验,见的多,沟通容易、能帮助客户提出的运营建议。作为成都一家网络公司,打造的就是网站建设产品直销的概念。选择创新互联公司,不只是建站,我们把建站作为产品,不断的更新、完善,让每位来访用户感受到浩方产品的价值服务。
lbUser=new JLabel("用户名");
gbLayout.setConstraints(tfUser,constraints);
container.add(tfUser);
你这里把 lbUser 指向了对象,此是的tfUser还没有,而你加的时候确是加的tfUser, 你把 tfUser改过来就好了
import javax.swing.*;
import java.awt.*;
public class GridBagLayoutDemo extends JFrame {
private GridBagLayout gbLayout = new GridBagLayout();
private GridBagConstraints constraints = new GridBagConstraints();
private JLabel lbUser, lbPassword;
private JTextField tfUser, tfPassword;
private JButton btnLog;
private Container container;
public GridBagLayoutDemo() {
super("网格包布局");
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setConstraints(GridBagConstraints gbc, int row, int column,
int numRows, int numColumns, int Weightx, int Weighty) {
gbc.gridx = row;
gbc.gridy = column;
gbc.gridwidth = numRows;
gbc.gridheight = numColumns;
gbc.weightx = Weightx;
gbc.weighty = Weighty;
}
public void setLyaout() {
container = this.getContentPane();
container.setLayout(gbLayout);
// 添加用户名标签
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.CENTER;
setConstraints(constraints, 0, 0, 1, 1, 0, 0);
lbUser = new JLabel("用户名");
gbLayout.setConstraints(lbUser, constraints);
container.add(lbUser);
// 添加用户名文本框
constraints.fill = GridBagConstraints.HORIZONTAL;
setConstraints(constraints, 1, 0, 1, 1, 100, 100);
tfUser = new JTextField();
gbLayout.setConstraints(tfUser, constraints);
container.add(tfUser);
// 添加密码标签
constraints.fill = GridBagConstraints.NONE;
setConstraints(constraints, 0, 1, 1, 1, 0, 0);
lbPassword = new JLabel("密码");
gbLayout.setConstraints(lbPassword, constraints);
container.add(lbPassword);
// 添加密码文本框
constraints.fill = GridBagConstraints.HORIZONTAL;
setConstraints(constraints, 1, 1, 1, 1, 100, 100);
tfPassword = new JTextField();
gbLayout.setConstraints(tfPassword, constraints);
container.add(tfPassword);
// 添加登录按钮
constraints.fill = GridBagConstraints.CENTER;
setConstraints(constraints, 0, 2, 2, 1, 0, 0);
btnLog = new JButton("登录");
gbLayout.setConstraints(btnLog, constraints);
container.add(btnLog);
}
public static void main(String[] args) {
GridBagLayoutDemo frame = new GridBagLayoutDemo();
frame.setLyaout();
frame.show();
}
}
之前有人做过用Table来实现一个表单的效果,其中也有跨多行和多列,是用纯java代码实现的,没有使用XML。用网格布局GridView也可以实现
效果图
参考代码
import java.awt.*;
import javax.swing.*;
public class GridFrame extends JFrame {
// 指定横向格子数量
static final int W_NUM = 3;
// 指定竖向格子数量
static final int H_NUM = 3;
// 指定每个格子的宽
static final int CELL_WIDTH = 50;
// 指定每个格子的高
static final int CELL_HEIGHT = 50;
CellPanel cp;
public GridFrame() {
cp = new CellPanel();
this.add(cp);
this.setBounds(350, 100, W_NUM * CELL_WIDTH+15 , H_NUM * CELL_HEIGHT
+ 40);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("网格窗口");
this.setVisible(true);
}
public static void main(String[] args) {
new GridFrame();
}
class CellPanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i H_NUM; i++) {
for (int j = 0; j W_NUM; j++) {
g.setColor(new Color((int) (Math.random() * 256),
(int) (Math.random() * 256),
(int) (Math.random() * 256)));
g.fill3DRect(j * CELL_WIDTH, i * CELL_HEIGHT, CELL_WIDTH,
CELL_HEIGHT, true);
}
}
}
}
}