Java GUI将对象添加到数据库

时间:2013-12-04 13:30:48

标签: java swing

所以我制作了一个GUI,在这种情况下如果要将Car添加到数据库中,则单击“添加” 然后使用以下代码弹出JFrame:

  public void newCar()
   {


      JFrame window = new JFrame("New Car");

      JPanel newCarButtons = new JPanel();

      newCarButtons.setLayout(new FlowLayout());

      saveCar=new JButton("Save");
      saveCar.addActionListener(buttonListener);
      newCarButtons.add(saveCar);

      cancelCar=new JButton("Cancel");
      cancelCar.addActionListener(buttonListener);
      newCarButtons.add(cancelCar);



      JPanel newCarText = new JPanel();

      GroupLayout group = new GroupLayout(newCarText);

      newCarText.setLayout(group);

      group.setAutoCreateGaps(true);
      group.setAutoCreateContainerGaps(true);

      JLabel make = new JLabel("Brand:");
      JTextField maket = new JTextField(10);

      newCarText.add(make);
      newCarText.add(maket);

      JLabel model = new JLabel("Model:");
      JTextField modelt = new JTextField(10);

      newCarText.add(model);
      newCarText.add(modelt);

      JLabel license = new JLabel("License Plate Numbers:");
      JTextField licenset = new JTextField(10);

      newCarText.add(license);
      newCarText.add(licenset);

      JLabel color = new JLabel("Color:");
      JTextField colort = new JTextField(10);

      newCarText.add(color);
      newCarText.add(colort);

      JLabel year = new JLabel("Year:");
      final JTextField yeart = new JTextField(10);
      yeart.addKeyListener(new KeyAdapter()
      {
         @Override
         public void keyTyped(KeyEvent e)
         {
            super.keyTyped(e);

            e.getKeyCode();

            if (!((int) e.getKeyChar() >= 48 && (int) e.getKeyChar() <= 57))
            {
               e.consume();
            }
         }

      });

      newCarText.add(year);
      newCarText.add(yeart);

      JLabel horse = new JLabel("Horse Power: ");
      JTextField horset = new JTextField(10);

      newCarText.add(horse);
      newCarText.add(horset);

      JLabel isAvailable = new JLabel("Car Status:");
      JLabel isAvailablet = new JLabel("Available");

      newCarText.add(isAvailable);
      newCarText.add(isAvailablet);

      JLabel time = new JLabel("Time Until Service: ");
      JTextField timet = new JTextField(10);

      newCarText.add(time);
      newCarText.add(timet);

      JLabel consumption = new JLabel("Consumption per 100km: ");
      JTextField consumptiont = new JTextField(10);

      newCarText.add(consumption);
      newCarText.add(consumptiont);



      JLabel seats = new JLabel("Number of Seats: ");
      JTextField seatst = new JTextField(10);
      seatst.addKeyListener(new KeyAdapter()
      {
         @Override
         public void keyTyped(KeyEvent e)
         {
            super.keyTyped(e);

            e.getKeyCode();

            if (!((int) e.getKeyChar() >= 48 && (int) e.getKeyChar() <= 57))
            {
               e.consume();
            }
         }

      });

      newCarText.add(seats);
      newCarText.add(seatst);

      JLabel doors = new JLabel("Number of Doors: ");
      JTextField doorst = new JTextField(10);

      doorst.addKeyListener(new KeyAdapter()
      {
         @Override
         public void keyTyped(KeyEvent e)
         {
            super.keyTyped(e);

            e.getKeyCode();

            if (!((int) e.getKeyChar() >= 48 && (int) e.getKeyChar() <= 57))
            {
               e.consume();
            }
         }

      });

      newCarText.add(doors);
      newCarText.add(doorst);

      JLabel transmission = new JLabel("Transmission ");
      JComboBox transmissiont = new JComboBox<String>();

      transmissiont.addItem("Auto");
      transmissiont.addItem("Manual");

      transmissiont.addActionListener(buttonListener);

      newCarText.add(transmission);
      newCarText.add(transmissiont);

      JLabel climate = new JLabel("Climate Control: ");
      JComboBox climatet = new JComboBox<String>();
      climatet.addItem("Yes");
      climatet.addItem("No");

      newCarText.add(climate);
      newCarText.add(climatet);

      GroupLayout.SequentialGroup hGroup = group.createSequentialGroup();

      hGroup.addGroup(group.createParallelGroup().addComponent(make)
            .addComponent(model).addComponent(license).addComponent(color)
            .addComponent(time).addComponent(consumption)
            .addComponent(year).addComponent(horse).addComponent(isAvailable)
            .addComponent(seats).addComponent(doors).addComponent(transmission)
            .addComponent(climate));

      hGroup.addGroup(group.createParallelGroup().addComponent(maket)
            .addComponent(modelt).addComponent(licenset).addComponent(colort)
            .addComponent(timet).addComponent(consumptiont)
            .addComponent(yeart).addComponent(isAvailablet)
            .addComponent(horset).addComponent(seatst).addComponent(doorst)
            .addComponent(transmissiont).addComponent(climatet));

      group.setHorizontalGroup(hGroup);

      GroupLayout.SequentialGroup vGroup = group.createSequentialGroup();

      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(make).addComponent(maket));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(model).addComponent(modelt));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(license).addComponent(licenset));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(color).addComponent(colort));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(year).addComponent(yeart));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(horse).addComponent(horset));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(time).addComponent(timet));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(consumption).addComponent(consumptiont));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(isAvailable).addComponent(isAvailablet));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(seats).addComponent(seatst));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(doors).addComponent(doorst));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(transmission).addComponent(transmissiont));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(climate).addComponent(climatet));

      group.setVerticalGroup(vGroup);

      JPanel newCar = new JPanel();
      newCar.setLayout(new BorderLayout());

      newCar.add(newCarText, BorderLayout.NORTH);
      newCar.add(newCarButtons, BorderLayout.SOUTH);

      newCar.setBorder(new TitledBorder(BorderFactory
            .createLineBorder(Color.black), "[New Car]", 2, 0));

      Car car1= new Car(maket.getText(), modelt.getText(), licenset.getText(), colort.getText(), Integer.parseInt("yeart.getText()"), null, horset.getText(), timet.getText(), consumptiont.getText(), Integer.parseInt("seatst.getText()"), Integer.parseInt("doorst.getText()"), null,  null, 0);

      window.add(newCar);
      window.setLocationRelativeTo(null);
      window.setSize(400, 450);
      window.setVisible(true);

   }

