2013-12-23 58 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