【问题标题】:Java calling a method from my main class from another classJava 从另一个类调用我的主类中的方法
【发布时间】:2014-11-01 02:27:17
【问题描述】:

同一个包中的两个类,

具有公共静态变量和实例变量组合的主类。 它创建了一个名为instance 的类的实例。

另一个类Login,不是子类,它是业务,但我想从main 调用guiInstance 的方法。

问题是我似乎无法让它工作,我无法让Login 类找到“gui”实例或类的任何方法,尽管它们是公开的。

我刚刚使用我想要的配置制作了一个新包,显然我对调用方法的工作方式有错误的想法。

public class GUI {

    JPanel jp = new JPanel();

    public static void main(String[] args) {
        GUI instance = new GUI();
    }

    public void ping(){
        System.out.println("that worked");
    }
}


public class Login extends JPanel {

    /** Creates new form Login */
    public Login() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        jButton1.setText("jButton1");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(112, 112, 112)
                .addComponent(jButton1)
                .addContainerGap(215, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jButton1)
                .addContainerGap(231, Short.MAX_VALUE))
        );
    }// </editor-fold>

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
        instance.ping();
    }


    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration

}

【问题讨论】:

    标签: java swing methods main invoke


    【解决方案1】:

    您的main() 方法中有一个GUI 的实例,但您似乎没有创建Login 的实例。我建议您将 GUI 实例传递给您的 Login 构造函数,因此在 Login 中添加类似 -

    private GUI gui = null;
    public Login(GUI gui) {
      this.gui = gui;
      initComponents();
    }
    

    然后在main() 你可以做类似的事情,

    public static void main(String[] args) {
      GUI instance = new GUI();
      Login login = new Login(instance);
      // next I believe you need to make your Frame and components visible
    }
    

    【讨论】:

    • 请注意,这不是我以前做过的事情,当你不知道它叫什么时,不可能用谷歌搜索你认为应该发生的事情。现在一切看起来都那么简单!谢谢!!!
    • Nitpick:虽然肯定是正确的,但就个人而言,我更愿意将某种接口传递给 Login,以免暴露出 Login 不需要知道或应该能够知道的 GUI 元素访问
    • 谢谢MadProgrammer,请问您有实现该功能的代码指针吗?
    猜你喜欢
    • 2016-08-13
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多