问题描述
很简单的问题,但我做不到.我有 3 个班级:
Very simple question but I can't do it. I have 3 classes:
DrawCircle
类
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
类
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
类
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.
这篇关于从另一个类访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!