swing中网格合并 在swing的网格布局中怎样合并两个单元格? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 怎么用GridBagLayout ,比如说我想合并第三行的三列,然后让第三行的buttion居中。 看不懂,我要的是swing中网格布局的合并。 import java.awt.Container;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Image;import java.awt.Insets;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.Date;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.Transaction;import com.sessionFacrory.HibernateSessionFactory;public class Save extends JFrame {JTextField field1 = new JTextField(10);JComboBox box = new JComboBox();ComboBoxText ct = new ComboBoxText();JComboBox cb = new JComboBox();JComboBox ca = new JComboBox(ct.a());JTextField field5 = new JTextField(20);JTextField field6 = new JTextField(10);JButton button = new JButton("添加");JButton canel = new JButton("退出");JButton dispaly = new JButton("呵呵");JLabel label = new JLabel("姓名:");JLabel labe2 = new JLabel("性别:");JLabel labe3 = new JLabel("所在系:");JLabel labe4 = new JLabel("所在专业:");JLabel labe5 = new JLabel("地址:");JLabel labe6 = new JLabel("开始时间:");p1 p = new p1();Save() {super("学生信息添加");this.setSize(790, 140);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel panel = new JPanel();GridLayout gridLayout = new GridLayout();GridBagLayout bagLayout = new GridBagLayout();// /gridLayout.removeLayoutComponent(button);gridLayout.removeLayoutComponent(canel);panel.setLayout(gridLayout);JFrame f = new JFrame();Container contentPane = f.getContentPane();contentPane.setLayout(bagLayout);box.addItem("男");box.addItem("女");box.setSelectedIndex(-1);// Toolkit kit=Toolkit.getDefaultToolkit();//设置顶层容器框架为居中Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();Dimension frm = this.getSize();setLocation((scr.width - frm.width) / 2, (scr.height - frm.height) / 2);// 窗体居中GridBagConstraints c2 = new GridBagConstraints();c2.insets = new Insets(20, 20, 20, 20);c2.gridx = 0;c2.gridy = 0;c2.gridwidth = 4;c2.gridheight = 10;c2.weightx = 0;c2.weighty = 1.5;c2.anchor = GridBagConstraints.WEST;c2.fill = GridBagConstraints.NONE;bagLayout.setConstraints(label, c2);panel.add(label, c2);// panel.add(label);c2.insets = new Insets(0, 0, 0, 0);c2.gridx = 0;c2.gridy = 0;c2.gridwidth = 4;c2.gridheight = 2;c2.weightx = 10;c2.weighty = 0.5;c2.anchor = GridBagConstraints.WEST;c2.fill = GridBagConstraints.NONE;// bagLayout.setConstraints(field1, c2);panel.add(field1, c2);// panel.add(field1);c2.insets = new Insets(0, 0, 0, 0);c2.gridx = 0;c2.gridy = 0;c2.gridwidth = 4;c2.gridheight = 5;c2.weightx = 20;c2.weighty = 0.5;c2.anchor = GridBagConstraints.WEST;c2.fill = GridBagConstraints.NONE;bagLayout.setConstraints(labe2, c2);panel.add(labe2, c2);// panel.add(labe2);c2.insets = new Insets(0, 0, 0, 0);c2.gridx = 0;c2.gridy = 0;c2.gridwidth = 4;c2.gridheight = 10;c2.weightx = 10;c2.weighty = 0.5;c2.anchor = GridBagConstraints.WEST;c2.fill = GridBagConstraints.NONE;bagLayout.setConstraints(box, c2);panel.add(box, c2);// panel.add(box);c2.insets = new Insets(0, 0, 0, 0);c2.gridx = 0;c2.gridy = 0;c2.gridwidth = 4;c2.gridheight = 5;c2.weightx = 20;c2.weighty = 0.5;c2.anchor = GridBagConstraints.WEST;c2.fill = GridBagConstraints.NONE;// bagLayout.setConstraints(labe3, c2);panel.add(labe3, c2);// panel.add(labe3);c2.insets = new Insets(0, 0, 0, 0);c2.gridx = 0;c2.gridy = 0;c2.gridwidth = 4;c2.gridheight =5;c2.weightx = 20;c2.weighty = 0.5;c2.anchor = GridBagConstraints.WEST;c2.fill = GridBagConstraints.NONE;bagLayout.setConstraints(ca, c2);panel.add(ca, c2);// panel.add(ca);c2.insets = new Insets(0, 0, 0, 0);c2.gridx = 0;c2.gridy = 0;c2.gridwidth = 4;c2.gridheight = 6;c2.weightx = 20;c2.weighty = 0.5;c2.anchor = GridBagConstraints.WEST;c2.fill = GridBagConstraints.NONE;bagLayout.setConstraints(labe4, c2);panel.add(labe4, c2);// panel.add(labe4);c2.insets = new Insets(0, 0, 0, 0);c2.gridx = 0;c2.gridy = 0;c2.gridwidth = 20;c2.gridheight = 5;c2.weightx = 10;c2.weighty = 0.5;c2.anchor = GridBagConstraints.WEST;c2.fill = GridBagConstraints.NONE;bagLayout.setConstraints(cb, c2);panel.add(cb, c2);// panel.add(cb);c2.insets = new Insets(0, 0, 0, 0);c2.gridx = 0;c2.gridy = 0;c2.gridwidth = 20;c2.gridheight = 5;c2.weightx = 10;c2.weighty = 0.5;c2.anchor = GridBagConstraints.WEST;c2.fill = GridBagConstraints.NONE;bagLayout.setConstraints(labe5, c2);panel.add(labe5, c2);// panel.add(labe5);c2.insets = new Insets(0, 0, 0, 0);c2.gridx = 0;c2.gridy = 0;c2.gridwidth = 4;c2.gridheight = 10;c2.weightx = 10;c2.weighty = 0.5;c2.anchor = GridBagConstraints.WEST;c2.fill = GridBagConstraints.NONE;bagLayout.setConstraints(field5, c2);panel.add(field5, c2);// panel.add(field5);c2.insets = new Insets(0, 0, 0, 0);c2.gridx = 0;c2.gridy = 0;c2.gridwidth = 4;c2.gridheight = 10;c2.weightx = 10;c2.weighty = 0.5;c2.anchor = GridBagConstraints.WEST;c2.fill = GridBagConstraints.NONE;bagLayout.setConstraints(labe6, c2);panel.add(labe6, c2);// panel.add(labe6);c2.insets = new Insets(0, 0, 0, 0);c2.gridx = 0;c2.gridy = 0;c2.gridwidth = 4;c2.gridheight = 10;c2.weightx = 0;c2.weighty = 0.5;c2.anchor = GridBagConstraints.WEST;c2.fill = GridBagConstraints.NONE;bagLayout.setConstraints(field6, c2);panel.add(field6, c2);button.setVisible(false);// panel.add(dispaly);// dispaly.setVisible(false);panel.add(button);// panel.add(canel);panel.add(p);c2.fill = GridBagConstraints.NONE;this.setContentPane(panel);Container c = new Container();c.remove(button);c.remove(canel);gridLayout.setColumns(8);gridLayout.setRows(4);gridLayout.setHgap(2);gridLayout.setVgap(2);c.setLayout(gridLayout);// 下拉框处理ca.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {// 清空选项框cb.removeAllItems();// 得到系的选项Object selected = ca.getSelectedItem();// 给专业赋值Object[] newItems = ct.b(selected);for (int i = 0; i < newItems.length; i++) {cb.addItem(newItems[i]);}}});// 初始化系选项为-1ca.setSelectedIndex(-1);}public class p1 extends JPanel {p1() {JButton das = new JButton("添加");das.setSize(50, 50);JButton das2 = new JButton("取消");super.add(das);super.add(das2);}}public static void main(String[] args) {Save save = new Save();save.show();}}我想把 姓名这个label这列 和 文本框的距离缩短,怎么办啊? http://www.iteye.com/topic/740310 http://www.iteye.com/topic/740310 关于package的路径问题,新手提问 字符串处理 java链表的节点类疑惑 讨论关于上次的提问!~~~~~~ java 应用程序只运行一次的问题 关于Swing中的KeyListener的问题?困扰了我好久了! 运行Applet的客户端的JRE 版本问题 工具栏和菜单栏显示的问题 String转换辉byte 求大神解释错误原因。 正则表达式--possessive问题 问一下正则表达式的效率
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.sessionFacrory.HibernateSessionFactory;public class Save extends JFrame {JTextField field1 = new JTextField(10);JComboBox box = new JComboBox();ComboBoxText ct = new ComboBoxText();JComboBox cb = new JComboBox();JComboBox ca = new JComboBox(ct.a());JTextField field5 = new JTextField(20);JTextField field6 = new JTextField(10);JButton button = new JButton("添加");JButton canel = new JButton("退出");JButton dispaly = new JButton("呵呵");JLabel label = new JLabel("姓名:");JLabel labe2 = new JLabel("性别:");JLabel labe3 = new JLabel("所在系:");JLabel labe4 = new JLabel("所在专业:");JLabel labe5 = new JLabel("地址:");JLabel labe6 = new JLabel("开始时间:");p1 p = new p1();Save() {
super("学生信息添加");
this.setSize(790, 140);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
GridLayout gridLayout = new GridLayout();
GridBagLayout bagLayout = new GridBagLayout();
// /gridLayout.removeLayoutComponent(button);
gridLayout.removeLayoutComponent(canel);
panel.setLayout(gridLayout);
JFrame f = new JFrame();
Container contentPane = f.getContentPane();
contentPane.setLayout(bagLayout);
box.addItem("男");
box.addItem("女");
box.setSelectedIndex(-1);
// Toolkit kit=Toolkit.getDefaultToolkit();//设置顶层容器框架为居中
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
setLocation((scr.width - frm.width) / 2, (scr.height - frm.height) / 2);// 窗体居中
GridBagConstraints c2 = new GridBagConstraints();
c2.insets = new Insets(20, 20, 20, 20);
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 4;
c2.gridheight = 10;
c2.weightx = 0;
c2.weighty = 1.5;
c2.anchor = GridBagConstraints.WEST;
c2.fill = GridBagConstraints.NONE;
bagLayout.setConstraints(label, c2);
panel.add(label, c2);// panel.add(label);
c2.insets = new Insets(0, 0, 0, 0);
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 4;
c2.gridheight = 2;
c2.weightx = 10;
c2.weighty = 0.5;
c2.anchor = GridBagConstraints.WEST;
c2.fill = GridBagConstraints.NONE;
// bagLayout.setConstraints(field1, c2);
panel.add(field1, c2);
// panel.add(field1);c2.insets = new Insets(0, 0, 0, 0);
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 4;
c2.gridheight = 5;
c2.weightx = 20;
c2.weighty = 0.5;
c2.anchor = GridBagConstraints.WEST;
c2.fill = GridBagConstraints.NONE;
bagLayout.setConstraints(labe2, c2);
panel.add(labe2, c2);
// panel.add(labe2);
c2.insets = new Insets(0, 0, 0, 0);
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 4;
c2.gridheight = 10;
c2.weightx = 10;
c2.weighty = 0.5;
c2.anchor = GridBagConstraints.WEST;
c2.fill = GridBagConstraints.NONE;
bagLayout.setConstraints(box, c2);
panel.add(box, c2);
// panel.add(box);c2.insets = new Insets(0, 0, 0, 0);
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 4;
c2.gridheight = 5;
c2.weightx = 20;
c2.weighty = 0.5;
c2.anchor = GridBagConstraints.WEST;
c2.fill = GridBagConstraints.NONE;
// bagLayout.setConstraints(labe3, c2);
panel.add(labe3, c2);
// panel.add(labe3);
c2.insets = new Insets(0, 0, 0, 0);
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 4;
c2.gridheight =5;
c2.weightx = 20;
c2.weighty = 0.5;
c2.anchor = GridBagConstraints.WEST;
c2.fill = GridBagConstraints.NONE;
bagLayout.setConstraints(ca, c2);
panel.add(ca, c2);
// panel.add(ca);
c2.insets = new Insets(0, 0, 0, 0);
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 4;
c2.gridheight = 6;
c2.weightx = 20;
c2.weighty = 0.5;
c2.anchor = GridBagConstraints.WEST;
c2.fill = GridBagConstraints.NONE;
bagLayout.setConstraints(labe4, c2);
panel.add(labe4, c2);
// panel.add(labe4);
c2.insets = new Insets(0, 0, 0, 0);
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 20;
c2.gridheight = 5;
c2.weightx = 10;
c2.weighty = 0.5;
c2.anchor = GridBagConstraints.WEST;
c2.fill = GridBagConstraints.NONE;
bagLayout.setConstraints(cb, c2);
panel.add(cb, c2);
// panel.add(cb);
c2.insets = new Insets(0, 0, 0, 0);
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 20;
c2.gridheight = 5;
c2.weightx = 10;
c2.weighty = 0.5;
c2.anchor = GridBagConstraints.WEST;
c2.fill = GridBagConstraints.NONE;
bagLayout.setConstraints(labe5, c2);
panel.add(labe5, c2);
// panel.add(labe5);
c2.insets = new Insets(0, 0, 0, 0);
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 4;
c2.gridheight = 10;
c2.weightx = 10;
c2.weighty = 0.5;
c2.anchor = GridBagConstraints.WEST;
c2.fill = GridBagConstraints.NONE;
bagLayout.setConstraints(field5, c2);
panel.add(field5, c2);
// panel.add(field5);
c2.insets = new Insets(0, 0, 0, 0);
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 4;
c2.gridheight = 10;
c2.weightx = 10;
c2.weighty = 0.5;
c2.anchor = GridBagConstraints.WEST;
c2.fill = GridBagConstraints.NONE;
bagLayout.setConstraints(labe6, c2);
panel.add(labe6, c2);
// panel.add(labe6);
c2.insets = new Insets(0, 0, 0, 0);
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 4;
c2.gridheight = 10;
c2.weightx = 0;
c2.weighty = 0.5;
c2.anchor = GridBagConstraints.WEST;
c2.fill = GridBagConstraints.NONE;
bagLayout.setConstraints(field6, c2);
panel.add(field6, c2);button.setVisible(false);
// panel.add(dispaly);
// dispaly.setVisible(false);
panel.add(button);
// panel.add(canel);
panel.add(p);
c2.fill = GridBagConstraints.NONE;
this.setContentPane(panel);
Container c = new Container();
c.remove(button);
c.remove(canel);
gridLayout.setColumns(8);
gridLayout.setRows(4);
gridLayout.setHgap(2);
gridLayout.setVgap(2);
c.setLayout(gridLayout);
// 下拉框处理
ca.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 清空选项框
cb.removeAllItems();
// 得到系的选项
Object selected = ca.getSelectedItem();
// 给专业赋值
Object[] newItems = ct.b(selected);
for (int i = 0; i < newItems.length; i++) {
cb.addItem(newItems[i]);
}}});
// 初始化系选项为-1
ca.setSelectedIndex(-1);}public class p1 extends JPanel {
p1() {
JButton das = new JButton("添加");
das.setSize(50, 50);
JButton das2 = new JButton("取消");
super.add(das);
super.add(das2);}
}public static void main(String[] args) {Save save = new Save();
save.show();
}
}
我想把 姓名这个label这列 和 文本框的距离缩短,怎么办啊?