如何创建和存储从 Java GUI 获取输入的对象?

我一直在做一个项目,我需要创建一个像 car 这样的类,它具有 brandregistration_No 等属性,并带有适当的参数化构造函数。

这是 CarDemo 类:

package com.company;

import java.io.*;
    
class CarDemo implements Serializable {
    public String compName;
    public String model;
    public String regNo;
    public String ownerName;
    public String mobile;

    CarDemo(String compName,String model,String regNo,String ownerName,String mobile) {
        this.compName = compName;
        this.model = model;
        this.regNo = regNo;
        this.ownerName = ownerName;
        this.mobile = mobile;
    }
}

然后我需要通过 GUI 表单获取输入并初始化对象。 我已经成功创建了 GUI 并且还能够通过单击按钮创建汽车类的对象,但无法弄清楚如何创建多个对象以及如何存储它们。这是我的问题,你能帮帮我吗。

GUI 代码:

package com.company;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.actionEvent;
import java.awt.event.actionListener;
import java.io.*;
import java.util.ArrayList;

public class CarGUI extends JFrame implements actionListener,Serializable {

    JLabel jl1,jl2,jl3,jl4,jl5,msg,msg1;
    JTextField tf1,tf2,tf3,tf4,tf5;
    JButton button1,button2,button3;
    JTable table;
    DefaultTableModel model ;
     public String brand; 
     public String carModel;
     public String regNo;
     public String ownerName;
     public String mobile;

    CarGUI() {
        setTitle("Car Registration");
        setSize(1000,650);
        setLocationRelativeTo(null);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container c = getcontentPane();
        c.setLayout(null);

        jl1 = new JLabel("Brand Name");
        jl1.setbounds(20,50,100,20);
        c.add(jl1);

        tf1 = new JTextField();
        tf1.setbounds(130,200,20);
        c.add(tf1);

        jl2 = new JLabel("Model");
        jl2.setbounds(20,20);
        c.add(jl2);

        tf2 = new JTextField();
        tf2.setbounds(130,20);
        c.add(tf2);

        jl3 = new JLabel("Registration No.");
        jl3.setbounds(20,150,20);
        c.add(jl3);

        tf3 = new JTextField();
        tf3.setbounds(130,20);
        c.add(tf3);

        jl4 = new JLabel("Owner Name");
        jl4.setbounds(20,20);
        c.add(jl4);

        tf4 = new JTextField();
        tf4.setbounds(130,20);
        c.add(tf4);

        jl5 = new JLabel("Owner Mobile");
        jl5.setbounds(20,250,20);
        c.add(jl5);

        tf5 = new JTextField();
        tf5.setbounds(130,20);
        c.add(tf5);

        button1 = new JButton("SUBMIT");
        button1.setfocusable(false);
        button1.setbounds(130,300,25);
        c.add(button1);

        button1.addactionListener(this);

        button2 = new JButton("SHOW");
        button2.setfocusable(false);
        button2.setbounds(330,25);
        c.add(button2);

        button2.addactionListener(this);

        button3 = new JButton("Reset");
        button3.setfocusable(false);
        button3.setbounds(230,330,25);
        c.add(button3);

        button3.addactionListener(this);

        model= new DefaultTableModel();
        Object[] columns = {"Brand","Model","Reg_No","Owner","Mobile"};
        model.setColumnIdentifiers(columns);

        table = new JTable(model);
        table.setModel(model);
        table.setbounds(400,450,200);
        c.add(table);

        validate();

        msg = new JLabel("");
        msg.setbounds(20,20);
        c.add(msg);

        msg1 = new JLabel("");
        msg1.setbounds(20,550,20);
        c.add(msg1);

        setVisible(true);
    }

    @Override
    public void actionPerformed(actionEvent e) {
        if (e.getsource() == button2) {

            if (tf1.getText().isEmpty() || tf2.getText().isEmpty() || tf3.getText().isEmpty() || tf4.getText().isEmpty() || tf5.getText().isEmpty()) {
                msg.setText("Remaining fields can't be empty");
            } else {
                msg.setText("Registration Successful!");

                Object [] row = new Object[5];

                row[0]=tf1.getText();
                row[1]=tf2.getText();
                row[2]=tf3.getText();
                row[3]=tf4.getText();
                row[4]=tf5.getText();

                model.addRow(row);
            }
        }
        if (e.getsource()==button3){

            tf1.setText(null);
            tf2.setText(null);
            tf3.setText(null);
            tf4.setText(null);
            tf5.setText(null);
        }
        if(e.getsource() == button1){
            //int count = 0;
              brand = tf1.getText();
              carModel=tf2.getText();
              regNo=tf3.getText();
              ownerName=tf4.getText();
              mobile=tf5.getText();

            CarDemo ce = new CarDemo(tf1.getText(),tf2.getText(),tf3.getText(),tf4.getText(),tf5.getText());
            try {
                FileOutputStream fileout = new FileOutputStream("Carinfo.ser");
                ObjectOutputStream out = new ObjectOutputStream(fileout);
                out.writeObject(ce);
                out.close();
                fileout.close();
            }catch(IOException ee){
                System.out.println(ee);
            }
            msg.setText("object info saved !!");
        }
    }

    public static void main(String[] args) {
        CarGUI car = new CarGUI();
        System.out.println("object info saved !!");
    }
}
kukuming 回答:如何创建和存储从 Java GUI 获取输入的对象?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/44311.html

大家都在问