java radiobutton重置_java - 根据JRadioButton选择更改JButton的图标 - 堆栈内存溢出

这篇博客展示了如何在Java Swing GUI中创建一个包含两个JRadioButton的面板。当用户点击其中一个单选按钮时,另一个面板中的JButton图像会相应地更新。主要涉及ActionListener事件监听和组件交互。
摘要由CSDN通过智能技术生成

我有一个带有ActionListener的JRadioButton,但无法弄清楚单击它时如何在另一个面板中触发JButton的图标更改。 两者的代码在下面列出。 选择正确的单选按钮后,图像需要从左按钮切换到右按钮。

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值