无法使用引用访问组件

  发布于 2023-02-08 13:35

我无法引用Square类中的类Colors.我之前已经创建了一个引用,但是在这种情况下,当类扩展另一个类(如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);

}

}

我可以使用Colors中的所有方法,但无法访问其所有组件.现在不需要类圈和矩形.我是新手

1 个回答
  • 您在其构造函数中声明所有Colors的组件变量.这意味着这些变量在构造函数之外是不可访问的.您希望将它们声明为类中的字段.

    换句话说,移动这些线:

    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(){
    

    2023-02-08 13:38 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有