首页上一页 1 下一页尾页 2 条记录 1/1页
java入门经典 第14章上机实践2
发表在Java图书答疑
2014-11-29
是否精华
是
否
版块置顶:
是
否
我把对应单选按钮的图片放在Label里面了,选择单选按钮时,面板加载对应图片的label,移除其他按钮的图片label,但是这样的程序只能运行一遍,如果我选择下面程序里面的apple,再选banana,图片可以正确显示,但是如果在选apple,就无法显示apple的图片了,但是,再继续选择没有选过的strawberry时,就可以正确显示草莓的图片。。。每个按钮只能显示一次。。。这是为什么?
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class test1 extends JFrame{
public test1(){
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,500,600);
setLayout(null);
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0,0,400,500);
getContentPane().add(panel);
final JRadioButton rb1 = new JRadioButton("banana");
rb1.setBounds(50,100,100,20);
panel.add(rb1);
final JRadioButton rb2 = new JRadioButton("strawberry");
rb2.setBounds(50,150,100,20);
panel.add(rb2);
final JRadioButton rb3 = new JRadioButton("apple");
rb3.setBounds(50,200,100,20);
panel.add(rb3);
ImageIcon banana = new ImageIcon("F:/s/banana.jpg");
final JLabel banana_label = new JLabel(banana);
ImageIcon strawberry = new ImageIcon("F:/s/strawberry.jpg");
final JLabel strawberry_label = new JLabel(strawberry);
ImageIcon apple = new ImageIcon("F:/s/apple.jpg");
final JLabel apple_label = new JLabel(apple);
rb1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(rb1.isSelected()){
panel.add(banana_label);
banana_label.setBounds(250,50,200,200);
panel.remove(strawberry_label);
panel.remove(apple_label);
}
}
});
rb2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(rb2.isSelected()){
panel.add(strawberry_label);
strawberry_label.setBounds(250,50,200,200);
panel.remove(banana_label);
panel.remove(apple_label);
}
}
});
rb3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(rb3.isSelected()){
panel.add(apple_label);
apple_label.setBounds(250,50,200,200);
panel.remove(banana_label);
panel.remove(strawberry_label);
}
}
});
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
setVisible(true);
}
public static void main(String[] args){
new test1();
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class test1 extends JFrame{
public test1(){
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,500,600);
setLayout(null);
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0,0,400,500);
getContentPane().add(panel);
final JRadioButton rb1 = new JRadioButton("banana");
rb1.setBounds(50,100,100,20);
panel.add(rb1);
final JRadioButton rb2 = new JRadioButton("strawberry");
rb2.setBounds(50,150,100,20);
panel.add(rb2);
final JRadioButton rb3 = new JRadioButton("apple");
rb3.setBounds(50,200,100,20);
panel.add(rb3);
ImageIcon banana = new ImageIcon("F:/s/banana.jpg");
final JLabel banana_label = new JLabel(banana);
ImageIcon strawberry = new ImageIcon("F:/s/strawberry.jpg");
final JLabel strawberry_label = new JLabel(strawberry);
ImageIcon apple = new ImageIcon("F:/s/apple.jpg");
final JLabel apple_label = new JLabel(apple);
rb1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(rb1.isSelected()){
panel.add(banana_label);
banana_label.setBounds(250,50,200,200);
panel.remove(strawberry_label);
panel.remove(apple_label);
}
}
});
rb2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(rb2.isSelected()){
panel.add(strawberry_label);
strawberry_label.setBounds(250,50,200,200);
panel.remove(banana_label);
panel.remove(apple_label);
}
}
});
rb3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(rb3.isSelected()){
panel.add(apple_label);
apple_label.setBounds(250,50,200,200);
panel.remove(banana_label);
panel.remove(strawberry_label);
}
}
});
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
setVisible(true);
}
public static void main(String[] args){
new test1();
}
}