Измените JTextField с другого класса Java/BlueJ

Я работаю над проектом с JFrame (впервые). У меня есть несколько JTextFields и 1 JButton на кадре. Поэтому я пытаюсь редактировать текст моего JTextField из другого класса. Я ввожу 2 значения в JTextField и хочу, чтобы результат был показан в другом JTextField.

Всякий раз, когда я нажимаю кнопку, я хочу, чтобы одно текстовое поле JTextField9 было изменено на значение, которое я вычислил в методе моего другого класса (в данном случае метод totalHours() в модуле).

Я пробовал много примеров, которые нашел, но ни один из них, похоже, не помог мне T_T.

Вот код, с которым у меня проблемы.

Класс GUI:

private JButton button1;
private JTextField textfield5; // First value
private JTextField textfield7; // Second value
public JTextField textfield9;  // Outcome - I made it public because I would get an error of 'cannot find symbol setText()' in the Module Class (totalHours() method)

// So when I click on the Button I want the textfield9 to show the method (totalHours()) from the Module class
button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) 
        {
            Module module = new Module();
            int text = module.totalHours();
            String textt = Integer.valueOf(text).toString();
            textfield9.setText(textt);
        }
    });

Модульный класс:

Это результат, который я хочу, чтобы он отображался в textfield9 всякий раз, когда я нажимаю на кнопку в рамке

public int totalHours()
{
    int num1 = Integer.parseInt(getHoursWeek());        // getter from GUI Class - textfield5
    int num2 = Integer.parseInt(getTotalWeeksCourse()); // getter from GUI Class - textfield7
    int num3 = num1 * num2;
    gui.textfield9.setText(Integer.toString(num3));
    return num3;
}

Я не знаю почему, но в textfield9 ничего не отображается, вместо этого открывается еще один JFrame с 2 Exception:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at Module.totalHours(Module.java:49)
at GUI$1.actionPerformed(GUI.java:67)

Вот эти 2 строки:

int num1 = Integer.parseInt(getHoursWeek()); // Module Class totalHours() method
int text = module.totalHours();  // GUI Class button1 actionlistener

Вот полный код обоих классов. Удаление ненужных строк.

Класс GUI:

public class GUI extends JFrame 
{
    private JMenuBar menuBar;
    private JButton button1;
    private JLabel label5;
    private JLabel label7;
    private JLabel label9;
    private JTextField textfield5;
    private JTextField textfield7;
    public JTextField textfield9;

//Constructor 
public GUI()
{        
    setTitle("GUI");
    setSize(468,400);

    //pane with null layout
    JPanel contentPane = new JPanel(null);
    contentPane.setPreferredSize(new Dimension(468,400));
    contentPane.setBackground(new Color(192,192,192));

    button1 = new JButton();
    button1.setBounds(181,332,127,44);
    button1.setText("Invoer");

    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) 
        {
            Module module = new Module();
            int text = module.totalHours();
            String textt = Integer.valueOf(text).toString();
            textfield9.setText(textt);
        }
    });

    label5 = new JLabel();
    label5.setBounds(5,115,94,31);
    label5.setText("Module Nummer");

    label7 = new JLabel();
    label7.setBounds(274,14,156,33);
    label7.setText("Per module");

    label9 = new JLabel();
    label9.setBounds(276,57,90,35);
    label9.setText("Totaal uren");

    textfield5 = new JTextField();
    textfield5.setBounds(120,225,90,35);

    textfield7 = new JTextField();
    textfield7.setBounds(120,280,90,35);

    textfield9 = new JTextField();
    textfield9.setBounds(361,57,90,35);

    //adding components to contentPane panel
    contentPane.add(button1);
    contentPane.add(label5);
    contentPane.add(label7);
    contentPane.add(label8);
    contentPane.add(label9);
    contentPane.add(textfield5);
    contentPane.add(textfield7);
    contentPane.add(textfield9);

    //adding panel to JFrame and seting of window position and close operation
    getContentPane().add(contentPane);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    pack();
    setVisible(true);

    //initGUI();
}

 public static void main(String[] args)
 {
    System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() 
        {
            new GUI();
        }
    });
}

