将图像添加到GridBagLayout内的按钮

时间:2013-12-28 23:57:51

标签: java swing jbutton gridbaglayout

Ali这就是我现在所做的,但我现在得到错误非静态方法getClass()无法从静态上下文引用

 buttonc = new JButton("6");
    cc.fill = GridBagConstraints.HORIZONTAL;
    cc.ipady = 20;       //reset to default
    cc.weightx = 0.0;   //request any extra vertical space
//    c.anchor = GridBagConstraints.LAST_LINE_START; //bottom of space
//    c.insets = new Insets(10,0,0,0);  //top padding
    cc.gridx = 1;       //aligned with button 2
    cc.gridwidth = 1;   //2 columns wide
    cc.gridy = 2;       //third row
    pane.add(button, cc);

        try{
       Image  img = ImageIO.read(getClass().getResource("gui/UpdateLib.png"));
       Image resize = img.getScaledInstance(290, 180, java.awt.Image.SCALE_SMOOTH);
       buttonc.setIcon(new ImageIcon(resize));

        }catch(Exception e){
        }

1 个答案:

答案 0 :(得分:0)

您应该考虑查看Java文档here

ImageIcon img = createImageIcon("img/photo.gif");

设置完图片后,您可以使用

将其添加到JButton
button = new JButton("6", img);