gpt4 book ai didi

java - 单击 JButton 时显示图像

转载 作者:行者123 更新时间:2023-11-29 10:20:25 24 4
gpt4 key购买 nike

我试图在单击 JButton 时显示图像,但在执行时单击按钮时不显示图像。

    import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import java.awt.*;

public class new2 extends JFrame implements ActionListener
{
private boolean b1,b2;

Container contentPane= getContentPane();
JButton awar=new JButton("@war");
JButton arrow=new JButton("arrow");
private Image image1,image2;

public new2()
{
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane.setLayout(new FlowLayout());

awar.addActionListener(this);
contentPane.add(awar).setVisible(true);

arrow.addActionListener(this);
contentPane.add(arrow).setVisible(true);

}

public void init()
{


image1=Toolkit.getDefaultToolkit().getImage("@war.jpeg");
image2=Toolkit.getDefaultToolkit().getImage("arrow.gif");
}


public void paint(Graphics g)
{

if(b1==true)
{
g.drawImage(image1,0,0,this);
}
else if(b2==true)
{
g.drawImage(image2,0,0,this);
}

}

public void actionPerformed(ActionEvent event)
{

String actionCommand = event.getActionCommand();

if(actionCommand.equals("@war"))
{
b1=true;
}
else if(actionCommand.equals("arrow"))
{
b2=true;
}
}

public static void main(String args[])
{
new2 m=new new2();
m.setVisible(true);
}

最佳答案

..display an image upon clicking a JButton but upon execution the image is not displayed when button is clicked.

使用 JToggleButton 如图所示 here .

关于java - 单击 JButton 时显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7214307/

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