图标式按纽
-
把图标放在JBuuton按纽上
-
可以设置鼠标悬浮时提示
-
单选框要放在一个组中
-
如果先设置图片,程序会出问题
-
第一次运行不显示图标,要操作一次窗口才会显示
-
原因:图片太大了窗口显示不完全
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ButtonIconDemo01 extends JFrame{
public static void main(String[] args) {
new ButtonIconDemo01().init();
//new ButtonIconDemo01().testRadio();
//new ButtonIconDemo01().testCheck();
}
public void init(){
setBounds(100,100,300,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
URL resource = ButtonIconDemo01.class.getResource("天明 (3).jpg");
ImageIcon imageIcon = new ImageIcon(resource);
JButton button = new JButton();
/**
* 如果先设置图片,程序会出问题
* 第一次运行不显示图标,要操作一次窗口才会显示
*
* 原因:图片太大了窗口显示不完全
*/
add(button);
button.setIcon(imageIcon);
button.setToolTipText("这是一个图片按纽");
}
//单选框
public void testRadio(){
setBounds(100,100,600,600);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JRadioButton jRadioButton = new JRadioButton("a");
JRadioButton jRadioButton1 = new JRadioButton("b");
JRadioButton jRadioButton2 = new JRadioButton("c");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton);
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
add(jRadioButton);
add(jRadioButton1);
add(jRadioButton2);
}
//复选框
public void testCheck(){
setBounds(100,100,600,600);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JCheckBox jCheckBox = new JCheckBox("苹果");
JCheckBox jCheckBox1 = new JCheckBox("香蕉");
JCheckBox jCheckBox2 = new JCheckBox("梨子");
add(jCheckBox);
add(jCheckBox1);
add(jCheckBox2);
}
}
先后为:ImageButton,redioButton和checkBox