简体   繁体   English

java的Swing按钮动作

[英]java Swing button action

I am new in java and i wanted to work on a simple paint program using java swing. 我是Java的新手,我想使用Java swing开发一个简单的绘画程序。 my simple paint program should draw a shape like triangle, circle and square whenever i clicked on buttons. 每当单击按钮时,我的简单绘画程序便应绘制一个三角形,圆形和正方形这样的形状。 i managed to draw these shapes and print it without buttons but i can not do it using ActionListener? 我设法绘制这些形状并在没有按钮的情况下将其打印出来,但是我无法使用ActionListener做到这一点?

As you see i have a single button at the moment, i want to draw the oval whenever this button is clicked. 如您所见,目前我只有一个按钮,只要单击此按钮,我就想绘制一个椭圆。 This is the code that i am working on it so far: 到目前为止,这是我正在处理的代码:

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


    }


}

Could you please take following steps: 您能否采取以下步骤:

Step 1: 第1步:

Insert button1.addActionListener(paint); 插入button1.addActionListener(paint); just after PaintProject paint=new PaintProject(); 就在PaintProject paint=new PaintProject(); in the main method of PaintProject.java PaintProject.javamain方法中

Step 2: 第2步:

Remove the method named protected void paintComponent(Graphics g) . 删除名为protected void paintComponent(Graphics g) Instead create following method: 而是创建以下方法:

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

Step 3: 第三步:

Call the above method as follows: 调用上述方法如下:

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

EDIT: 编辑:

Following example demonstrates how to draw two shapes when respective buttons are clicked: 下面的示例演示如何在单击相应按钮时绘制两个形状:

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();
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM