【问题标题】:Changing the icon of a JButton based on a JRadioButton selection根据 JRadioButton 选择更改 JButton 的图标
【发布时间】:2015-05-21 09:35:39
【问题描述】:

我有一个带有 ActionListener 的 JRadioButton,但无法弄清楚如何在单击不同面板中触发 JButton 的图标更改。下面列出了两者的代码。 The image needs to switch from the left button to the right when the correct radio button is selected.

package gui;

public class ExampleGUI extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel contentPane;
ImageIcon icon = new ImageIcon(ExampleGUI.class
        .getResource("/gui/schlange1.gif"));

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ExampleGUI frame = new ExampleGUI();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public ExampleGUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 5));
    setContentPane(contentPane);

    JLabel lblExampleGui = new JLabel("Example GUI");
    lblExampleGui.setFont(new Font("Tahoma", Font.PLAIN, 18));
    lblExampleGui.setHorizontalAlignment(SwingConstants.CENTER);
    contentPane.add(lblExampleGui, BorderLayout.NORTH);

    JPanel radioButtonPanel = new JPanel();
    contentPane.add(radioButtonPanel, BorderLayout.SOUTH);

    JPanel imagePanelBoxes = mainImagePanel();
    contentPane.add(imagePanelBoxes, BorderLayout.CENTER);

    JButton leftImage = leftClickImage();
    imagePanelBoxes.add(leftImage);

    JButton rightImage = rightClickImage();
    imagePanelBoxes.add(rightImage);

    JRadioButton leftRadioButton = leftRadioButton();
    radioButtonPanel.add(leftRadioButton);

    JRadioButton rightRadioButton = rightRadioButton();
    radioButtonPanel.add(rightRadioButton);

    ButtonGroup group = new ButtonGroup();
    group.add(leftRadioButton);
    group.add(rightRadioButton);
}

private JPanel mainImagePanel() {
    JPanel imagesPanel = new JPanel();
    imagesPanel.setBorder(new EmptyBorder(0, 5, 0, 5));
    imagesPanel.setLayout(new GridLayout(0, 2, 10, 0));
    return imagesPanel;
}

private JRadioButton leftRadioButton() {
    final JRadioButton leftRadioButton = new JRadioButton("LEFT");
    leftRadioButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            changeIcon(leftClickImage(), icon);
        }
    });
    leftRadioButton.setSelected(true);
    return leftRadioButton;
}

private JRadioButton rightRadioButton() {
    final JRadioButton rightRadioButton = new JRadioButton("RIGHT");
    rightRadioButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            changeIcon(rightClickImage(), icon);
        }
    });
    rightRadioButton.isSelected();
    return rightRadioButton;
}

private JButton leftClickImage() {
    JButton leftImage = new JButton("");
    leftImage.setIcon(new ImageIcon(ExampleGUI.class
            .getResource("/gui/schlange1.gif")));
    leftImage.setBackground(Color.BLACK);
    return leftImage;
}

private JButton rightClickImage() {
    final JButton rightImage = new JButton("");
    rightImage.setBackground(Color.BLACK);
    return rightImage;
}

public void changeIcon(JButton jb, ImageIcon icon) {
    jb.setIcon(icon);
}

}

【问题讨论】:

  • 你既没有按钮也没有单选按钮。您有两种方法正在返回它们。您是否需要这些方法,或者您想实现您上面描述的内容?如果最后,请参见下文,如果不是,请说明您的需求
  • 我添加了完整的代码。图片默认显示在左侧按钮上,点击右侧单选按钮时需要切换到右侧按钮。
  • 它肯定不是完整的代码,你的类定义丢失了 XD
  • 现在显示完整的代码减去导入。

标签: java swing jbutton windowbuilder jradiobutton


【解决方案1】:

只是简单

yourJButton.setIcon(yourIconToSet);

这应该从单选按钮上的动作监听器中调用

leftRadioButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        // ? <- here invoke code to change your button's label
    }
});

【讨论】:

  • 我试过了,但是没有用。如果您想查看,我添加了完整的代码。
