我想将一个人/公司添加到一种车辆类型。关键是要在人或公司之间做出选择,然后继续输入有关它们的详细信息。然后继续选择它是什么类型的车辆,然后是更多细节。我不知道我要做什么,让程序正常工作,我希望有人可以帮助我。
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) {
}
答案 0 :(得分:0)
您需要为特定示例使用某些内容。 首先,我建议您阅读如何使用SpringLayout,因为您需要指定&#34; Springs&#34;为了使文本框正确显示(请参阅:http://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html)
&#34;显然,我们遇到了一些问题。框架不仅太小,而且即使调整大小,组件都位于(0,0)。发生这种情况是因为我们没有设置弹簧来指定组件&#39;位置和容器的宽度。一个小小的安慰是至少组件处于它们的首选大小 - 我们从SpringLayout为每个组件创建的默认弹簧中免费获得它。&#34;
就个人而言,我会使用带有两列的网格布局。 因此,根据您的初始帖子,我进行了更改,并提出了以下代码。我强烈建议你阅读一些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下方。