2

私は Java GUI 構築のスプリング レイアウトは初めてですが、自分の GUI を思い通りにレイアウトしたい場合は、スプリング レイアウトを使用することが唯一の方法であると思われました。

私が抱えている問題は、私の GUI のパネルの 1 つが約 1 ピクセルまたは 2 ピクセル拡大することJFrameです。しかし、resizeable を true に設定すると、すべてが思いどおりに完璧に整列します。1 ピクセルまたは 2 ピクセルが大きくずれていないことはわかっていますが、一部のテキスト ボックスが他のテキスト ボックスよりも 1 ピクセルまたは 2 ピクセル大きく引き伸ばされているため、GUI が奇妙に見えるには十分です。これが私の春のレイアウトのコードです:

    pnlTop.setLayout(layout);

    lblClosetLoc.setText("Closet location:");
    lblPhone1.setText("Phone 1:");
    lblJackPaired.setText("Jack paired:");
    lblPhone2.setText("Phone 2:");
    lblCubicle.setText("Cubicle:");
    lblJackType.setText("Jack type:");
    lblName.setText("Name:");
    lblPhoneJack1.setText("Phone jack 1:");
    lblPhoneJack2.setText("Phone jack 2:");
    lblDataJack1.setText("Data jack 1:");
    lblDataJack2.setText("Data jack 2:");
    lblComments.setText("Comments:");

    txtClosetLoc.setEditable(false);
    txtClosetLoc.setText("");
    txtPhone1.setEditable(false);
    txtPhone1.setText("");
    txtJackPaired.setEditable(false);
    txtJackPaired.setText("");
    txtPhone2.setEditable(false);
    txtPhone2.setText("");
    txtCubicle.setEditable(false);
    txtCubicle.setText("");
    txtJackType.setEditable(false);
    txtJackType.setText("");
    txtFname.setEditable(false);
    txtFname.setText("");
    txtLname.setEditable(false);
    txtLname.setText("");
    txtPhoneJack1.setEditable(false);
    txtPhoneJack1.setText("");
    txtPhoneJack2.setEditable(false);
    txtPhoneJack2.setText("");
    txtDataJack1.setEditable(false);
    txtDataJack1.setText("");
    txtDataJack2.setEditable(false);
    txtDataJack2.setText("");
    txtComments.setEditable(false);
    txtComments.setText("");

    pnlTop.add(lblClosetLoc);
    pnlTop.add(txtClosetLoc);
    pnlTop.add(lblPhone1);
    pnlTop.add(txtPhone1);
    pnlTop.add(lblJackPaired);
    pnlTop.add(txtJackPaired);
    pnlTop.add(lblPhone2);
    pnlTop.add(txtPhone2);
    pnlTop.add(lblCubicle);
    pnlTop.add(txtCubicle);
    pnlTop.add(lblJackType);
    pnlTop.add(txtJackType);
    pnlTop.add(lblName);
    pnlTop.add(txtFname);
    pnlTop.add(txtLname);
    pnlTop.add(lblPhoneJack1);
    pnlTop.add(lblPhoneJack2);
    pnlTop.add(lblDataJack1);
    pnlTop.add(lblDataJack2);
    pnlTop.add(lblComments);
    pnlTop.add(txtPhoneJack1);
    pnlTop.add(txtPhoneJack2);
    pnlTop.add(txtDataJack1);
    pnlTop.add(txtDataJack2);
    pnlTop.add(txtComments);

    layout.putConstraint(layout.WEST, lblClosetLoc, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblClosetLoc, 1, layout.NORTH, txtClosetLoc);

    layout.putConstraint(layout.WEST, txtClosetLoc, 5, layout.EAST, lblClosetLoc);
    layout.putConstraint(layout.NORTH, txtClosetLoc, 5, layout.NORTH, pnlTop);

    layout.putConstraint(layout.WEST, lblPhone1, 0, layout.WEST, lblPhone2);
    layout.putConstraint(layout.NORTH, lblPhone1, 1, layout.NORTH, txtPhone1);

    layout.putConstraint(layout.WEST, txtPhone1, 0, layout.WEST, txtPhone2);
    layout.putConstraint(layout.NORTH, txtPhone1, 0, layout.NORTH, txtClosetLoc);

    layout.putConstraint(layout.WEST, lblJackPaired, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblJackPaired, 1, layout.NORTH, txtJackPaired);

    layout.putConstraint(layout.WEST, txtJackPaired, 22, layout.EAST, lblJackPaired);
    layout.putConstraint(layout.NORTH, txtJackPaired, 5, layout.SOUTH, txtClosetLoc);

    layout.putConstraint(layout.WEST, lblPhone2, 0, layout.WEST, lblJackType);
    layout.putConstraint(layout.NORTH, lblPhone2, 1, layout.NORTH, txtPhone2);

    layout.putConstraint(layout.WEST, txtPhone2, 0, layout.WEST, txtJackType);
    layout.putConstraint(layout.NORTH, txtPhone2, 0, layout.NORTH, txtJackPaired);

    layout.putConstraint(layout.WEST, lblCubicle, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblCubicle, 1, layout.NORTH, txtCubicle);

    layout.putConstraint(layout.WEST, txtCubicle, 47, layout.EAST, lblCubicle);
    layout.putConstraint(layout.NORTH, txtCubicle, 5, layout.SOUTH, txtJackPaired);

    layout.putConstraint(layout.WEST, lblJackType, 83, layout.EAST, txtCubicle);
    layout.putConstraint(layout.NORTH, lblJackType, 1, layout.NORTH, txtJackType);

    layout.putConstraint(layout.WEST, txtJackType, 0, layout.WEST, txtDataJack1);
    layout.putConstraint(layout.NORTH, txtJackType, 0, layout.NORTH, txtCubicle);

    layout.putConstraint(layout.WEST, lblName, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblName, 1, layout.NORTH, txtFname);

    layout.putConstraint(layout.WEST, txtFname, 56, layout.EAST, lblName);
    layout.putConstraint(layout.NORTH, txtFname, 5, layout.SOUTH, txtCubicle);

    layout.putConstraint(layout.WEST, txtLname, 5, layout.EAST, txtFname);
    layout.putConstraint(layout.NORTH, txtLname, 5, layout.SOUTH, txtCubicle);

    layout.putConstraint(layout.WEST, txtPhoneJack1, 0, layout.WEST, txtFname);
    layout.putConstraint(layout.NORTH, txtPhoneJack1, 5, layout.SOUTH, txtFname);

    layout.putConstraint(layout.WEST, txtPhoneJack2, 0, layout.WEST, txtPhoneJack1);
    layout.putConstraint(layout.NORTH, txtPhoneJack2, 5, layout.SOUTH, txtPhoneJack1);

    layout.putConstraint(layout.WEST, txtComments, 0, layout.WEST, txtPhoneJack2);
    layout.putConstraint(layout.NORTH, txtComments, 5, layout.SOUTH, txtPhoneJack2);

    layout.putConstraint(layout.WEST, txtDataJack1, 0, layout.WEST, txtDataJack2);
    layout.putConstraint(layout.NORTH, txtDataJack1, 5, layout.SOUTH, txtLname);

    layout.putConstraint(layout.WEST, lblPhoneJack1, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblPhoneJack1, 1, layout.NORTH, txtPhoneJack1);

    layout.putConstraint(layout.WEST, txtDataJack2, 5, layout.EAST, lblDataJack2);
    layout.putConstraint(layout.NORTH, txtDataJack2, 5, layout.SOUTH, txtDataJack1);

    layout.putConstraint(layout.WEST, lblPhoneJack2, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblPhoneJack2, 1, layout.NORTH, txtPhoneJack2);

    layout.putConstraint(layout.WEST, lblDataJack1, 0, layout.WEST, lblJackType);
    layout.putConstraint(layout.NORTH, lblDataJack1, 1, layout.NORTH, txtDataJack1);

    layout.putConstraint(layout.WEST, lblDataJack2, 0, layout.WEST, lblDataJack1);
    layout.putConstraint(layout.NORTH, lblDataJack2, 1, layout.NORTH, txtDataJack2);

    layout.putConstraint(layout.WEST, lblComments, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblComments, 1, layout.NORTH, txtComments);

    layout.putConstraint(layout.EAST, pnlTop, 5, layout.EAST, txtDataJack2);
    layout.putConstraint(layout.SOUTH, pnlTop, 5, layout.SOUTH, txtComments);

