java gui图形问题

发布于 2024-10-23 22:50:42 字数 3830 浏览 4 评论 0原文

我想做的是每次点击时,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) {
    }
}

what i'm trying to do is everytime i click, a square should come up in the Jpanel, but for some reason the Jpanel is blocking the squares(click on the edges). I'm really not sure what i did wrong, thanks for your help!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当梦初醒 2024-10-30 22:50:42

试试这个代码。我刚刚删除了你的“空白区域”面板..

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

try this code. I just removed your 'blankArea' panel..

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) {
}
}
白馒头 2024-10-30 22:50:42

但由于某种原因,Jpanel 挡住了方块

您的意思是,方块不可见?这可能是因为 blankArea 覆盖了您在其中绘制正方形的父面板。

but for some reason the Jpanel is blocking the squares

What do you mean, the squares are not visible? This might be because blankArea overlays the parent panel in which you draw the squares.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文