关JLabel的问题

beaux 2002-04-22 02:02:50
我想通过给一个按钮加监听器,从而改变JLabel的图标。主要块的代码应该怎么写?
...全文
36 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
beaux 2002-04-22
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CCDViewer extends JFrame
{
private JLabel label = new JLabel(new ImageIcon("beauxSwing3.jpg"));
private JButton button = new JButton("Action");
private JScrollPane sp = new JScrollPane(label);
private String message = "Input the file's name: ";

public CCDViewer()
{
Container contentPane = getContentPane();
button.setMnemonic('T');
contentPane.add(sp, BorderLayout.CENTER);
contentPane.add(button, BorderLayout.SOUTH);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String s = JOptionPane.showInputDialog(message);
if (s != null)
{
label = new JLabel(new ImageIcon(s));
/*这个地方应该怎么写*/

}
}
});
}
public static void main(String args[])
{
GJApp.launch(new CCDViewer(), "Beaux 02-04-19", 100, 100, 600, 500);
}
}

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