2013-12-23 61 views
0

我无法对类Square的类Color进行参考。我之前创建了一个引用,但是在这种情况下,当类扩展另一个类(如Canvas)时,这似乎正在发生。无法使用参考访问组件

这里是我的代码:

颜色:

import java.awt.*; 

import javax.swing.*; 




public class Colours extends Canvas { 

Colours(){ 
JPanel menupn; 
ButtonGroup group; 
JRadioButton square; 
JRadioButton rect; 
JRadioButton circle; 
JFrame frame; 
JPanel sqpn; 
JPanel crpn; 
JPanel rtpn; 
Circle Circle; 
Rect Rect; 
Square Square; 

Circle = new Circle(); 
Rect = new Rect(); 
Square = new Square(this); 
menupn = new JPanel(); 
group = new ButtonGroup(); 
square = new JRadioButton("Square"); 
rect = new JRadioButton("Rectangle"); 
circle = new JRadioButton("Circle"); 
frame = new JFrame("Colours"); 

frame.setSize(1000,500); 
frame.setLayout(null); 

group.add(square); 
group.add(circle); 
group.add(rect); 
group.setSelected(square.getModel(),true); 

square.addActionListener(Square); 

circle.addActionListener(Circle); 

rect.addActionListener(Rect); 

menupn.setLayout(new GridLayout(3,1)); 
menupn.add(square); 
menupn.add(circle); 
menupn.add(rect);  
menupn.setBounds(0, 360, 1000, 100); 

this.setBackground(new Color(255,255,255)); 
this.setBounds(0,0,1000,400); 

frame.add(menupn); 
frame.add(this); 



frame.setVisible(true); 


} 

public void paint(Graphics g){ 

    g.fillRect(0,0,50,50); 
    g.fillOval(100,100,50,50); 
    g.fillRect(200,200,100,50); 
} 



public static void main(String[] args) { 

    Colours Colours = new Colours(); 

} 
} 

形状:

import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JTextField; 
import javax.swing.JPanel; 
import javax.swing.JLabel; 



public class Square implements ActionListener { 
Colours Colours; 
JPanel panel; 
JTextField colfld1; 
JTextField colfld2; 
JTextField colfld3; 
JTextField locx; 
JTextField locy; 


Square(Colours Colours){ 
    this.Colours = Colours; 
} 

public void actionPerformed(ActionEvent e) { 

    panel = new JPanel(); 
    colfld1 = new JTextField(3); 
    colfld2 = new JTextField(3); 
    colfld3 = new JTextField(3); 
    locx = new JTextField(4); 
    locy = new JTextField(3); 
    JLabel positionx = new JLabel("X Axis Position"); 
    JLabel positiony = new JLabel("Y Axis Position"); 
    JLabel rgb = new JLabel("RGB Value"); 
    panel.setBackground(new Color(0,0,0)); 
    panel.setBounds(0, 0, 100, 200); 

} 

} 

我可以使用颜色的所有方法,但不访问它的所有部件。现在不需要类圆和矩形。我是一个新手

+2

我将开始通过其通过[Java编程语言代码规范(http://www.oracle读。 com/technetwork/java/codeconv-138413.html),不要混合重量和重量轻的组件,并了解[布局管理](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html)工作。我也会考虑看看[执行自定义绘画](http://docs.oracle.com/javase/tutorial/uiswing/painting/)和[在AWT和Swing中绘画](http://www.oracle。 com/technetwork/java/painting-140037.html)让你了解painitng是如何工作的 – MadProgrammer

+0

是什么问题。它看起来像'Square'里面的'Colors','Square'的构造函数中有'this.Colours = Colors;'行​​。它给你一个错误? –

+0

@Sam我是否它不是 – user3130960

回答

5

你声明所有颜色的组件变量在其构造函数。这意味着这些变量在构造函数之外是不可访问的。您想要将它们声明为类中的字段。

换句话说,将这些行:

JPanel menupn; 
ButtonGroup group; 
JRadioButton square; 
JRadioButton rect; 
JRadioButton circle; 
JFrame frame; 
JPanel sqpn; 
JPanel crpn; 
JPanel rtpn; 
Circle Circle; 
Rect Rect; 
Square Square; 

这条线之上:

Colours(){ 
+0

+1 - 出于好奇,你知道它是否是java中的一个问题吗?实例化一个类的变量作为类名本身,例如'Colors Colours'?我从来没有见过,并试图找到答案 – fdsa

+0

@fdsa除了可读性,不,我不认为有问题 – MadProgrammer

+0

@MadProgrammer好吧,知道我猜。 – fdsa

3

所有的变量中Colours是本地类的构造函数。这意味着它们永远不能在构造函数的外部访问。

考虑两件事情......

  1. 移动变量,你可能要在稍后访问,构造之外
  2. 考虑使用setter和getter方法来访问这些变量,你想外面类有权访问。这可以防止其他类在的方式与Colours搞乱你不把它们想

我阿洛斯建议你有一个读通过Code Conventions for the Java Programming Language,不能混用重和轻重量(CanvasJPanel)组件,并了解如何layout manages工作。

我还要考虑考虑看看Performing Custom PaintingPainting in AWT and Swing让您了解painitng是如何工作的

+0

另外,你也是对的。 – user3130960