java swing中标签和按钮排列问题 swingjava 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用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); } }} 重写equals和hashCode JAVA权限管理系统 网络编程的问题 布局问题,高手看看怎么回事。 菜鸟求java程序!!!急!!!! java如何向ms sql2000的存储过程传递参数? 在java中实现宏替换功能. 如何判断一个java程序的另一个实例正在运行? 如何在lotus domino中调用applet的method? 文件流的删除修改问题 Socket接收输入流readLine() 求排序
示例: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);
}
}
}