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);// 窗体居中 // public void paintComponent(Graphics g) {
// Graphics2D g2 = (Graphics2D)g;
// // super.paintComponent(g);
// Image img =
// Toolkit.getDefaultToolkit().getImage("D:\\images\\02.jpg");
// g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
// }
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);
// panel.add(field6); // dispaly.setVisible(false);
// panel.add(dispaly);
// gridLayout.removeLayoutComponent(button);
// gridLayout.removeLayoutComponent(canel); // JPanel panel2 = new JPanel();
// GridBagConstraints c2 = new GridBagConstraints();
// c2.gridx = 50;
// c2.gridy =50;
// c2.gridwidth = 4;
// c2.gridheight = 10;
// c2.weightx = 20;
// c2.weighty = 0.5;
// c2.anchor = GridBagConstraints.SOUTH;
// c2.fill = GridBagConstraints.HORIZONTAL;
// panel2.add(canel,c2);
// panel2.add(button,c2);
// panel2.add(canel,c2);
// panel2.setVisible(true);
// GridBagLayout bagLayout = new GridBagLayout();
// bagLayout.getConstraints(button); 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);我写的控制好像起不到作用。

解决方案 »

  1.   

    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]);
    } } });
    // 初始化系选项为-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();
    }
    }
      

  2.   

    我想把 姓名这个label这列 和 文本框的距离缩短,怎么办啊?
      

  3.   

    真是的。你可以把你的问题描述清楚,让别人知道如何去帮你,你的疑点没有。你想让别人花时间来读你整个流程是需要时间的。1搂的也告诉你了代码也贴完全,以为下面你的完全的,ComboBoxText这个对象是你自己建的吧。
      

  4.   

    没问题啊
    ComboBoxText 它不是我建的。
    我想把 姓名这个label这列 和 文本框的距离缩短。
    就这个问题啊