请注意,底部有一个Car对象。这是我需要你帮助的地方。添加汽车时,适配器将Car对象作为参数,因此我们的想法是填写所有文本字段,并根据您输入的文本创建一个新的Car对象。但是如果我这样做,这样它就会创建一个空的Car对象,因为当你打开窗口时,你在文本域中没有任何东西。那么当你点击“添加”时我如何制作它会根据文本字段中的内容发送一个填充的Car对象?

按钮监听器:

private class MyButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         if (e.getSource() == addCars)
         {
            newCar();

         }

         if (e.getSource() == saveCar)
         {

            adapter.addCar(car);
         }
       }
   }

2 个答案:

答案 0 :(得分:0)

为什么不简单地将car对象作为参数传递给newCar方法并在那里初始化正确的字段,就像在持久化汽车实例时一样?

 if (e.getSource() == addCars)
 {
    newCar(car);

 }

请记住更改newCar方法签名。

  public void newCar(Car newCar)
   {
     //DO WHATEVER YOU WANT WITH THAT DATA AND INITIALIZE PROPER FIELDS

      JFrame window = new JFrame("New Car");
    (.....)

答案 1 :(得分:0)

这应该进入听众

Car car1= new Car(maket.getText(), modelt.getText(), licenset.getText(), 
                 colort.getText(), Integer.parseInt("yeart.getText()"), null,
                 horset.getText(), timet.getText(), consumptiont.getText(), 
                 Integer.parseInt("seatst.getText()"),  
                 Integer.parseInt("doorst.getText()"), 
                 null,  null, 0);

您想要在按下按钮时获取文本。不在您的构造函数

 private class MyButtonListener implements ActionListener
 {
     public void actionPerformed(ActionEvent e)
     {
         if (e.getSource() == addCars)
         {
             newCar();  // I don't know what you're trying to do here
         }

         if (e.getSource() == saveCar)
         {
             Car car1= new Car(maket.getText(), modelt.getText(), licenset.getText(), 
                     colort.getText(), Integer.parseInt("yeart.getText()"), null,
                     horset.getText(), timet.getText(), consumptiont.getText(), 
                     Integer.parseInt("seatst.getText()"),  
                     Integer.parseInt("doorst.getText()"), 
                     null,  null, 0);

             // add car1 to database
         }
     }
 }

编辑:将所有文本字段声明为类成员,然后您可以在yout listener类中使用它们

public class GUI{
    JTextFeild maket = new JTextField();
    JTextFeild licenset = new JTextField();
    JTextFeild colort = new JTextField();
    JTextFeild yeart = new JTextField();
    JTextFeild horset = new JTextField();
    JTextFeild timet = new JTextField();
    JTextFeild consumptiont = new JTextField();
    JTextFeild maket = new JTextField();

    public void newCar(){
        ...  // remove your textfields from here.
    }

    public static void main(String[] args){
        ...
    }

    private class MyButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == addCars)
            {
                newCar();  // I don't know what you're trying to do here
            }

            if (e.getSource() == saveCar)
            {
                Car car1= new Car(maket.getText(), modelt.getText(), licenset.getText(), 
                    colort.getText(), Integer.parseInt("yeart.getText()"), null,
                    horset.getText(), timet.getText(), consumptiont.getText(), 
                    Integer.parseInt("seatst.getText()"),  
                    Integer.parseInt("doorst.getText()"), 
                    null,  null, 0);

                // add car1 to database
             }
         }
     }
 }