我遇到了问题,它是通过从另一个链接列表调用链接列表。我不知道如何调用CustomerPark类并将元素放在文本字段中。
这是我的第一堂课
package assignment_parking_2;
import java.util.*;
public class Parking{
public static void main(String[]args){
LinkedList<String> customers = new LinkedList<String>();
Scanner scan = new Scanner(System.in);
int choice=0;
do{
//for(int i=0; i<=25; i++){
System.out.println("\nRentable Parking Space");
System.out.println("[1]Park a Car");
System.out.println("[2]Leave");
System.out.println("[3]View Parking Lot");
System.out.println("[4]Exit");
System.out.println("Enter choice");
choice = scan.nextInt();
switch(choice){
case 1:
System.out.println("Insert your plate number: ");
String platenos = scan.next();
customers.addLast(platenos);
break;
case 2:
System.out.println("Your customer who has a plate number " +customers.removeFirst()+ " has already left!");
break;
case 3:
System.out.println("There are " +customers.size()+ " customer/s in the line.");
CustomerPark viewPark = new CustomerPark();
viewPark.main(args);
int index = 0;
while(index<customers.size()){
System.out.print("||" + customers.get(index));
index+=1;
}
break;
case 4:
System.out.println("Thank you for using the System. The system will now exit.");
break;
default:
System.out.println("You have made an invalid choice, please choose again.");
}
// }System.out.println("Sorry the parking space is already full!");
}
while(choice!=4);
} }
我的第二堂课
package assignment_parking_2;
import java.awt.*;
import javax.swing.*;
import java.util.*;
公共类CustomerPark扩展了JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_8;
private JTextField textField_9;
private JTextField textField_10;
private JTextField textField_11;
private JTextField textField_12;
private JTextField textField_13;
private JTextField textField_14;
private JTextField textField_15;
private JTextField textField_16;
private JTextField textField_17;
private JTextField textField_18;
private JTextField textField_19;
private JTextField textField_20;
private JTextField textField_21;
private JTextField textField_22;
private JTextField textField_23;
private JTextField textField_24;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CustomerPark frame = new CustomerPark();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CustomerPark() {
setResizable(false);
setTitle("View Parking Lot");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 358, 360);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setEditable(false);
textField.setBounds(21, 11, 52, 50);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setEditable(false);
textField_1.setBounds(83, 11, 52, 50);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setEditable(false);
textField_2.setBounds(145, 11, 52, 50);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setEditable(false);
textField_3.setBounds(207, 11, 52, 50);
contentPane.add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setEditable(false);
textField_4.setBounds(268, 11, 52, 50);
contentPane.add(textField_4);
textField_4.setColumns(10);
textField_5 = new JTextField();
textField_5.setEditable(false);
textField_5.setBounds(21, 72, 52, 50);
contentPane.add(textField_5);
textField_5.setColumns(10);
textField_6 = new JTextField();
textField_6.setEditable(false);
textField_6.setBounds(83, 72, 52, 50);
contentPane.add(textField_6);
textField_6.setColumns(10);
textField_7 = new JTextField();
textField_7.setEditable(false);
textField_7.setBounds(145, 72, 52, 50);
contentPane.add(textField_7);
textField_7.setColumns(10);
textField_8 = new JTextField();
textField_8.setEditable(false);
textField_8.setBounds(207, 72, 52, 50);
contentPane.add(textField_8);
textField_8.setColumns(10);
textField_9 = new JTextField();
textField_9.setEditable(false);
textField_9.setBounds(268, 72, 52, 50);
contentPane.add(textField_9);
textField_9.setColumns(10);
textField_10 = new JTextField();
textField_10.setEditable(false);
textField_10.setBounds(21, 133, 52, 50);
contentPane.add(textField_10);
textField_10.setColumns(10);
textField_11 = new JTextField();
textField_11.setEditable(false);
textField_11.setBounds(83, 133, 52, 50);
contentPane.add(textField_11);
textField_11.setColumns(10);
textField_12 = new JTextField();
textField_12.setEditable(false);
textField_12.setBounds(145, 133, 52, 50);
contentPane.add(textField_12);
textField_12.setColumns(10);
textField_13 = new JTextField();
textField_13.setEditable(false);
textField_13.setBounds(207, 133, 52, 50);
contentPane.add(textField_13);
textField_13.setColumns(10);
textField_14 = new JTextField();
textField_14.setEditable(false);
textField_14.setBounds(268, 133, 52, 50);
contentPane.add(textField_14);
textField_14.setColumns(10);
textField_15 = new JTextField();
textField_15.setEditable(false);
textField_15.setBounds(21, 194, 52, 50);
contentPane.add(textField_15);
textField_15.setColumns(10);
textField_16 = new JTextField();
textField_16.setEditable(false);
textField_16.setBounds(83, 194, 52, 50);
contentPane.add(textField_16);
textField_16.setColumns(10);
textField_17 = new JTextField();
textField_17.setEditable(false);
textField_17.setBounds(145, 194, 52, 50);
contentPane.add(textField_17);
textField_17.setColumns(10);
textField_18 = new JTextField();
textField_18.setEditable(false);
textField_18.setBounds(207, 194, 52, 50);
contentPane.add(textField_18);
textField_18.setColumns(10);
textField_19 = new JTextField();
textField_19.setEditable(false);
textField_19.setBounds(268, 194, 52, 50);
contentPane.add(textField_19);
textField_19.setColumns(10);
textField_20 = new JTextField();
textField_20.setEditable(false);
textField_20.setBounds(21, 255, 52, 50);
contentPane.add(textField_20);
textField_20.setColumns(10);
textField_21 = new JTextField();
textField_21.setEditable(false);
textField_21.setBounds(83, 255, 52, 50);
contentPane.add(textField_21);
textField_21.setColumns(10);
textField_22 = new JTextField();
textField_22.setEditable(false);
textField_22.setBounds(145, 255, 52, 50);
contentPane.add(textField_22);
textField_22.setColumns(10);
textField_23 = new JTextField();
textField_23.setEditable(false);
textField_23.setBounds(207, 255, 52, 50);
contentPane.add(textField_23);
textField_23.setColumns(10);
textField_24 = new JTextField();
textField_24.setEditable(false);
textField_24.setBounds(268, 255, 52, 50);
contentPane.add(textField_24);
textField_24.setColumns(10);
}
}
我希望你能帮助我。谢谢!!
答案 0 :(得分:1)
你的主要问题是你正试图用Swing GUI将一个静态主方法程序推到一起,而这简直不会起作用,因为它的编程相当于混合油和水。重写您的顶级类作为符合OOP的类,一个具有非静态私有字段,具有公共非静态方法(例如,使用&#34;状态&#34;和#34;行为&#34;)和用它作为大脑或&#34;模型&#34;您的GUI(&#34;视图&#34;)。主要方法应该简短,只需要创建和连接模型与视图即可。
因此,这也意味着您不希望使用Scanner和System.in获取用户输入,而是创建通过GUI获取用户输入的代码,因为线性控制台/扫描仪代码也是如此与事件驱动的GUI代码不兼容。
其他问题 - 是的,摆脱所有不必要的冗余,因为它会使维护,调试和增强这个程序成为一个彻头彻尾的噩梦。
请注意,您通过方法和构造函数将信息从一个类传递到另一个类。例如,如果您为CustomerPark构造函数提供了LinkedList参数,则可以在创建时将该信息传递给类。
例如,您可以使用JTextField的2D数组并将其填充到接收LinkedList的构造函数中。
public CustomerPark(List<String> customerList) {
setLayout(new GridLayout(ROWS, COLS, GAP, GAP));
for (int r = 0; r < textFields.length; r++) {
for (int c = 0; c < textFields[r].length; c++) {
textFields[r][c] = new JTextField(TF_COLS);
textFields[r][c].setEditable(false);
textFields[r][c].setFont(TF_FONT);
textFields[r][c].setText(customerList.remove(0));
add(textFields[r][c]);
}
}
}