java 摆动按钮动作

java Swing button action

我是 java 的新手,我想使用 java swing 开发一个简单的绘画程序。 每当我点击按钮时,我的简单绘图程序应该绘制三角形、圆形和正方形等形状。 我设法绘制了这些形状并在没有按钮的情况下打印出来,但我不能使用 ActionListener 来完成?

如您所见,我现在只有一个按钮,我想在单击此按钮时绘制椭圆形。 这是我目前正在处理的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class PaintProject extends JComponent implements ActionListener{
    public static void main(String[] args) {


        JFrame frame=new JFrame("NEW PAINT PROGRAME!");
        JButton button1=new JButton("ADD");
        PaintProject paint=new PaintProject();

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(paint);
        frame.add(button1);

        frame.pack();
        frame.setVisible(true);

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

    }
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillOval(0,0, 100, 100);

    }

    @Override
    public void actionPerformed(ActionEvent e) {


    }


}

能否请您执行以下步骤:

第 1 步:

PaintProject.java

main 方法的 PaintProject paint=new PaintProject(); 之后插入 button1.addActionListener(paint);

第 2 步:

删除名为 protected void paintComponent(Graphics g) 的方法。而是创建以下方法:

 private void drawOval(){
    Graphics g = this.getGraphics();
    g.setColor(Color.red);
    g.fillOval(0,0, 100, 100);        
 }

第 3 步:

调用上面的方法如下:

@Override
public void actionPerformed(ActionEvent e) {
     drawOval();      
}

编辑:

以下示例演示如何在单击相应按钮时绘制两个形状:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class PaintProject extends JComponent implements ActionListener {
    public static void main(String[] args) {

        JFrame frame = new JFrame("NEW PAINT PROGRAME!");
        JButton ovalButton = new JButton("Oval");
        ovalButton.setActionCommand("Oval");

        JButton rectangleButton = new JButton("Rectangle");
        rectangleButton.setActionCommand("Rectangle");

        PaintProject paint = new PaintProject();
        ovalButton.addActionListener(paint);
        rectangleButton.addActionListener(paint);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(paint);
        frame.add(ovalButton);
        frame.add(rectangleButton);

        frame.pack();
        frame.setVisible(true);

    }

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

    }

    private void drawOval() {
        Graphics g = this.getGraphics();
        g.setColor(Color.red);
        g.fillOval(0, 0, 100, 100);
    }

    private void drawRectangle() {
        Graphics g = this.getGraphics();
        g.setColor(Color.green);
        g.fillRect(150, 150, 100, 100);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals("Oval")) {
            drawOval();
        } else if (command.equals("Rectangle")) {
            drawRectangle();
        }

    }

}