我无法对类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