public String getTextfield1()
{
    String txtfield1 = textfield1.getText();
    return txtfield1;
}

public String getTextfield2()
{
    String txtfield2 = textfield2.getText();
    return txtfield2;
}

public String getTextfield3()
{
    String txtfield3 = textfield3.getText();
    return txtfield3;
}

public String getTextfield4()
{
    String txtfield4 = textfield4.getText();    
    return txtfield4;
}

public String getTextfield5()
{
    String txtfield5 = textfield5.getText();
    return txtfield5;
}

public String getTextfield7()
{
    String txtfield7 = textfield7.getText();
    return txtfield7;
}

public String getTextfield9()
{
    String txtfield9 = textfield9.getText();
    return txtfield9;
}
}

Модульный класс:

public class Module
{
private GUI gui;

/**
 * Constructor for objects of class Module
 */
public Module()
{
    gui = new GUI();
}

public String getCourseName()
{
    return gui.getTextfield1();
}

public String getSchoolDays()
{
    return gui.getTextfield2();
}

public String getModuleNumber()
{
    return gui.getTextfield3();
}

public String getWeekNumber()
{
    return gui.getTextfield4();
}

public String getHoursWeek()
{
    return gui.getTextfield5();
}

public String getTotalWeeksCourse()
{
    return gui.getTextfield7();
}

public int totalHours()
{
    int num1 = Integer.parseInt(getHoursWeek());    
    int num2 = Integer.parseInt(getTotalWeeksCourse());
    int num3 = num1 * num2;
    gui.textfield9.setText(Integer.toString(num3));
    return num3;
}
}

Извините, если мне трудно понять, что я пытаюсь сказать, я никогда не умел объяснять. Может ли кто-нибудь помочь мне с этим!?

1 ответ

Решение

Проблема с вашим кодом заключается в том, что вы создаете новый объект Module во внутреннем классе ActionListener при каждом нажатии кнопки, которая, в свою очередь, создает новый объект GUI и т. Д. У вас также есть неприятный потенциальный цикл - если вы переместите модуль из ActionListener, он создаст новый графический интерфейс, который создаст новый модуль, создаст новый графический интерфейс и так далее...

Есть несколько вопросов - я буду решать их один за другим:

Циклическая ссылка

Не создавайте новый графический интерфейс для каждого модуля и новый модуль для каждого интерфейса. Вместо этого - есть поле для ссылки на GUI и метод setGUI () в модуле:

public class Module
{
    private GUI gui;

    public void setGUI(GUI myGUI)
    {
        this.gui = myGUI;
    }
 ... // rest of Module class

вызвать это из конструктора GUI т.е.

module.setGUI(this);

Создание объектов модуля в ActionListener

Тогда проблема с этим блоком кода:

   button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) 
    {
        Module module = new Module();
        int text = module.totalHours();
        String textt = Integer.valueOf(text).toString();
        textfield9.setText(textt);
    }
});

В частности - эта строка:

Module module = new Module();

Вам нужен единственный экземпляр объекта Module, на который ссылается ваш объект GUI. Затем вы каждый раз читаете общее количество часов с того же объекта. Есть несколько вариантов, как вы это сделаете - я предложу один:

  1. Иметь модуль в качестве поля в вашем классе GUI
  2. Заставить класс GUI реализовать ActionListener
  3. добавьте ваш метод actionPerformed в класс GUI:

    public void actionPerformed(ActionEvent evt) { int text = module.totalHours(); String textt = Integer.valueOf(text).toString(); textfield9.setText(textt); }

  4. замените текущий код для добавления слушателя действия button1.addActionListener(this);

Исключение при получении ничего от метода totalHours()

Ты видишь

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

Если вы посмотрите на трассировку стека - вы найдете что-то вроде:

at Module.totalHours(Module.java:49)

Когда вы смотрите на ваш метод totalHours(), он читает текстовые поля GUI и пытается разобрать их в целые числа. Они пусты (если вы не ввели что-то в них), поэтому parseInt пытается создать целое число из пустой строки (""). Вам нужно добавить некоторые проверки ошибок для этого метода.

Другие вопросы по тегам