SSCCE :

スタートアップ クラス:

public class test1 {

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainFrame mFrame = new MainFrame();

                mFrame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

}

メインフレーム:

public class MainFrame extends JFrame {

public MainFrame() {
    setupFrame();
}

private void setupFrame() {
    SpringLayout layout = new SpringLayout();

    pnlMainCenter.setLayout(new BoxLayout(pnlMainCenter, BoxLayout.Y_AXIS));

    //Top part of the main panel 
    //Information to be displayed
    pnlTop.setLayout(layout);

    lblClosetLoc.setText("Closet location:");
    lblPhone1.setText("Phone 1:");
    lblJackPaired.setText("Jack paired:");
    lblPhone2.setText("Phone 2:");
    lblCubicle.setText("Cubicle:");
    lblJackType.setText("Jack type:");
    lblName.setText("Name:");
    lblPhoneJack1.setText("Phone jack 1:");
    lblPhoneJack2.setText("Phone jack 2:");
    lblDataJack1.setText("Data jack 1:");
    lblDataJack2.setText("Data jack 2:");
    lblComments.setText("Comments:");

    txtClosetLoc.setEditable(false);
    txtClosetLoc.setText("");
    txtPhone1.setEditable(false);
    txtPhone1.setText("");
    txtJackPaired.setEditable(false);
    txtJackPaired.setText("");
    txtPhone2.setEditable(false);
    txtPhone2.setText("");
    txtCubicle.setEditable(false);
    txtCubicle.setText("");
    txtJackType.setEditable(false);
    txtJackType.setText("");
    txtFname.setEditable(false);
    txtFname.setText("");
    txtLname.setEditable(false);
    txtLname.setText("");
    txtPhoneJack1.setEditable(false);
    txtPhoneJack1.setText("");
    txtPhoneJack2.setEditable(false);
    txtPhoneJack2.setText("");
    txtDataJack1.setEditable(false);
    txtDataJack1.setText("");
    txtDataJack2.setEditable(false);
    txtDataJack2.setText("");
    txtComments.setEditable(false);
    txtComments.setText("");

    pnlTop.add(lblClosetLoc);
    pnlTop.add(txtClosetLoc);
    pnlTop.add(lblPhone1);
    pnlTop.add(txtPhone1);
    pnlTop.add(lblJackPaired);
    pnlTop.add(txtJackPaired);
    pnlTop.add(lblPhone2);
    pnlTop.add(txtPhone2);
    pnlTop.add(lblCubicle);
    pnlTop.add(txtCubicle);
    pnlTop.add(lblJackType);
    pnlTop.add(txtJackType);
    pnlTop.add(lblName);
    pnlTop.add(txtFname);
    pnlTop.add(txtLname);
    pnlTop.add(lblPhoneJack1);
    pnlTop.add(lblPhoneJack2);
    pnlTop.add(lblDataJack1);
    pnlTop.add(lblDataJack2);
    pnlTop.add(lblComments);
    pnlTop.add(txtPhoneJack1);
    pnlTop.add(txtPhoneJack2);
    pnlTop.add(txtDataJack1);
    pnlTop.add(txtDataJack2);
    pnlTop.add(txtComments);

    layout.putConstraint(layout.WEST, lblClosetLoc, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblClosetLoc, 1, layout.NORTH, txtClosetLoc);

    layout.putConstraint(layout.WEST, txtClosetLoc, 5, layout.EAST, lblClosetLoc);
    layout.putConstraint(layout.NORTH, txtClosetLoc, 5, layout.NORTH, pnlTop);

    layout.putConstraint(layout.WEST, lblPhone1, 0, layout.WEST, lblPhone2);
    layout.putConstraint(layout.NORTH, lblPhone1, 1, layout.NORTH, txtPhone1);

    layout.putConstraint(layout.WEST, txtPhone1, 0, layout.WEST, txtPhone2);
    layout.putConstraint(layout.NORTH, txtPhone1, 0, layout.NORTH, txtClosetLoc);

    layout.putConstraint(layout.WEST, lblJackPaired, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblJackPaired, 1, layout.NORTH, txtJackPaired);

    layout.putConstraint(layout.WEST, txtJackPaired, 22, layout.EAST, lblJackPaired);
    layout.putConstraint(layout.NORTH, txtJackPaired, 5, layout.SOUTH, txtClosetLoc);

    layout.putConstraint(layout.WEST, lblPhone2, 0, layout.WEST, lblJackType);
    layout.putConstraint(layout.NORTH, lblPhone2, 1, layout.NORTH, txtPhone2);

    layout.putConstraint(layout.WEST, txtPhone2, 0, layout.WEST, txtJackType);
    layout.putConstraint(layout.NORTH, txtPhone2, 0, layout.NORTH, txtJackPaired);

    layout.putConstraint(layout.WEST, lblCubicle, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblCubicle, 1, layout.NORTH, txtCubicle);

    layout.putConstraint(layout.WEST, txtCubicle, 47, layout.EAST, lblCubicle);
    layout.putConstraint(layout.NORTH, txtCubicle, 5, layout.SOUTH, txtJackPaired);

    layout.putConstraint(layout.WEST, lblJackType, 83, layout.EAST, txtCubicle);
    layout.putConstraint(layout.NORTH, lblJackType, 1, layout.NORTH, txtJackType);

    layout.putConstraint(layout.WEST, txtJackType, 0, layout.WEST, txtDataJack1);
    layout.putConstraint(layout.NORTH, txtJackType, 0, layout.NORTH, txtCubicle);

    layout.putConstraint(layout.WEST, lblName, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblName, 1, layout.NORTH, txtFname);

    layout.putConstraint(layout.WEST, txtFname, 56, layout.EAST, lblName);
    layout.putConstraint(layout.NORTH, txtFname, 5, layout.SOUTH, txtCubicle);

    layout.putConstraint(layout.WEST, txtLname, 5, layout.EAST, txtFname);
    layout.putConstraint(layout.NORTH, txtLname, 5, layout.SOUTH, txtCubicle);

    layout.putConstraint(layout.WEST, txtPhoneJack1, 0, layout.WEST, txtFname);
    layout.putConstraint(layout.NORTH, txtPhoneJack1, 5, layout.SOUTH, txtFname);

    layout.putConstraint(layout.WEST, txtPhoneJack2, 0, layout.WEST, txtPhoneJack1);
    layout.putConstraint(layout.NORTH, txtPhoneJack2, 5, layout.SOUTH, txtPhoneJack1);

    layout.putConstraint(layout.WEST, txtComments, 0, layout.WEST, txtPhoneJack2);
    layout.putConstraint(layout.NORTH, txtComments, 5, layout.SOUTH, txtPhoneJack2);

    layout.putConstraint(layout.WEST, txtDataJack1, 0, layout.WEST, txtDataJack2);
    layout.putConstraint(layout.NORTH, txtDataJack1, 5, layout.SOUTH, txtLname);

    layout.putConstraint(layout.WEST, lblPhoneJack1, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblPhoneJack1, 1, layout.NORTH, txtPhoneJack1);

    layout.putConstraint(layout.WEST, txtDataJack2, 5, layout.EAST, lblDataJack2);
    layout.putConstraint(layout.NORTH, txtDataJack2, 5, layout.SOUTH, txtDataJack1);

    layout.putConstraint(layout.WEST, lblPhoneJack2, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblPhoneJack2, 1, layout.NORTH, txtPhoneJack2);

    layout.putConstraint(layout.WEST, lblDataJack1, 0, layout.WEST, lblJackType);
    layout.putConstraint(layout.NORTH, lblDataJack1, 1, layout.NORTH, txtDataJack1);

    layout.putConstraint(layout.WEST, lblDataJack2, 0, layout.WEST, lblDataJack1);
    layout.putConstraint(layout.NORTH, lblDataJack2, 1, layout.NORTH, txtDataJack2);

    layout.putConstraint(layout.WEST, lblComments, 5, layout.WEST, pnlTop);
    layout.putConstraint(layout.NORTH, lblComments, 1, layout.NORTH, txtComments);

    layout.putConstraint(layout.EAST, pnlTop, 5, layout.EAST, txtDataJack2);
    layout.putConstraint(layout.SOUTH, pnlTop, 5, layout.SOUTH, txtComments);
    //end top part

    //Middle part of the main panel
    //Buttons and options
    pnlMid.setLayout(layout);

    lblRecords.setText("0/MaxRecords");
    lblRecords.setHorizontalAlignment(JLabel.CENTER);
    lblRecords.setPreferredSize(new Dimension(82, 20));

    btnSearch.setText("Search");
    btnSearch.setPreferredSize(new Dimension(75, 22));
    btnSearch.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    btnEditSave.setText("Edit");
    btnEditSave.setPreferredSize(new Dimension(75, 22));
    btnEditSave.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    btnPrevRecord.setText("<<");
    btnPrevRecord.setPreferredSize(new Dimension(50, 22));
    btnPrevRecord.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    btnNextRecord.setText(">>");
    btnNextRecord.setPreferredSize(new Dimension(50, 22));
    btnNextRecord.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });

    pnlMid.add(btnSearch);
    pnlMid.add(btnEditSave);
    pnlMid.add(btnPrevRecord);
    pnlMid.add(btnNextRecord);
    pnlMid.add(lblRecords);

    layout.putConstraint(layout.WEST, lblRecords, 12, layout.WEST, btnPrevRecord);
    layout.putConstraint(layout.NORTH, lblRecords, 0, layout.NORTH, pnlMid);

    layout.putConstraint(layout.WEST, btnSearch, 5, layout.WEST, pnlMid);
    layout.putConstraint(layout.NORTH, btnSearch, 5, layout.SOUTH, lblRecords);

    layout.putConstraint(layout.WEST, btnEditSave, 5, layout.EAST, btnSearch);
    layout.putConstraint(layout.NORTH, btnEditSave, 5, layout.SOUTH, lblRecords);

    layout.putConstraint(layout.EAST, btnNextRecord, -5, layout.EAST, pnlMid);
    layout.putConstraint(layout.NORTH, btnNextRecord, 5, layout.SOUTH, lblRecords);

    layout.putConstraint(layout.EAST, btnPrevRecord, -5, layout.WEST, btnNextRecord);
    layout.putConstraint(layout.NORTH, btnPrevRecord, 5, layout.SOUTH, lblRecords);

    layout.putConstraint(layout.EAST, pnlMid, 0, layout.EAST, pnlTop);
    layout.putConstraint(layout.SOUTH, pnlMid, 5, layout.SOUTH, btnNextRecord);
    //end middle part

    //add the objects to the panels
    pnlMainCenter.add(pnlTop);
    pnlMainCenter.add(pnlMid);
    //add the panels to the frame
    getContentPane().add(pnlMainCenter, BorderLayout.CENTER);

    //set frame properties
    setTitle("Test");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setLocation(300,300);
    pack();
}

