问题描述
非常简单的问题,但我不能这样做。我有3个班级:
Very simple question but I can't do it. I have 3 classes:
DrawCircle
class
DrawCircle
class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DrawCircle extends JPanel
{
private int w, h, di, diBig, diSmall, maxRad, xSq, ySq, xPoint, yPoint;
public DrawFrame d;
public DrawCircle()
{
w = 400;
h = 400;
diBig = 300;
diSmall = 10;
maxRad = (diBig/2) - diSmall;
xSq = 50;
ySq = 50;
xPoint = 200;
yPoint = 200;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.blue);
g.drawOval(xSq, ySq, diBig, diBig);
for(int y=ySq; y<ySq+diBig; y=y+diSmall*2)
{
for(int x=xSq; x<w-xSq; x=x+diSmall)
{
if(Math.sqrt(Math.pow(yPoint-y,2) + Math.pow(xPoint-x, 2))<= maxRad)
{
g.drawOval(x, y, diSmall, diSmall);
}
}
}
for(int y=ySq+10; y<ySq+diBig; y=y+diSmall*2)
{
for(int x=xSq+5; x<w-xSq; x=x+diSmall)
{
if(Math.sqrt(Math.pow(yPoint-y,2) + Math.pow(xPoint-x, 2))<= maxRad)
{
g.drawOval(x, y, diSmall, diSmall);
}
}
}
}
}
DrawFrame
class
DrawFrame
class
public class DrawFrame extends JFrame
{
public DrawFrame()
{
int width = 400;
int height = 400;
setTitle("Frame");
setSize(width, height);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Container contentPane = getContentPane();
contentPane.add(new DrawCircle());
}
}
CircMain
class
CircMain
class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CircMain
{
public static void main(String[] args)
{
JFrame frame = new DrawFrame();
frame.show();
}
}
一个类创建一个框架,另一个类绘制一个圆圈并用较小的圆圈填充它。在 DrawFrame
中设置宽度和高度。在 DrawCircle
中,我需要访问 DrawFrame
的宽度和高度。我该怎么做?
One class creates a frame, the other draws a circle and fills it with smaller circles. In DrawFrame
I set width and height. In DrawCircle
I need to access the width and height of DrawFrame
. How do I do this?
我试过制作一个对象并尝试使用 .getWidth
和 .getHeight
但无法让它工作。我需要特定的代码,因为我已经尝试了很多东西,但无法让它工作。我是否在 DrawFrame
中声明宽度和高度错误?我在 DrawCircle
中错误地创建对象?
I've tried making an object and tried using .getWidth
and .getHeight
but can't get it to work. I need specific code here because I've tried a lot of things but can't get it to work. Am I declaring width and height wrong in DrawFrame
? Am creating the object the wrong way in DrawCircle
?
此外,我在 DrawCircle ,我是否应该在构造函数中使用它们?
Also, the variables i use in DrawCircle
, should I have them in the constructor or not?
推荐答案
你可以制作变量公共字段:
You could make the variables public fields:
public int width;
public int height;
DrawFrame() {
this.width = 400;
this.height = 400;
}
然后你可以像这样访问变量:
You could then access the variables like so:
DrawFrame frame = new DrawFrame();
int theWidth = frame.width;
int theHeight = frame.height;
然而,更好的解决方案是使变量私有字段为您的类添加两个访问器方法,保持DrawFrame类中的数据封装:
A better solution, however, would be to make the variables private fields add two accessor methods to your class, keeping the data in the DrawFrame class encapsulated:
private int width;
private int height;
DrawFrame() {
this.width = 400;
this.height = 400;
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
然后你可以像这样得到宽度/高度:
Then you can get the width/height like so:
DrawFrame frame = new DrawFrame();
int theWidth = frame.getWidth();
int theHeight = frame.getHeight();
我强烈建议你使用后一种方法。
I strongly suggest you use the latter method.
这篇关于从另一个类访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!