如何将图标添加到JButton

时间:2017-09-02 10:42:53

标签: java swing

我在java中创建了一个按钮但是当我想在按钮上添加一个图标时,它会显示一个空白按钮。

public class T36 {

    public static void main(String[] args) {

        JFrame f = new JFrame("test 36");

        JTextField tf = new JTextField();
        tf.setBounds(40, 40, 80, 30);

        JButton b = new JButton(new ImageIcon("C:\\Java\\Test\\src\\test\\calculator1600.gif"));
        b.setBounds(40, 100, 200, 30);

        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                tf.setText("hello");
            }
        });
        f.add(tf);
        f.add(b);
        f.setLayout(null);
        f.setSize(400, 300);
        f.setVisible(true);
    }

}

0 个答案:

没有答案