永发信息网

JAVA按钮事件

答案:1  悬赏:60  手机版
解决时间 2021-12-26 01:46
  • 提问者网友:我们很暧昧
  • 2021-12-25 17:17
已经设置好了一个jlabel和icon,如何实现点击按钮将icon放入jlabel中,需要代码谢谢
最佳答案
  • 五星知识达人网友:千夜
  • 2021-12-25 18:34
package zhidao;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class MyButton extends JFrame
{
private static final long serialVersionUID = 1L;

public MyButton ( String title )
{
setTitle (title);
setLayout (new BorderLayout ());
setResizable (false);
setSize (300, 200);
setLocationRelativeTo (null);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}

private static void addComponent (JFrame frame)
{
final Icon icon = new ImageIcon ("dlt.png");
final JLabel label = new JLabel ();
JButton button = new JButton ("test");
button.addActionListener (new ActionListener()
{
@Override
public void actionPerformed ( ActionEvent e )
{
label.setIcon (icon);
}
});
frame.add (label, BorderLayout.CENTER);
frame.add (button, BorderLayout.SOUTH);
}

public static void main ( String[] args )
{
SwingUtilities.invokeLater (new Runnable()
{
@Override
public void run ()
{
MyButton mb = new MyButton ("JAVA按钮事件");
addComponent(mb);
mb.setVisible (true);
}
});
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