维持变量的价值

时间:2014-06-28 19:40:41

标签: java swing

我的代码如下:

private void okJButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    srcLang=(srcLangJComboBox.getSelectedItem().toString());
    targLang=(targLangJComboBox.getSelectedItem().toString());
           infoNew.this.setVisible(false);
}                                         

/**
 * @param args the command line arguments
 */
public static String[] newButton() {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(infoNew.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(infoNew.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(infoNew.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(infoNew.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new infoNew().setVisible(true);
        }
    });
    System.out.println(srcLang);
    return new String[]{srcLang,targLang};
}

有两个函数较低,一个调用upper函数作为jframe按钮,upper函数接收两个变量的值,这些变量本质上是公共的,但是在这里分配它们的值在lower函数中不可用。

在函数okJButtonActionPerformed(java.awt.event.ActionEvent evt)中分配给变量srcLang和tarLang的值

中没有

函数newButton()。

P.S。变量srcLang和targLang声明为public static

2 个答案:

答案 0 :(得分:2)

不要使用静态方法。

相反,您需要创建一个包含代码的类,以便定义instance个变量。然后,一旦创建了实例变量,就可以从类中的任何方法访问该变量。

可能从How to Use Buttons上的Swing教程中的ButtonDemo示例代码开始。它显示了如何使用按钮的实例变量创建面板。然后可以在该类的其他方法中访问这些按钮。

答案 1 :(得分:1)

这篇文章的答案(虽然没有正式的问题)是方法newButton()是静态的,变量srcLangtarLang是实例变量。

更新答案:(不是最佳做法)

可以使用staticinstance方法调用static方法。 instance方法需要调用类的instance。您可以做的是传递您正在使用的实例的副本(如果您在实例方法中工作,可以使用this关键字)并从静态方法调用实例方法,但是,我建议您试图按照我在此处描述的方法static制作方法:

您还有另一个我想到的选项:您可以将okJButtonActionPerformed函数中的实例变量转换为static,这应该没问题。

private static void okJButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          

答案的较旧部分:(最佳实践)

静态方法不需要运行类的实例,而实例变量确实需要对象的正式声明。

要解决此问题,您可以尝试删除static方法前面的newButton()

更改:public static String[] newButton() {

至:public String[] newButton() {

此时,在没有看到更多代码的情况下(因为没有主要方法,我不知道从哪里调用这些方法),这与我能提供的信息差不多。如果您想要更详细的答案,请发布更多示例代码。