有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我一直在做一个项目,在这个项目中,我需要创建一个像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,还能够通过单击按钮创建car类的对象,但不知道如何创建多个对象以及如何存储它们。这是我的问题,你能帮我吗

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, 50, 200, 20);
        c.add(tf1);

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

        tf2 = new JTextField();
        tf2.setBounds(130, 100, 200, 20);
        c.add(tf2);

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

        tf3 = new JTextField();
        tf3.setBounds(130, 150, 200, 20);
        c.add(tf3);

        jl4 = new JLabel("Owner Name");
        jl4.setBounds(20, 200, 100, 20);
        c.add(jl4);

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

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

        tf5 = new JTextField();
        tf5.setBounds(130, 250, 200, 20);
        c.add(tf5);

        button1 = new JButton("SUBMIT");
        button1.setFocusable(false);
        button1.setBounds(130, 300, 100, 25);
        c.add(button1);

        button1.addActionListener(this);

        button2 = new JButton("SHOW");
        button2.setFocusable(false);
        button2.setBounds(330, 300, 100, 25);
        c.add(button2);

        button2.addActionListener(this);

        button3 = new JButton("Reset");
        button3.setFocusable(false);
        button3.setBounds(230, 330, 100, 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,50,450,200);
        c.add(table);

        validate();

        msg = new JLabel("");
        msg.setBounds(20, 450, 250, 20);
        c.add(msg);

        msg1 = new JLabel("");
        msg1.setBounds(20, 550, 250, 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 !!");
    }
}

共 (0) 个答案