我無法對類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);
}
}
我可以使用顏色的所有方法,但不訪問它的所有部件。現在不需要類圓和矩形。我是一個新手
我將開始通過其通過[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
是什麼問題。它看起來像'Square'裏面的'Colors','Square'的構造函數中有'this.Colours = Colors;'行。它給你一個錯誤? –
@Sam我是否它不是 – user3130960