javagui图形问题

javagui图形问题,java,swing,user-interface,graphics2d,Java,Swing,User Interface,Graphics2d,我想做的是,每次单击时,Jpanel中都会出现一个正方形,但出于某种原因,Jpanel会阻塞这些正方形(单击边)。我真的不知道我做错了什么,谢谢你的帮助 import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class VIewa extends JFrame implements ActionListener { JRadioButton[]

我想做的是,每次单击时,Jpanel中都会出现一个正方形,但出于某种原因,Jpanel会阻塞这些正方形(单击边)。我真的不知道我做错了什么,谢谢你的帮助

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class VIewa extends JFrame implements ActionListener {

    JRadioButton[]      buttons;
    JRadioButton[]      colorBut;
    JButton             colorButton;
    JPanel     blankArea; 
    panel ppp;
    public VIewa(String title) {

        super(title);
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            } catch (Exception e) {}
        setLayout(new FlowLayout());

        add(new JLabel("shapes:"));
        ButtonGroup     operations = new ButtonGroup();
        buttons = new JRadioButton[8];
        buttons[0] = new JRadioButton("line", false);
        buttons[1] = new JRadioButton("FillRec", false);
        buttons[2] = new JRadioButton("HolRec", false);
        buttons[3] = new JRadioButton("FilCir", false);
        buttons[4] = new JRadioButton("HolCir", false);
        buttons[5] = new JRadioButton("FilPol", false);
        buttons[6] = new JRadioButton("HolPol", false);
        buttons[7] = new JRadioButton("Text", false);
        for (int i=0; i<8; i++) {
            add(buttons[i]);
            operations.add(buttons[i]);
            buttons[i].addActionListener(this);
        }
        add(new JLabel("colors:"));
        ButtonGroup     colo = new ButtonGroup();
        colorBut = new JRadioButton[8];
        colorBut[0] = new JRadioButton("red", false);
        colorBut[1] = new JRadioButton("orange", false);
        colorBut[2] = new JRadioButton("yellow", false);
        colorBut[3] = new JRadioButton("green", false);
        colorBut[4] = new JRadioButton("blue", false);
        colorBut[5] = new JRadioButton("black", false);
        colorBut[6] = new JRadioButton("gray", false);
        colorBut[7] = new JRadioButton("white", false);
        for (int i=0; i<8; i++) {
            add(colorBut[i]);
            colo.add(colorBut[i]);
            colorBut[i].addActionListener(this);
        }
        add(new JLabel("current color:"));
        colorButton = new JButton();
        add(colorButton); 
        ppp = new panel();
        add(ppp);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600,670);
    }

    public class panel extends JPanel implements MouseListener {

        private ArrayList<Point>  squares;
        public panel(){
              squares = new ArrayList<Point>(); 
            JPanel     blankArea;
            blankArea = new JPanel(); 
            blankArea.setLayout(null); 
            //blankArea.setOpaque(true); 
            blankArea.setBorder(BorderFactory.createLineBorder(Color.black)); 
            blankArea.setPreferredSize(new Dimension(450, 550)); 
            addMouseListener(this); 
            add(blankArea);
        }
        public void paintComponent(Graphics graphics) { 
            super.paintComponent(graphics);

            graphics.setColor(Color.black);
            for (Point center: squares)
                graphics.drawRect(center.x - 20, center.y - 20, 40, 40); 
        }
        public void mouseClicked(MouseEvent arg0) {}
        public void mouseEntered(MouseEvent arg0) {}
        public void mouseExited(MouseEvent arg0) {}
        public void mousePressed(MouseEvent event) {
            squares.add(event.getPoint()); 
            repaint(); }
        public void mouseReleased(MouseEvent arg0) {}
    }

    public static void main(String args[]) {
        JFrame frame = new VIewa("Graphics");
        JPanel framex = new JPanel(); 
        framex.add(new SquareCanvas()); 
        framex.setVisible(true); 
        frame.setVisible(true);
    }
    public void actionPerformed(ActionEvent arg0) {
    }
}
import java.awt.*;
导入java.awt.event.*;
导入java.util.*;
导入javax.swing.*;
公共类VIewa扩展JFrame实现ActionListener{
JRadioButton[]按钮;
JRadioButton[]彩色按钮;
JButton彩色按钮;
杰帕内尔空白区;
面板购买力平价;
公共视图A(字符串标题){
超级(标题);
试一试{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}捕获(例外e){}
setLayout(新的FlowLayout());
添加(新JLabel(“形状:”);
ButtonGroup操作=新建ButtonGroup();
按钮=新的JRadioButton[8];
按钮[0]=新的JRadioButton(“行”,false);
按钮[1]=新的JRadioButton(“FillRec”,false);
按钮[2]=新的JRadioButton(“HolRec”,false);
按钮[3]=新的JRadioButton(“FilCir”,false);
按钮[4]=新的JRadioButton(“HolCir”,false);
按钮[5]=新的JRadioButton(“FilPol”,false);
按钮[6]=新的JRadioButton(“HolPol”,false);
按钮[7]=新的JRadioButton(“文本”,false);
对于(int i=0;i
但出于某种原因,Jpanel正在封锁广场


什么意思,正方形不可见?这可能是因为
blankArea
覆盖了绘制正方形的父面板。

试试这个代码。我刚刚删除了您的“blankArea”面板

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

class VIewa extends JFrame implements ActionListener {

JRadioButton[] buttons;
JRadioButton[] colorBut;
JButton colorButton;
JPanel blankArea;
panel ppp;

public VIewa(String title) {

    super(title);
    try {
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    } catch (Exception e) {
    }
    setLayout(new FlowLayout());

    add(new JLabel("shapes:"));
    ButtonGroup operations = new ButtonGroup();
    buttons = new JRadioButton[8];
    buttons[0] = new JRadioButton("line", false);
    buttons[1] = new JRadioButton("FillRec", false);
    buttons[2] = new JRadioButton("HolRec", false);
    buttons[3] = new JRadioButton("FilCir", false);
    buttons[4] = new JRadioButton("HolCir", false);
    buttons[5] = new JRadioButton("FilPol", false);
    buttons[6] = new JRadioButton("HolPol", false);
    buttons[7] = new JRadioButton("Text", false);
    for (int i = 0; i < 8; i++) {
        add(buttons[i]);
        operations.add(buttons[i]);
        buttons[i].addActionListener(this);
    }
    add(new JLabel("colors:"));
    ButtonGroup colo = new ButtonGroup();
    colorBut = new JRadioButton[8];
    colorBut[0] = new JRadioButton("red", false);
    colorBut[1] = new JRadioButton("orange", false);
    colorBut[2] = new JRadioButton("yellow", false);
    colorBut[3] = new JRadioButton("green", false);
    colorBut[4] = new JRadioButton("blue", false);
    colorBut[5] = new JRadioButton("black", false);
    colorBut[6] = new JRadioButton("gray", false);
    colorBut[7] = new JRadioButton("white", false);
    for (int i = 0; i < 8; i++) {
        add(colorBut[i]);
        colo.add(colorBut[i]);
        colorBut[i].addActionListener(this);
    }
    add(new JLabel("current color:"));
    colorButton = new JButton();
    add(colorButton);
    ppp = new panel();
    add(ppp);
    ppp.setBorder(BorderFactory.createLineBorder(Color.black));
    ppp.setPreferredSize(new Dimension(450, 550));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(600, 670);
}

public class panel extends JPanel implements MouseListener {

    private ArrayList<Point> squares;

    public panel() {
        squares = new ArrayList<Point>();
        //JPanel blankArea;
        //blankArea = new JPanel();
        //blankArea.setLayout(null);
        //blankArea.setOpaque(true);
        //blankArea.setBorder(BorderFactory.createLineBorder(Color.black));
        //blankArea.setPreferredSize(new Dimension(450, 550));
        addMouseListener(panel.this);
        //add(blankArea);
    }

    @Override
    public void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);

        graphics.setColor(Color.black);
        for (Point center : squares) {
            graphics.drawRect(center.x - 20, center.y - 20, 40, 40);
        }
    }

    public void mouseClicked(MouseEvent arg0) {
        squares.add(arg0.getPoint());
        repaint();
    }

    public void mouseEntered(MouseEvent arg0) {
    }

    public void mouseExited(MouseEvent arg0) {
    }

    public void mousePressed(MouseEvent event) {
        squares.add(event.getPoint());
        repaint();
    }

    public void mouseReleased(MouseEvent arg0) {
    }
}

public static void main(String args[]) {
    JFrame frame = new VIewa("Graphics");
    JPanel framex = new JPanel();
    JPanel p = new JPanel();
    p.setBounds(new Rectangle(100,50));
    framex.add(p);
    framex.setVisible(true);
    frame.setVisible(true);
}

public void actionPerformed(ActionEvent arg0) {
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.util.*;
导入javax.swing.*;
类VIewa扩展JFrame实现ActionListener{
JRadioButton[]按钮;
JRadioButton[]彩色按钮;
JButton彩色按钮;
杰帕内尔空白区;
面板购买力平价;
公共视图A(字符串标题){
超级(标题);
试一试{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}捕获(例外e){
}
setLayout(新的FlowLayout());
添加(新JLabel(“形状:”);
ButtonGroup操作=新建ButtonGroup();
按钮=新的JRadioButton[8];
按钮[0]=新的JRadioButton(“行”,false);
按钮[1]=新的JRadioButton(“FillRec”,false);
按钮[2]=新的JRadioButton(“HolRec”,false);
按钮[3]=新的JRadioButton(“FilCir”,false);
按钮[4]=新的JRadioButton(“HolCir”,false);
按钮[5]=新的JRadioButton(“FilPol”,false);
按钮[6]=新的JRadioButton(“HolPol”,false);
按钮[7]=新的JRadioButton(“文本”,false);
对于(int i=0;i<8;i++){
添加(按钮[i]);
添加(按钮[i]);
按钮[i].addActionListener(此);
}
添加(新JLabel(“颜色:”);
ButtonGroup colo=新建ButtonGroup();
colorBut=新的JRadioButton[8];
colorBut[0]=新的JRadioButton(“红色”,false);
colorBut[1]=新的JRadioButton(“橙色”,假);
colorBut[2]=新的JRadioButton(“黄色”,假);
colorBut[3]=新的JRadioButton(“绿色”,假);
colorBut[4]=新的JRadioButton(“蓝色”,假);
colorBut[5]=新的JRadioButton(“黑色”,假);
colorBut[6]=新的JRadioButton(“灰色”,假);
colorBut[7]=新的JRadioButton(“白色”,假);
对于(int i=0;i<8;i++){
添加(但[i]);
colo.add(colorBut[i]);
colorBut[i].addActionListener(此);
}
添加(新JLabel(“当前颜色:”);
colorButton=newjbutton();
添加(彩色按钮);
ppp=新面板();
新增(购买力平价);
ppp.setboorder(BorderFactory.createLineBorder(Color.black));
ppp.setPreferredSize(新尺寸(450550));
setDefaultCloseOperation(关闭时退出);
设置大小(600670);
}
公共类面板扩展JPanel实现MouseListener{
私人广场;
公共事务委员会(){
squares=新的ArrayList();
//杰帕内尔空白区;
//blankArea=新的JPanel();
//blankArea.setLayout(空);
//blankArea.set不透明(true);
//blankArea.setboorder(BorderFactory.createLineBorder(Color.black));
//空白区域。设置首选尺寸(新尺寸(450550));
addMouseListener(panel.this);
//添加(空白区域);
}
@凌驾
公共虚空绘制组件(图形){
super.paintComponent(图形);
图形.设置颜色(颜色.黑色);
用于(点中心:正方形){
graphics.drawRect(center.x-20,center.y-20,40,40);
}
}
公共无效鼠标单击(鼠标事件arg0){
add(arg0.getPoint());
重新油漆();
}
公共无效鼠标事件(鼠标事件arg0){
}
public void mouseexitted(MouseEvent arg0){
}
公共无效鼠标按下(鼠标事件){
add(event.getPoint());
重新油漆();
}
公共无效MouseEvent arg0{
}
}
公共静态void main(字符串参数[]){
JFrame=新视图A(“图形”);
JPanel framex=新的JPanel();
JPanel p=新的JPanel();
p、 立根(新矩形(100,50));
framex.add(p);
framex.setVisible(true);
frame.setVisible(true);
}
已执行的公共无效操作(操作事件arg0){
}
}
您还可以分享您的“新SquareCanvas()”吗?