0

我想将人/公司添加到一种车辆。关键是在个人或公司之间做出选择,然后继续输入有关他们的详细信息。然后继续选择它是哪种类型的车辆,然后选择更多细节。我不知道我必须做什么才能让程序正常运行,我希望有人能帮助我。

private void addVehicle() {
    System.out.println("Add Vehicle");
    String[] options = {"Private person", "Firm"};
    String[] vehicle = {"Car", "Truck", "MC"};
    String[] personDetails = {"Firstname: ","Lastname: ","Date of Birth: ",
                              "Address: ","Phone Number: "};

    int chooseOwnerType = JOptionPane.showOptionDialog(this, "Private person/firm",
        "Choose an option", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, "");
    int chooseVehicleType = JOptionPane.showOptionDialog(this, "What kind of vehicle is it?",
        "Choose an option", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, vehicle, "");
    int numPairs = personDetails.length; 

    if(chooseOwnerType == 0) {
        JPanel p = new JPanel(new SpringLayout());
        for (int i = 0; i < numPairs; i++) {
            JLabel l = new JLabel(personDetails[i], JLabel.TRAILING);
            p.add(l);
            JTextField textField = new JTextField(10);
            l.setLabelFor(textField);
            p.add(textField);
        }
    }

    if(chooseOwnerType == 1) {
    }
4

1 回答 1

0

对于特定示例,您需要使用一些东西。首先,我建议您阅读如何使用 SpringLayout,因为您需要指定“Springs”才能正确显示文本框(请参阅:http ://docs.oracle.com/javase/tutorial/uiswing/layout /spring.html )

“显然,我们遇到了一些问题。不仅框架太小,而且即使调整大小,组件也都位于 (0,0)。发生这种情况是因为我们没有设置任何弹簧来指定组件的位置和容器的宽度。一个小小的安慰是,至少组件具有它们的首选尺寸——我们从 SpringLayout 为每个组件创建的默认弹簧中免费获得。

就个人而言,我会使用两列的网格布局。因此,根据您最初的帖子,我进行了更改并提出了以下代码。我强烈建议您阅读一些 swing 和 java 教程。

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

public class Test1 {

    public static void main(String args[]) {
        new Test1().addVehicle();
    }

    private void addVehicle() {
        System.out.println("Add Vehicle");
        String[] options = { "Private person", "Firm" };
        String[] vehicle = { "Car", "Truck", "MC" };
        String[] personDetails = { "Firstname: ", "Lastname: ",
                "Date of Birth: ", "Address: ", "Phone Number: " };

        int chooseOwnerType = JOptionPane.showOptionDialog(null,
                "Private person/firm", "Choose an option",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                options, "");
        int chooseVehicleType = JOptionPane.showOptionDialog(null,
                "What kind of vehicle is it?", "Choose and option",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                vehicle, "");
        int numPairs = personDetails.length;

        System.out.println("Owner Type:" + chooseOwnerType);
        System.out.println("Vehicle Type:" + chooseVehicleType);
        JPanel p = null;

        GridLayout gridLayout = new GridLayout(0,2);

            //Private Person
        if (chooseOwnerType == 0) {
            p = new JPanel(gridLayout);
            for (int i = 0; i < numPairs; i++) {
                JLabel l = new JLabel(personDetails[i], JLabel.TRAILING);
                p.add(l);
                JTextField textField = new JTextField(10);
                l.setLabelFor(textField);
                p.add(textField);
            }
        }

        if (chooseOwnerType == 1) {
        }

        if (p != null) {
            JFrame window = new JFrame("GUI Test");
            window.setContentPane(p);
            window.setSize(250, 100);
            window.setLocation(100, 100);
            window.setVisible(true);
            window.pack();
        }

    }

}

当用户为所有者选择第一个按钮时,在自定义生成的 JFrame 下方。 在此处输入图像描述

于 2013-04-07T16:06:32.373 回答