将JTextField添加到构造函数

时间:2018-01-22 23:29:12

标签: java swing stack-overflow jtextfield

所以我知道StackOverflowError是什么,问题是我在这里找不到它。我应该用标签,文本字段和按钮制作一个简单的GUI。其中一个按钮应该“清除”文本字段,所以我通过在构造函数中将文本字段作为参数添加,但是当我实际添加文本字段时,我只是得到堆栈溢出错误。这是代码:

订单类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Orders extends JFrame {
   public Orders() {
      JPanel panel1 = new JPanel();
      panel1.setLayout(new GridLayout(4, 2, 2, 2));

      JLabel name = new JLabel("Item name:");
      JLabel number = new JLabel("Number of:");
      JLabel cost = new JLabel("Cost:");
      JLabel amount = new JLabel("Amount owed:");

      JTextField nameJtf = new JTextField(10);
      JTextField numberJtf = new JTextField(10);
      JTextField costJtf = new JTextField(10);
      JTextField amountJtf = new JTextField(10);

      panel1.add(name);
      panel1.add(nameJtf);

      panel1.add(number);
      panel1.add(numberJtf);

      panel1.add(cost);
      panel1.add(costJtf);

      panel1.add(amount);
      panel1.add(amountJtf);

      JPanel panel2 = new JPanel();

      JButton calculate = new JButton("Calculate");
      JButton save = new JButton("Save");
      JButton clear = new JButton("Clear");
      JButton exit = new JButton("Exit");

      panel2.add(calculate);
      panel2.add(save);
      panel2.add(clear);
      panel2.add(exit); 

      OnClick action = new OnClick(exit, clear, save, calculate, nameJtf, numberJtf, costJtf, amountJtf);
      exit.addActionListener(action);

      this.setTitle("Otto's Items Orders Calculator");
      this.add(panel1, BorderLayout.NORTH);
      this.add(panel2, BorderLayout.SOUTH);
      this.setSize(400, 200);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      this.setLocationRelativeTo(null);
      this.setVisible(true);
   }

   public static void main(String[] args) {
      new Orders();
   }
}

OnClick类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class OnClick extends Orders implements ActionListener {

   private JButton exitModify = null;
   private JButton clearModify = null;
   private JButton saveModify = null;
   private JButton calculateModify = null;

   private JTextField nameModify = null;
   private JTextField numberModify = null;
   private JTextField costModify = null;
   private JTextField amountModify = null;

   public OnClick (JButton _exitModify, JButton _clearModify, JButton _saveModify, JButton _calculateModify, JTextField _nameModify, JTextField _numberModify, JTextField _costModify, JTextField _amountModify) {
      exitModify = _exitModify;
      clearModify = _clearModify;
      saveModify = _saveModify;
      calculateModify = _calculateModify;
      nameModify = _nameModify;
      numberModify = _numberModify;
      costModify = _numberModify;
      amountModify = _amountModify;
   }

   public void actionPerformed(ActionEvent e) {

      Object o = e.getSource();

      if (o == this.exitModify) {
         System.exit(0);
      } else if (o == this.clearModify) {
         amountModify = null;
         nameModify = null;
         costModify = null;
         numberModify = null;
      }
   }   
}

一旦我添加nameJtf,我就会收到此错误。

0 个答案:

没有答案