使用JRadioButton显示图像

时间:2015-05-13 05:59:03

标签: java swing actionlistener

我需要一些小问题的帮助。单击JRadioButton后,我不确定如何正确显示图像。单击任何按钮并忘记从哪里开始后,没有显示任何内容。任何帮助表示赞赏。

public class DisplayImage extends JFrame{
private JRadioButton pic1Button, pic2Button;
private ImageIcon pic1Image,pic2Image;
private JLabel pic1Label,pic2Label;


public DisplayImage(){
    super("JRadioButton Image");
    setLayout(new FlowLayout());

    pic1Button = new JRadioButton("pic1");
    pic1Button.addActionListener(new displayListener());

    pic2Button = new JRadioButton("pic2");
    pic2Button.addActionListener(new displayListener());
    ButtonGroup bg = new ButtonGroup();
    bg.add(pic1Button);
    bg.add(pic2Button);
    JPanel panel = new JPanel();
    panel.add(pic1Button);
    panel.add(pic2Button);
    add(panel);
}
private class displayListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==pic1Button){
            pic1Image = new ImageIcon(getClass().getResource("car.jpg"));
            pic1Label = new JLabel(pic1Image);
            add(pic1Label);
        }
        if(e.getSource()==pic2Button){
            pic2Image = new ImageIcon(getClass().getResource("plane.jpg"));
            pic2Label = new JLabel(pic2Image);
            add(pic2Label);
        }

    }

}
public static void main(String[]args){
    DisplayImage gui = new DisplayImage();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setSize(300,300);
    gui.setVisible(true);
}

1 个答案:

答案 0 :(得分:4)

添加图片标签后,您可能需要致电revalidaterepaint,但更好的解决方案是在构造函数中添加一个JLabel(以及您的广播)按钮),只需使用它的setIcon方法来更改图像

  import java.awt.FlowLayout;
  import java.awt.event.ActionListener;
  import javafx.event.ActionEvent;
  import javax.swing.ButtonGroup;
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JRadioButton;
  import static sun.misc.ClassFileTransformer.add;

  public class DisplayImage extends JFrame {

    private JRadioButton pic1Button, pic2Button;
    private JLabel pic1Label;

    public DisplayImage() {
      super("JRadioButton Image");
      setLayout(new FlowLayout());

      pic1Button = new JRadioButton("pic1");
      pic1Button.addActionListener(new DisplayListener());

      pic2Button = new JRadioButton("pic2");
      pic2Button.addActionListener(new DisplayListener());
      ButtonGroup bg = new ButtonGroup();
      bg.add(pic1Button);
      bg.add(pic2Button);
      JPanel panel = new JPanel();
      panel.add(pic1Button);
      panel.add(pic2Button);
      add(panel);
      pic1Label = new JLabel();
      add(pic1Label);
    }

    private class DisplayListener implements ActionListener {

      public void actionPerformed(ActionEvent e) {
        ImageIcon icon = null;
        if (e.getSource() == pic1Button) {
          icon = new ImageIcon(getClass().getResource("car.jpg"));
        }
        if (e.getSource() == pic2Button) {
          icon = new ImageIcon(getClass().getResource("plane.jpg"));
        }
        pic1Label.setIcon(icon);

      }

    }

    public static void main(String[] args) {
      DisplayImage gui = new DisplayImage();
      gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      gui.setSize(300, 300);
      gui.setVisible(true);
    }
  }