【问题标题】:Calling another method java GUI [closed]调用另一种方法java GUI [关闭]
【发布时间】:2013-12-31 22:35:11
【问题描述】:

我正在尝试制作一个 GUI,我可以用一种方法完成所有操作,但我想让代码更简单并制作多种方法。但是,我无法让它工作。我是 Java 编程新手。

public class Main {
    public static void main(String[] args) {
        FirstWindow fw = new FirstWindow();

        fw.setVisible(true);
        fw.setSize(600,400);
    }
}

public class FirstWindow extends JFrame {

    public FirstWindow() {
        checkbox c = new checkbox();
        c();
    }
}


public class checkbox extends JFrame {

    public checkbox() {
          //code
    }
}

【问题讨论】:

  • I am new to java programming. - 是时候做一些阅读了。从 How to Make Frames 上的 Swing 教程部分开始,它包含示例和解释,以帮助您更好地构建代码。并且不要忘记查看目录以获取使用其他 Swing 组件的示例。
  • 这个问题有点含糊。你还问什么?
  • 为什么要使用多个框架?
  • 我的问题有点含糊,对不起。我可以让程序在一个类/方法 FirstWindow 中正常工作,但是该方法的代码很长。我试图将代码分成多种方法,以使代码更有条理,并且仍然能够使用一帧。我会阅读更多内容并感谢所有帮助。
  • 在学习 Swing 之前,你会想要一本 Java 基础书籍,并首先学习 Java。

标签: java swing user-interface methods multiple-instances


【解决方案1】:

我不确定您要做什么,但有一点需要考虑:c(); 不会做任何事情。 ccheckbox 类的一个实例,不是要调用的方法。所以考虑一下:

public class FirstWindow extends JFrame {

    public FirstWindow() {
        checkbox c = new checkbox();
        c.yourMethod(yourParameters); // call the method you made in checkbox
    }
}

public class checkbox extends JFrame {

    public checkbox(yourParameters) { 
        // this is the constructor method used to initialize instance variables
    }

    public void yourMethod() // doesn't have to be void
    {
        // put your code here
    }
}

【讨论】:

    猜你喜欢
    • 2014-10-18
    • 2011-07-05
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2013-02-10
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多