作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如
头等舱:
class Stuck {
private int i;
public void WhatIwant(){
i = FirstClass.getWidht();
}
}
主类:
public class FirstClass extends applet{
@Override
public void init() {
setSize(500, 500);
}
}
我尝试创建新的 FirstClass 但显示 0?请帮助我,我被困住了。我首先在 Google 中尝试。
最佳答案
就像我在评论中解释的那样,当您调用 FirstClass.getWidth()
时,Applet
的 init()
方法没有还没被叫到呢而且,更重要的是,您无法像这样访问 FirstClass
的非静态 getWidth()
方法!。确保您创建了一个 FirstClass 对象来访问它,如下所示:
FirstClass fc = new FirstClass();
fc.init(); // Not recommended.
// Now, you can get the width.
但不建议这样做。因此,您应该采用这种方法:
1. Create a method "getPreferredSize()" in the "FirstClass". Declare and
initialize two private variables namely "width" and "height":
public class FirstClass extends Applet {
private int width = 400;
private int height = 300;
...
@Override
public void Dimension getPreferredSize() {
return new Dimension(width,height);
}
...
2. Or you can directly return the preferred width as:
...
public int getPreferredWidth() {
return width;
// OR:
// return (int)getPreferredSize().getWidth();
} //and the same for height
关于java - 如何从小程序类获取宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22740716/
我是一名优秀的程序员,十分优秀!