Java:在ActionListener中使用图形组件

我有两个单独的类和驱动程序文件,在类文件中我创建了paint方法:

public void paint(Graphics g){
g.drawLine(......
....
//along with all of my other draw commands
}

在代码中,我创建了一个JButton,在这个按钮的动作监听器中,我不知道如何使用Graphics对象在JFrame中创建更多图形.我应该在我的驱动程序中添加一些内容来实现这一点,还是有办法在我的动作监听器中使用这些图形?谢谢,任何帮助表示赞赏.

解决方法:

您需要在paint方法中绘制所有内容. actionPerformed只应该改变paint方法中已有的东西的状态,然后调用repaint.例如

boolean drawHello = true;
boolean drawWorld = false;

protected void paintComponent(Graphics g) {
    super.paintCompoent(g);
    if (drawHello)
             g.drawString("Hello", 50, 50);

    if (drawWorld)
             g.drawString("World", 10, 10);
}

然后在actionPerformed中,您可以将drawWorld的状态更改为true并调用repaint().

public void actionPerformed(ActionEvent e) {
    drawWorld = true;
    repaint();
}

如您所见,应该在paintComponent方法中绘制所有内容.您可以隐藏和绘制渲染图,并使其从动作命令中“可见”.您应该已经预定了可以绘制的内容.然后只需更改渲染状态即可

正如@MadPrgrammer所指出的那样,你不应该在像JFrame这样的*容器上绘画.而是在自定义JPanel或JComponent上绘制并覆盖paintComponent方法,而不是JFrame和paint

这是一个例子,每次按下按钮我都会画一个新的方块.如果看一下代码,你会看到在paintComponent方法中,我遍历一个Squares列表并绘制它们,在actionPerformed中,我所做的就是在List中添加一个新的Square并调用repaint()

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class AddSquares {

    private int R = 0;
    private int G = 0;
    private int B = 0;
    private int xLoc = 0;
    private int yLoc = 0;

    List<Square> squares = new ArrayList<>();
    private JButton addSquare = new JButton("Add Square");
    private RectsPanel panel = new RectsPanel();

    public AddSquares() {

        addSquare.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                Color color = new Color(R, G, B);
                squares.add(new Square(xLoc, yLoc, color));
                panel.repaint();
                R += 10;
                G += 20;
                B += 30;
                xLoc += 20;
                yLoc += 20;

            }
        });

        JFrame frame = new JFrame("Draw Squares");
        frame.add(panel, BorderLayout.CENTER);
        frame.add(addSquare, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private class RectsPanel extends JPanel {

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (Square square : squares) {
                square.drawSquare(g);
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(250, 250);
        }
    }

    private class Square {
        int x = 0; 
        int y = 0;
        Color color;

        public Square(int x, int y, Color color) {
            this.x = x;
            this.y = y;
            this.color = color;
        }

        public void drawSquare(Graphics g) {
            g.setColor(color);
            g.fillRect(x, y, 75 ,75);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                AddSquares addSquares = new AddSquares();
            }
        });
    }
}
上一篇:c#using和Java import之间的区别


下一篇:java – 如何使用键绑定使矩形在屏幕上移动?