【问题标题】:2 classes, constructor and main not in the same class2个类,构造函数和main不在同一个类中
【发布时间】:2014-03-05 08:20:58
【问题描述】:

Grafics 的班级有 2 个班级。我想把所有grafic 的东西,比如创建框架、按钮等,放在inhalt 类中。我想用grafics创建所有函数。

我的问题:

Main 是否可以在 inhalt 类中,而构造函数在 grafics 类中?

package uebung2;   
import javax.swing.JFrame;    
public class Grafics extends JFrame {       
    private static final long serialVersionUID = 1L;    
    public Grafics(){           
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
    }       
}

带有main方法的类:

package uebung2;    
public class Inhalt {       
    public void main(String[]args){         
        Grafics fenster = new Grafics();    
    }    
}

【问题讨论】:

  • 有什么困难或错误,不如自己试一试,贴出来

标签: java class user-interface constructor


【解决方案1】:

当然是这样 - main 只是程序入口点,通常最好不要将应用程序粘合(如启动程序、设置环境)和程序的“业务逻辑”混为一谈。

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 2014-08-01
    • 2012-06-22
    • 1970-01-01
    • 2014-03-12
    • 2013-08-04
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多