从另一个类Java / BlueJ更改JTextField

时间:2015-01-13 14:25:05

标签: java swing jframe jpanel bluej

我正在使用JFrame进行项目(第一次)。我在框架上有一些JTextFields和1个JButton。所以我想要做的是从另一个类编辑我的JTextField的文本。我在JTextField中输入2个值,我希望结果显示在另一个JTextField中。

每当我按下Button时,我想要将一个JTextField textfield9更改为我在其他类的方法中计算的值(在本例中为Module中的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 in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at Module.totalHours(Module.java:49)
at GUI$1.actionPerformed(GUI.java:67)

这两行:

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 个答案:

答案 0 :(得分:1)

您的代码的问题在于,每次单击按钮时都会在ActionListener内部类中创建一个新的Module对象,而该按钮又会创建一个新的GUI对象等等...您还有一个令人讨厌的问题潜在的循环 - 如果你将模块移出ActionListener,它将创建一个新的GUI,它将创建一个新的模块,创建一个新的GUI等等......

有一些问题 - 我将逐一解决这些问题:

周期性参考

不要为每个模块创建新的GUI,也不要为每个GUI创建新的模块。相反 - 有一个用于引用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

中创建Module对象

然后问题在于这段代码:

   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();

您需要从GUI对象引用的Module对象的单个实例。然后,每次都从同一个对象中读取totalHours。如何做到这一点有几个不同的选择 - 我会提出一个:

  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);

  5. 替换当前用于添加动作侦听器的代码

    从totalHours()方法

    中没有收到任何内容的例外情况

    你看

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

    如果你向下看堆栈跟踪 - 你会发现类似的东西:

    at Module.totalHours(Module.java:49)
    

    当您查看totalHours()方法时,它正在读取GUI文本字段并尝试将它们解析为整数。它们是空的(除非你在它们中输入了一些东西),所以parseInt试图从空字符串(“”)创建一个整数。您需要为该方法添加一些错误检查。

相关问题