使用gridbaglayout布局方式的疑问
利用GridBagLayout布局方式进行布局。想让表格显示在左边,占9*9的格子。想让2个按钮显示在(11,7)和(11,9)的位置。为什么编程后,两个按钮的位置不对,其中“添加”按钮跑到最上面了?为什么会出现这种情况?想达到自己想要的效果该如何编写代码呢?还请多多指教。
注:配图右图是我想要的效果,图里面的数值是我标注的坐标,和实际编程后出现的效果(左图)。


注:配图右图是我想要的效果,图里面的数值是我标注的坐标,和实际编程后出现的效果(左图)。


import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class ExampleFrame_0333 extends JFrame {
private JButton okbutton;
private JButton addbutton;
private JTable table;
public static void main(String args[]) {
ExampleFrame_0333 frame = new ExampleFrame_0333();
frame.setVisible(true);
}
public ExampleFrame_0333() {
super();
setTitle("使用网格组布局管理器");
getContentPane().setLayout(new GridBagLayout());
setBounds(100, 100, 500, 370);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table=new JTable();
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setEnabled(true);
DefaultTableModel dftm=(DefaultTableModel) table.getModel();
dftm.setColumnIdentifiers(new String[] {"型号","线别","站位","原始部番"});
dftm.addRow(new Vector());
table.setModel(dftm);
JScrollPane scrollpane=new JScrollPane(table);
GridBagConstraints gridbagconstraints_1=new GridBagConstraints();
gridbagconstraints_1.gridx=0;
gridbagconstraints_1.gridy=0;
gridbagconstraints_1.gridwidth=9;
gridbagconstraints_1.gridheight=9;
gridbagconstraints_1.weightx=1.0;
gridbagconstraints_1.weighty=1.0;
gridbagconstraints_1.fill=GridBagConstraints.BOTH;
gridbagconstraints_1.insets=new Insets(3,3,3,3);
getContentPane().add(scrollpane,gridbagconstraints_1);
addbutton=new JButton("添加");
GridBagConstraints gridbagconstraints_2=new GridBagConstraints();
gridbagconstraints_2.gridx=11;
gridbagconstraints_2.gridy=7;
gridbagconstraints_2.insets=new Insets(3,3,3,3);
getContentPane().add(addbutton,gridbagconstraints_2);
addbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DefaultTableModel model=(DefaultTableModel) table.getModel();
model.addRow(new Vector());
}
});
okbutton=new JButton("完成");
GridBagConstraints gridbagconstraints_3=new GridBagConstraints();
gridbagconstraints_3.gridx=11;
gridbagconstraints_3.gridy=9;
gridbagconstraints_3.insets=new Insets(3,3,3,3);
gridbagconstraints_1.fill=GridBagConstraints.VERTICAL;
getContentPane().add(okbutton,gridbagconstraints_3);
}
}
...全文
相关推荐
java中Swing五种常见的布局方式
在使用布局方式时,需要根据实际情况选择合适的布局方式,并注意布局方式的优缺点。
Java中GridBagLayout的用法
### Java中GridBagLayout的用法详解#### 一、GridBagLayout简介GridBagLayout是一种高度灵活且功能强大的布局管理器,在Java Swing图形用户界面编程中被广泛使用
GridBagLayout布局详解
GridBagLayout 布局详解GridBagLayout 是 Java Swing 编程中的一种强大且灵活的布局管理器,它可以实现几乎所有你想要的布局。
Java Swing GridBagLayout网格袋布局的实现
GridBagLayout使用动态的矩形单元格来计算单元格的大小,每个组件占用一个或多个单元格。3. GridBagLayout提供了多种约束属性来控制组件的布局,包括显示区域约束、组件约束等。
GridBagLayout布局器的使用
Java Swing GridBagLayout,很详细的Demo,对GridBagLayout如何使用做全面的讲解


Java SE
