Java如何对按钮和标签排列,java swing中标签和按钮排列有关问题

java swing中标签和按钮排列问题

public JLabel label = new JLabel("最高记录: ");

public JLabel labe2 = new JLabel("当前分数: ");

protected JButton  Jchongxin = new JButton("重新开始");

protected JButton Jpaihang = new JButton("排行榜");

protected JButton Jhuiqi = new JButton("悔棋");

protected JButton  Jtuichu  = new JButton("退出游戏");

protected JLabel labe3 = new JLabel("下一组棋子");

protected JTextArea JTxiayizu  = new JTextArea();

以上代码中的标签,按钮,文本框。请问如何进行一行一个排成一列?万分感谢!

------解决方案--------------------

用GridBagLayout布局就可以了;

示例:

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextArea;

import javax.swing.SwingUtilities;

public class GridBagLayoutDemo {

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

new GridBagLayoutDemo().new MyFrame();

}

});

}

class MyFrame extends JFrame{

private static final long serialVersionUID = 1L;

protected JLabel label = new JLabel("最高记录: ");

protected JLabel labe2 = new JLabel("当前分数: ");

protected JButton  Jchongxin = new JButton("重新开始");

protected JButton Jpaihang = new JButton("排行榜");

protected JButton Jhuiqi = new JButton("悔棋");

protected JButton  Jtuichu  = new JButton("退出游戏");

protected JLabel labe3 = new JLabel("下一组棋子");

protected JTextArea JTxiayizu  = new JTextArea();

public MyFrame(){

setTitle("A Test Demo");

setDefaultCloseOperation(EXIT_ON_CLOSE);

setBounds(100, 100, 200, 350);

GridBagLayout  gly = new GridBagLayout();

addComponent(0, 0,label, gly);

addComponent(1, 0,labe2, gly);

addComponent(2, 0,Jchongxin, gly);

addComponent(3, 0,Jpaihang, gly);

addComponent(4, 0,Jhuiqi, gly);

addComponent(5, 0,Jtuichu, gly);

addComponent(6, 0, JTxiayizu, gly);

setLayout(gly);

setVisible(true);

}

private void addComponent(int low, int column, JComponent compoent, GridBagLayout  gly){

GridBagConstraints c = new GridBagConstraints();

c.insets = new Insets(10, 10, 10, 10);

c.gridx = column;

c.gridy = low;

c.fill = GridBagConstraints.BOTH;

gly.setConstraints(compoent, c);

add(compoent);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值