JAVA:GirdLayout 无法解析为类型,TitleBorder 无法解析为类型
千夜怪盗 2014-10-25 03:12:40 源程序如下
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.BorderLayout;
public class TestJCheckBox extends JFrame{
private Icon[] icons = {new ImageIcon("images/bird.gif"),
new ImageIcon("images/cat.gif"), new ImageIcon("images/dog.gif"),
new ImageIcon("images/pig.gif"), new ImageIcon("images/rabbit.gif")
};
private JRadioButton[] radioButton = {new JRadioButton("鸟",true),//一开始设定单选按钮选中"鸟"
new JRadioButton("猫",false), new JRadioButton("狗",false),
new JRadioButton("猪",false), new JRadioButton("兔",false)
};
private ButtonGroup buttonGroup = new ButtonGroup();
private JLabel imgLabel = new JLabel(icons[0]);
public TestJCheckBox() {
JPanel westPanel = new JPanel();
westPanel.setLayout(new GirdLayout(5,1)); //这里错了
westPanel.setBorder(new TitleBorder("选择"));//创建并设置标题边框 //这里错了
ItemListener listener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int n = 0;
JRadioButton sourceRadioButton = (JRadioButton)e.getSource();
String text = sourceRadioButton.getText();
for( int index = 0; index < radioButton.length; index ++ ) {
if( text.equals(radioButton[index].getText()) ) {
n = index;
break;
}
}
imgLabel.setIcon(icons[n]);
}
};
for( int index = 0; index < radioButton.length; index ++ ) {
radioButton[index].addItemListener(listener);
buttonGroup.add(radioButton[index]);
westPanel.add(radioButton[index]);
}
setLayout(new BorderLayout());
add(imgLabel,BorderLayout.CENTER);
add(westPanel,BorderLayout.WEST);
}
public static void main(String[] args) {
TestJCheckBox frame = new TestJCheckBox();
frame.setTitle("测试JRadioButton");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}