gpt4 book ai didi

java - 根据 JRadioButton 选择更改 JButton 的图标

转载 作者:行者123 更新时间:2023-12-01 11:45:56 24 4
gpt4 key购买 nike

我有一个带有 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);
}

}

最佳答案

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 - 根据 JRadioButton 选择更改 JButton 的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29124810/

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