gpt4 book ai didi

java - 使用小程序单击按钮时打印图像?

转载 作者:行者123 更新时间:2023-12-01 11:36:28 24 4
gpt4 key购买 nike

所以我需要在单击按钮时显示图像。当我点击按钮时,它什么也不显示。是否可以将参数传递给 void Paint 方法,以便我可以使用 if 条件检查条件?如果单击方形按钮,则应打印方形,如果单击圆形按钮,则应打印圆形

import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Jungle extends Applet implements ActionListener{
Button n;
Button o;
public void init()

{
n=new Button("Square");
add(n);
n.addActionListener(this);
o = new Button("circle");
add(o);
o.addActionListener(this);



}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==n)
{
repaint();
};


}
class Delta
{
void graphics(Graphics g)
{
g.drawRect(40, 40, 20, 20);
g.fillRect(40, 40, 20, 20);
}
}

}

最佳答案

..so that I can check the condition using if condition..

将图像声明为类属性并将其值设置为 null:

Image img = null;

单击按钮时,将图像设置为与要显示的图像相同。

paint(Graphics) 方法中,在绘制图像之前检查是否为 null。例如:

if (img!=null) {
g.drawImage(img, 0, 0, this);
}

关于java - 使用小程序单击按钮时打印图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29932272/

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