private JPanel pnlMainCenter = new JPanel();
private JPanel pnlMid = new JPanel();
private JPanel pnlTop = new JPanel();
private JLabel lblClosetLoc = new JLabel();
private JLabel lblPhone1 = new JLabel();
private JLabel lblJackPaired = new JLabel();
private JLabel lblPhone2 = new JLabel();
private JLabel lblCubicle = new JLabel();
private JLabel lblJackType = new JLabel();
private JLabel lblName = new JLabel();
private JLabel lblPhoneJack1 = new JLabel();
private JLabel lblPhoneJack2 = new JLabel();
private JLabel lblDataJack1 = new JLabel();
private JLabel lblDataJack2 = new JLabel();
private JLabel lblComments = new JLabel();
private JLabel lblRecords = new JLabel();
private JTextField txtClosetLoc = new JTextField(5);
private JTextField txtPhone1 = new JTextField(5);
private JTextField txtJackPaired = new JTextField(5);
private JTextField txtPhone2 = new JTextField(5);
private JTextField txtCubicle = new JTextField(10);
private JTextField txtJackType = new JTextField(5);
private JTextField txtFname = new JTextField(9);
private JTextField txtLname = new JTextField(9);
private JTextField txtPhoneJack1 = new JTextField(6);
private JTextField txtPhoneJack2 = new JTextField(6);
private JTextField txtDataJack1 = new JTextField(6);
private JTextField txtDataJack2 = new JTextField(6);
private JTextField txtComments = new JTextField(15);
private JButton btnSearch = new JButton();
private JButton btnEditSave = new JButton();
private JButton btnPrevRecord = new JButton();
private JButton btnNextRecord = new JButton();
}
4

0 に答える 0