gpt4 book ai didi

java - 使用JRadioButton显示图片

转载 作者:行者123 更新时间:2023-11-30 07:00:50 25 4
gpt4 key购买 nike

我需要帮助解决一个小问题。单击 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);
}

最佳答案

您可能需要在添加图片标签后调用 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);
}
}

关于java - 使用JRadioButton显示图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30206789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com