gpt4 book ai didi

java - GUI Java 程序 - Paint 程序

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:02:55 28 4
gpt4 key购买 nike

我一直在努力找出我的代码有什么问题。这个想法是创建一个小的画图程序,并有红色、绿色、蓝色和清除按钮。我有我能想到的一切让它工作,但无法弄清楚代码有什么问题。程序打开,然后立即关闭。

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


}


}

最佳答案

你必须像 frame 和 panel 一样使用 visibility 属性

frame.setVisible(true);

g.setVisible(true); 行不适合您,因为您已经扩展了类 jcomponent 并且您正在使用 frame 而不是设置其属性以将其设置为可见。

你的面板也会出现同样的问题,所以你也必须设置它的属性,即

panel.setVisible(true);

这是添加这些属性后可以运行的完整代码

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


}


}

关于java - GUI Java 程序 - Paint 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13356365/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com