GUI Java程序-绘画程序 [英] GUI Java Program - Paint Program

查看:89
本文介绍了GUI Java程序-绘画程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找出我的代码在哪里出了问题.这个想法是创建一个小的Paint程序并具有红色,绿色,蓝色和透明按钮.我拥有所有可以想到的一切,但无法弄清楚代码出了什么问题.该程序将打开,然后立即关闭.

I've been trying to figure out what is wrong with my code here. The idea is to create a small Paint program and to have red, green, blue, and clear buttons. I have everything that I can think of for it to work, but can't figure out what is wrong with the code. The Program opens, and immediately closes.

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

public class Paint{

    public static void main(String[] args){
            gui g = new gui();
            g.setVisible(true);

    }

}

 public class gui extends JComponent implements ActionListener{
    JButton red, green, blue, clear;
    Image image;
    Graphics2D draw;
    int x, y, prevX, prevY;

    gui(){
            JFrame frame = new JFrame("Paint");
            Container content = frame.getContentPane();
            content.setLayout(new BorderLayout());
            setDoubleBuffered(false);

            JPanel panel = new JPanel();
            content.add(panel, BorderLayout.SOUTH);
            panel.setPreferredSize(new Dimension(32, 68));
            panel.setMinimumSize(new Dimension(32, 68));
            panel.setMaximumSize(new Dimension(32, 68));


            red = new JButton("Red");
            green = new JButton("Green");
            blue = new JButton("Blue");
            clear = new JButton("Clear");

            red.setPreferredSize(new Dimension(50, 16));
            green.setPreferredSize(new Dimension(50,16));
            blue.setPreferredSize(new Dimension(50, 16));

            panel.add(red);
            panel.add(green);
            panel.add(blue);
            panel.add(clear);

            red.addActionListener(this);
            green.addActionListener(this);
            blue.addActionListener(this);
            clear.addActionListener(this);

            frame.setSize(500, 500);


            addMouseListener(new MouseAdapter(){
                    public void mousePressed(MouseEvent e){
                            prevX = e.getX();
                            prevY = e.getY();
                    }

            });

            addMouseMotionListener(new MouseMotionAdapter(){
                    public void mouseDragged(MouseEvent e){
                            x = e.getX();
                            y = e.getY();
                            draw.drawLine(prevX, prevY, x, y);
                            repaint();
                            prevX = x;
                            prevY = y;

                    }


            });

    }


    public void paintComponent(Graphics g){

           if(image==null){
                    image = createImage(getSize().width, getSize().height);

                    draw = (Graphics2D)image.getGraphics();

                    draw.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                    draw.setPaint(Color.white);
                    draw.fillRect(0, 0, getSize().width, getSize().height);
                    draw.setPaint(Color.black);
                    repaint();
           }

            g.drawImage(image, 0, 0, null);

    }


    public void actionPerformed(ActionEvent e) {
        if( e.getSource()==red){
            draw.setPaint(Color.red);
            repaint();
        }
        if( e.getSource()==green){
            draw.setPaint(Color.green);
            repaint();
        }
        if( e.getSource()==blue){
            draw.setPaint(Color.blue);
            repaint();
        }
        if( e.getSource()==clear){
            draw.setPaint(Color.white);
            draw.fillRect(0, 0, getSize().width, getSize().height);
            draw.setPaint(Color.black);
            repaint();
        }


    }


 }

推荐答案

您必须将可见性属性与框架和面板一起使用,例如

your have to use the visibility properties with frame and panel as well like

frame.setVisible(true);

g.setVisible(true);行对您不起作用,因为您扩展了类jcomponent,并且正在使用frame且未设置其属性以将其设置为可见.

line g.setVisible(true); is not working for you as you have extended your class jcomponent and your are using frame and not setting its property to set it visible.

您的面板会出现相同的问题,因此您必须同时设置其属性,即

Same problem will occure with your panel so your have to set its property as well i-e

panel.setVisible(true);

这是添加这些属性后可以使用的完整代码

Here is your full code that is working after adding these properties

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

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

public class Paint{

    public static void main(String[] args){
            gui g = new gui();
            g.setVisible(true);

    }

}

 class gui extends JComponent implements ActionListener{
    JButton red, green, blue, clear;
    Image image;
    Graphics2D draw;
    int x, y, prevX, prevY;

    gui(){
            JFrame frame = new JFrame("Paint");
            Container content = frame.getContentPane();
            content.setLayout(new BorderLayout());
            setDoubleBuffered(false);

            JPanel panel = new JPanel();
            content.add(panel, BorderLayout.SOUTH);
            panel.setPreferredSize(new Dimension(32, 68));
            panel.setMinimumSize(new Dimension(32, 68));
            panel.setMaximumSize(new Dimension(32, 68));


            red = new JButton("Red");
            green = new JButton("Green");
            blue = new JButton("Blue");
            clear = new JButton("Clear");

            red.setPreferredSize(new Dimension(50, 16));
            green.setPreferredSize(new Dimension(50,16));
            blue.setPreferredSize(new Dimension(50, 16));

            panel.add(red);
            panel.add(green);
            panel.add(blue);
            panel.add(clear);

            panel.setVisible(true);

            red.addActionListener(this);
            green.addActionListener(this);
            blue.addActionListener(this);
            clear.addActionListener(this);

            frame.setSize(500, 500);

            frame.setVisible(true)            ;


            addMouseListener(new MouseAdapter(){
                    public void mousePressed(MouseEvent e){
                            prevX = e.getX();
                            prevY = e.getY();
                    }

            });

            addMouseMotionListener(new MouseMotionAdapter(){
                    public void mouseDragged(MouseEvent e){
                            x = e.getX();
                            y = e.getY();
                            draw.drawLine(prevX, prevY, x, y);
                            repaint();
                            prevX = x;
                            prevY = y;

                    }


            });

    }


    public void paintComponent(Graphics g){

           if(image==null){
                    image = createImage(getSize().width, getSize().height);

                    draw = (Graphics2D)image.getGraphics();

                    draw.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                    draw.setPaint(Color.white);
                    draw.fillRect(0, 0, getSize().width, getSize().height);
                    draw.setPaint(Color.black);
                    repaint();
           }

            g.drawImage(image, 0, 0, null);

    }


    public void actionPerformed(ActionEvent e) {
        if( e.getSource()==red){
            draw.setPaint(Color.red);
            repaint();
        }
        if( e.getSource()==green){
            draw.setPaint(Color.green);
            repaint();
        }
        if( e.getSource()==blue){
            draw.setPaint(Color.blue);
            repaint();
        }
        if( e.getSource()==clear){
            draw.setPaint(Color.white);
            draw.fillRect(0, 0, getSize().width, getSize().height);
            draw.setPaint(Color.black);
            repaint();
        }


    }


 }

这篇关于GUI Java程序-绘画程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