Java如何在JButton中显示图像?
package org.nhooo.example.swing;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.FlowLayout;
public class ButtonImageExample extends JFrame {
public ButtonImageExample() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ButtonImageExample().setVisible(true));
}
private void initComponents() {
setTitle("My Buttons");
setSize(200, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
//创建两个JButton对象,其中包含要显示的图像。该图像可以是
//支持gif,jpeg,png和其他一些类型。而且我们还设置了
//快捷键按钮的助记符。
JButton okButton = new JButton("OK", new ImageIcon("ok.png"));
okButton.setMnemonic('O');
JButton cancelButton = new JButton("Cancel", new ImageIcon("cancel.png"));
cancelButton.setMnemonic('C');
getContentPane().add(okButton);
getContentPane().add(cancelButton);
}
}热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短