
java中的GridBagLayout是怎么调组件间距的...
我一开始已经设置好是默认值的了,现在想直接通过方法的调用来设置间隔。。。怎么弄?GridBagLayoutgb=newGridBagLayout();GridBagCon...
我一开始已经设置好是默认值的了,现在想直接通过方法的调用来设置间隔。。。怎么弄?
GridBagLayout gb = new GridBagLayout(); GridBagConstraints gc2 = new GridBagConstraints(); jpL1.setLayout(gb); gc2.fill = GridBagConstraints.BOTH; gc2.weightx =1.8; gc2.weighty = 1; jpL1.add(jpL12,gc2); gc2.weightx=2; gc2.gridwidth = GridBagConstraints.REMAINDER; jpL1.add(jpL13,gc2); 展开
GridBagLayout gb = new GridBagLayout(); GridBagConstraints gc2 = new GridBagConstraints(); jpL1.setLayout(gb); gc2.fill = GridBagConstraints.BOTH; gc2.weightx =1.8; gc2.weighty = 1; jpL1.add(jpL12,gc2); gc2.weightx=2; gc2.gridwidth = GridBagConstraints.REMAINDER; jpL1.add(jpL13,gc2); 展开
展开全部
java中的GridBagLayout调组件间距使用百分比来调,实例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | package JavaGUI; import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.UIManager; public class GridBagLayoutDemo extends JFrame { /** * */ private static final long serialVersionUID = -4481121176026056530L; private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { try { UIManager .setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" ); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater( new Runnable() { public void run() { try { GridBagLayoutDemo frame = new GridBagLayoutDemo(); frame.setVisible( true ); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public GridBagLayoutDemo() { setTitle( "网格组布局" ); // 设置窗体的标题 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体退出时操作 setBounds( 100 , 100 , 450 , 200 ); // 设置窗体位置和大小 contentPane = new JPanel(); // 创建内容面板 contentPane.setBorder( new EmptyBorder( 5 , 5 , 5 , 5 )); // 设置面板的边框 setContentPane(contentPane); // 应用内容面板 GridBagLayout gbl_contentPane = new GridBagLayout(); // 创建网格组布局 contentPane.setLayout(gbl_contentPane); // 使用网格组布局 JButton button1 = new JButton( "A" ); // 创建按钮 GridBagConstraints gbc_button1 = new GridBagConstraints(); // 创建网格组布局约束条件 gbc_button1.insets = new Insets( 0 , 0 , 5 , 5 ); // 设置控件的空白 gbc_button1.fill = GridBagConstraints.HORIZONTAL; // 设置填充方式 gbc_button1.weightx = 10.0 ; // 第一列的分布方式为10% gbc_button1.gridx = 0 ; // 起始点为第1列 gbc_button1.gridy = 0 ; // 起始点为第1行 contentPane.add(button1, gbc_button1); // 增加按钮及其约束条件 JButton button2 = new JButton( "B" ); // 创建按钮 GridBagConstraints gbc_button2 = new GridBagConstraints(); // 创建网格组布局约束条件 gbc_button2.insets = new Insets( 0 , 5 , 5 , 5 ); // 设置控件的空白 gbc_button2.fill = GridBagConstraints.HORIZONTAL; // 设置填充方式 gbc_button2.weightx = 20.0 ; // 第一列的分布方式为20% gbc_button2.gridx = 1 ; // 起始点为第2列 gbc_button2.gridy = 0 ; // 起始点为第1行 contentPane.add(button2, gbc_button2); // 增加按钮及其约束条件 JButton button3 = new JButton( "C" ); // 创建按钮 GridBagConstraints gbc_button3 = new GridBagConstraints(); // 创建网格组布局约束条件 gbc_button3.gridheight = 2 ; // 占用2列 gbc_button3.fill = GridBagConstraints.BOTH; // 设置填充方式 gbc_button3.weightx = 30.0 ; // 第一列的分布方式为30% gbc_button3.insets = new Insets( 0 , 0 , 5 , 5 ); // 设置控件的空白 gbc_button3.gridx = 2 ; // 起始点为第3列 gbc_button3.gridy = 0 ; // 起始点为第1行 contentPane.add(button3, gbc_button3); // 增加按钮及其约束条件 JButton button4 = new JButton( "D" ); // 创建按钮 GridBagConstraints gbc_button4 = new GridBagConstraints(); // 创建网格组布局约束条件 gbc_button4.weightx = 40.0 ; // 第一列的分布方式为40% gbc_button4.fill = GridBagConstraints.BOTH; // 设置填充方式 gbc_button4.gridheight = 4 ; // 占用4列 gbc_button4.insets = new Insets( 0 , 5 , 0 , 0 ); // 设置控件的空白 gbc_button4.gridx = 3 ; // 起始点为第4列 gbc_button4.gridy = 0 ; // 起始点为第1行 contentPane.add(button4, gbc_button4); // 增加按钮及其约束条件 } } |
追问
我想要调的是各组间之间的距离,谢谢你!
追答
这样啊~~~抱歉
我很少用GridBagLayout的
如果界面布局不规则我一般是用absolute layout
这个布局的~~~
展开全部
| 分类:JAVA相关 | 浏览2次
我一开始已经设置好是默认值的了,现在想直接通过方法的调用来设置间隔。。。怎么弄? GridBagLayout gb = new GridBagLayout(); GridBagConstraints gc2 = new GridBagConstraints(); jpL1.setLayout(gb); gc2.fill = GridBagConstraints.BOTH; gc2.weightx =1.8; gc2.weighty = 1; jpL1.add(jpL12,gc2); gc2.weight
我一开始已经设置好是默认值的了,现在想直接通过方法的调用来设置间隔。。。怎么弄? GridBagLayout gb = new GridBagLayout(); GridBagConstraints gc2 = new GridBagConstraints(); jpL1.setLayout(gb); gc2.fill = GridBagConstraints.BOTH; gc2.weightx =1.8; gc2.weighty = 1; jpL1.add(jpL12,gc2); gc2.weight
本回答被提问者和网友采纳
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询