【解决方案2】:
public class SwitchButton {
public static void main(String [] args){
    SwitchButton sb = new SwitchButton();
}

JFrame jfButtons =  new JFrame();
JPanel jpButtons =  new JPanel();
JRadioButton jrb = new JRadioButton("if you click me");
JButton jb = new JButton("I'll change");

public SwitchButton(){
    jrb.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            changeColor(jb, Color.blue);
        }
    });
    jpButtons.add(jrb);
    jpButtons.add(jb);
    jfButtons.add(jpButtons);
    jfButtons.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    jfButtons.setVisible(true);
    jfButtons.pack();
}

public void changeColor(JButton jbtn, Color color){
    jbtn.setBackground(color);
}

}

这基本上完成了您想要做的事情。您只需将 changeColor() 更改为 changeIcon()

【讨论】:

  • Java 还是新手,所以我不确定如何将您的代码实现到我的代码中。如果您想查看它,我已经添加了我的完整代码。
【解决方案3】:

您可以提供要更改的 Button 作为 JRadioButton-creator 的参数,但这看起来很不可读且设计不当

private JRadioButton leftRadioButton(JButton affectedButton) {
    JRadioButton leftRadioButton = new JRadioButton("LEFT");
    leftRadioButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (leftRadioButton.isSelected())
                affectedButton=rightRadioButton();
            else 
                affectedButton=leftRadioButton();
        }
    });
    leftRadioButton.setSelected(true);
    return leftRadioButton;
}

我宁愿不使用您的 actionListener 的内联定义,而是在您的框架(或您使用的其他类)中实现它以访问该类中使用的按钮和标签等。如果您没有太多要听的项目,它会变得更具可读性。

public class buttonchanger extends JFrame implements ActionListener{
    JPanel radioButtonPanel;
    JPanel imagePanelBoxes;
    JButton leftImage;
    JButton rightImage;
    JRadioButton leftRadioButton;
    JRadioButton rightRadioButton;

    public initGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 5));
        setContentPane(contentPane);

        JLabel lblExampleGui = new JLabel("Example GUI");
        lblExampleGui.setFont(new Font("Tahoma", Font.PLAIN, 18));
        lblExampleGui.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(lblExampleGui, BorderLayout.NORTH);

        JPanel radioButtonPanel = new JPanel();
        contentPane.add(radioButtonPanel, BorderLayout.SOUTH);

        JPanel imagePanelBoxes = mainImagePanel();
        contentPane.add(imagePanelBoxes, BorderLayout.CENTER);

        JButton leftImage = leftClickImage();
        imagePanelBoxes.add(leftImage);

        JButton rightImage = rightClickImage();
        imagePanelBoxes.add(rightImage);

        JRadioButton leftRadioButton = leftRadioButton();
        leftRadioButton.addActionListener(this);
        radioButtonPanel.add(leftRadioButton);

        JRadioButton rightRadioButton = rightRadioButton();
        rightRadioButton.addActionListener(this);
        radioButtonPanel.add(rightRadioButton);

        ButtonGroup group = new ButtonGroup();
        group.add(leftRadioButton);
        group.add(rightRadioButton);
    }

    public void ActionListener(ActionEvent actE){
        Object obj=actE.getSource();
        if (obj==leftRadioButton){
            leftImage.setIcon(yourIcon);
            //or do whatever you intend to do
        }
    }

}

希望这是您正在寻找的更多解决方案。我还是不知道radioButton-Event之后哪个按钮应该变成什么状态

【讨论】:

  • 感谢您的回答,不幸的是我仍然有问题。我将与导师核实,看看他们是否可以提供帮助。
【解决方案4】:
ImageIcon icon = new ImageIcon("img src"); 

rightRadiobutton.addActionListener(new ActionListener(ActionEvent ae){
   changeIcon(rightButton, icon); 
});

public void changeIcon(JButon jb, ImageIcon icon){
    jb.setIcon(icon);
}

您对 leftRadioButton 执行相同的操作。您也不需要单独的方法来创建组件;您可以像这样分配和使用它们:

JRadioButton rightJrb = new JRadioButton("I am a radio button");
rightJrb.addActionListener();

【讨论】:

  • 我用最新的代码减去导入来编辑问题代码。我只使用一个类。尝试建议后,选择右单选按钮时仍未显示右侧按钮中的图像。我还尝试使用 if 语句来检查单选按钮是否已选中,但仍然没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2015-08-01
  • 1970-01-01
相关资源
最近更新 更多