各位,我在写程序的时候提示photolabel.setIcon(labelicon);这句出错,提示“标记上具有语法错误,错误放置了构造”,可能是没这个方法吧,但我又不不知道为什么,请教,我是新手!谢谢!源代码如下:
package com.frame;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class log extends JFrame implements ActionListener{
JLabel username = new JLabel();
JTextField useredit = new JTextField();
JLabel passlaebl = new JLabel();
JTextField passedit = new JTextField();
JButton logbutton = new JButton("登录");
JLabel photolabel = new JLabel();
Icon labelicon = new ImageIcon("images/log.bmp");
photolabel.setIcon(labelicon); //这里错了
private Container contrainer =getContentPane(); //初始化一个容器,用来在容器上添加一些控件
public log(){
setTitle("登录窗口");
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
log a = new log();
a.show();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == "logbutton" ){
System.out.print("登录吧,少年");
}
}
}
Java setIcon()问题
答案:2 悬赏:70 手机版
解决时间 2021-02-01 13:21
- 提问者网友:留有余香
- 2021-01-31 15:16
最佳答案
- 五星知识达人网友:独行浪子会拥风
- 2021-01-31 16:06
hello 少年 又是你
public ImageIcon createImageIcon(String filename) {
String path = "/images/" + filename;
return ImageIO.read(this.getClass().getClassLoader().getResource(file));
}
JButton button = new JButton(createImageIcon("log.bmp");
public ImageIcon createImageIcon(String filename) {
String path = "/images/" + filename;
return ImageIO.read(this.getClass().getClassLoader().getResource(file));
}
JButton button = new JButton(createImageIcon("log.bmp");
全部回答
- 1楼网友:洒脱疯子
- 2021-01-31 17:24
photolabel.setIcon(labelicon); //这个是语句块啊,语句块要写到方法或者构造里面的 而且你的组件都没有放到容器上面,运行成功是一个空白的登陆窗口!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