Java 我有两个类,想要从一个类到另一个类得到一个变量
我有两节课。画啊画啊。在DrawGUI中,我有一个JPanel。对于我的JUnit测试,我需要为getWidth()和getHeight()请求类Draw。因此,我的代码如下所示:Java 我有两个类,想要从一个类到另一个类得到一个变量,java,swing,awt,Java,Swing,Awt,我有两节课。画啊画啊。在DrawGUI中,我有一个JPanel。对于我的JUnit测试,我需要为getWidth()和getHeight()请求类Draw。因此,我的代码如下所示: public class Draw { public static void main(String[] args) throws ColorException {new Draw();} /** Application constructor: create an instance of our GUI cla
public class Draw {
public static void main(String[] args) throws ColorException {new Draw();}
/** Application constructor: create an instance of our GUI class */
public Draw() throws ColorException { window = new DrawGUI(this); }
protected JFrame window;
public void getWidth(){
}
}
class DrawGUI extends JFrame {
JPanel drawPanel;
public DrawGUI(Draw application) throws ColorException {
super("Draw"); // Create the window
app = application;
drawPanel = new JPanel();
}
}
那么如何实现getWidth呢?getWidth应该从JPanel drawPanel返回宽度一个选项是更改保存
窗口的弱类型
:
public class Draw {
public static void main(String[] args) throws ColorException {new Draw();}
/** Application constructor: create an instance of our GUI class */
public Draw() throws ColorException { window = new DrawGUI(this); }
protected DrawGUI window; // <- is now a DrawGUI
public int getWidth(){
return window.getPanelWidth();
}
}
class DrawGUI extends JFrame {
JPanel drawPanel;
...
public DrawGUI(Draw application) throws ColorException {
super("Draw"); // Create the window
app = application;
drawPanel = new JPanel();
}
public int getPanelWidth() { // <- added method to get panel width
return drawPanel.getWidth();
}
}
公共类绘图{
公共静态void main(字符串[]args)抛出ColorException{new Draw();}
/**应用程序构造函数:创建GUI类的实例*/
public Draw()抛出ColorException{window=new DrawGUI(this);}
受保护的DrawGUI窗口;//返回窗口。getWidth();
?@jornverne我希望从JPanel DrawPanel获取getWidth,而不是整个窗口。