java Swing按钮动作

时间:2016-07-10 11:46:30

标签: java swing actionlistener

我是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 个答案:

答案 0 :(得分:0)

请你采取以下步骤:

第1步:

button1.addActionListener(paint); PaintProject paint=new PaintProject(); main方法PaintProject.java后面protected void paintComponent(Graphics g)插入 private void drawOval(){ Graphics g = this.getGraphics(); g.setColor(Color.red); g.fillOval(0,0, 100, 100); }

第2步:

删除名为@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();
        }

    }

}

第3步:

按如下方式调用上述方法:

{{1}}

修改

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

{{1}}