在GridLayout中的标签之间放置空格

时间:2017-12-13 06:20:33

标签: java jframe

package committeeGUI;

import static committeeGUI.CommitteeGUI.comList;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class StudentMemberFrame extends JFrame {

    public StudentMemberFrame() {
        super("Add Student");
        setSize(450, 500);
        setLocation(561, 150);
        super.setResizable(false);
        addStudentMember();
    }

    public void addStudentMember() {
        CommitteeGUI.frame.setEnabled(false);

        final JPanel showConsoleArea = new JPanel(new FlowLayout(FlowLayout.LEFT));
        showConsoleArea.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        //creating border and size of the border 
        showConsoleArea.setBorder(BorderFactory.createLineBorder(Color.black, 3));

        add(showConsoleArea); //, BorderLayout.CENTER); 

        //setting a size to showConsoleArea.
        showConsoleArea.setSize(500, 500);

        final JLabel lblheading = new JLabel("STUDENT");

        //   showConsoleArea.add(lblheading,BorderLayout.CENTER);
        /*
         * creating components  of company form
         */
        final JLabel lblCommitteeName = new JLabel("Committee name");
        final JTextField txtName = new JTextField(15);

        final JLabel lblMemberName = new JLabel("Student name");
        final JTextField txtMemberName = new JTextField(15);

        final JLabel lblMemberNumber = new JLabel("Student number");
        final JTextField txtMemberNumber = new JTextField(15);

        final JLabel lblMemberCourse = new JLabel("Student course");
        final JTextField txtMemberCourse = new JTextField(15);

        final JButton buttAdd = new JButton("SAVE");
        final JButton buttCancel = new JButton("CANCEL");

        // adding components to the display area 
        c.gridx = 1;
        c.gridy = 0;
        showConsoleArea.add(lblheading, c);

        c.gridx = 0;
        c.gridy = 1;
        showConsoleArea.add(lblCommitteeName, c);
        c.gridx = 1;
        c.gridy = 1;
        showConsoleArea.add(txtName, c);

        c.gridx = 0;
        c.gridy = 2;
        showConsoleArea.add(lblMemberName, c);
        c.gridx = 1;
        c.gridy = 2;
        showConsoleArea.add(txtMemberName, c);

        c.gridx = 0;
        c.gridy = 3;
        showConsoleArea.add(lblMemberNumber, c);
        c.gridx = 1;
        c.gridy = 3;
        showConsoleArea.add(txtMemberNumber, c);

        c.gridx = 0;
        c.gridy = 4;
        showConsoleArea.add(lblMemberCourse, c);
        c.gridx = 1;
        c.gridy = 4;
        showConsoleArea.add(txtMemberCourse, c);

        c.gridx = 0;
        c.gridy = 5;
        showConsoleArea.add(buttAdd, c);
        c.gridx = 1;
        c.gridy = 5;
        showConsoleArea.add(buttCancel, c);

        /*
         * able to displaying the company frame
         */
        this.show();

        buttAdd.addActionListener((ActionEvent e) -> {
            if (txtName.getText().equals("")
                    || txtMemberName.getText().equals("")
                    || txtMemberNumber.getText().equals("")
                    || txtMemberCourse.getText().equals("")) //validating the data
            {
                CommitteeGUI.frame.setEnabled(false);
                setEnabled(false);
                messagebox("Enter a valid data", 0);
                return;
            }
            if (!txtMemberNumber.getText().matches("\\d+")) {
                CommitteeGUI.frame.setEnabled(false);
                setEnabled(false);
                messagebox("Member number must be a integer", 0);
                return;
            }

            for (Committee com : comList) {
                if (com.getName().equals(txtName.getText())) {
                    Student st = new Student();
                    st.setName(txtMemberName.getText());
                    st.setAcademicNo(Integer.parseInt(txtMemberNumber.getText()));
                    st.setCourse(txtMemberCourse.getText());
                    com.memberList.add(st);
                    messagebox("Member added successfully", 1);
                    setEnabled(false);
                    return;
                }
            }
            messagebox("No Committee found with given name", 1);
        });

        //creating ActionListner to Cancel button
        buttCancel.addActionListener((ActionEvent e) -> {
            //frame is enabled for user.
            CommitteeGUI.frame.setEnabled(true);
            dispose();  //disposing the frame
        } //pass the action to actionPerformed method and perform it.
        );
    }

    @SuppressWarnings("deprecation")
    public void messagebox(String label, final int conform) {
        final JDialog infoBox = new JDialog();//message box 
        infoBox.setSize(400, 90);
        infoBox.setAlwaysOnTop(true);
        infoBox.setResizable(false);
        infoBox.setLocation(675, 258);

        JLabel space = new JLabel("                                                                         ");
        JLabel label1 = new JLabel(label);

        JButton buttOk = new JButton("Ok");
        buttOk.addActionListener((ActionEvent e) -> {
            if (conform == 1) {
                // making frame operation enable.
                CommitteeGUI.frame.setEnabled(true);
                dispose();
            }
            setEnabled(true);
            infoBox.hide();
        });

        JPanel holder = new JPanel(new FlowLayout());
        holder.add(label1);
        holder.add(buttOk);
        infoBox.add(holder);
        infoBox.show();

    }
}

以上是我的代码。我想在标题(STUDENT)和字段之间放置空格。

附件是框架的快照:screenshot 我不熟悉这种布局。非常感谢帮助。

1 个答案:

答案 0 :(得分:0)

public void addStudentMember(){

    final JPanel showConsoleArea = new JPanel(new FlowLayout(FlowLayout.LEFT));
    showConsoleArea.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    // Add below line of code change appropriate spacing
    c.insets = new Insets(10, 10, 10, 10);