【问题标题】:Need some help calling another another class's method in another class需要一些帮助在另一个类中调用另一个类的方法
【发布时间】:2013-08-01 07:28:18
【问题描述】:

我是 Java 新手,正在学习尝试了解 OOP。

我正在尝试集成课程并通过 get secondwindow.form2('null') 工作。

我不知道如何将变量传递给其他类。

任何帮助将不胜感激!

public class helloworld {

    public static void main(String[] args){

        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                JFrame window = new JFrame("Hello world test");
                window.setVisible(true);
                window.setSize(500,500);
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JOptionPane.showMessageDialog(window, "do NOT click 'OK'");
                String word = JOptionPane.showInputDialog(window,"Enter a sentence");
                int characters =word.length();
                JOptionPane.showMessageDialog(window, characters);
                NewDialouge secondwindow = new NewDialouge();
                secondwindow.form2('null');
            }   
        });         
    }

}

import javax.swing.JOptionPane;

public class NewDialouge {

    public void form2(String spoon){

        JOptionPane.showMessageDialog(spoon,  "works.");

    }

}

【问题讨论】:

  • 对字符串使用双引号。为什么你有'null',而在所有其他地方你都使用了正确的格式?
  • 像这样的单引号:'a' 用于字符,像这样的双引号:"foobar" 用于字符串
  • 注意:无需在标题中添加“[JAVA]”——标签能更好地传达信息。
  • 你的标题中有很多“另一个”...

标签: java variables joptionpane


【解决方案1】:
secondwindow.form2('null');

错了,在Java中字符是用单引号括起来的,对于Strings你需要使用双引号。

【讨论】:

    猜你喜欢
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多