有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

}


共 (4) 个答案

  1. # 1 楼答案

    您可以提供按钮,该按钮将作为JRadioButton创建者的参数进行更改,但它看起来非常不可读,设计也很糟糕

    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事件发生后,什么按钮应该更改为什么状态

  2. # 2 楼答案

    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();
    
  3. # 3 楼答案

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

  4. # 4 楼答案

    就这么简单

    yourJButton.setIcon(yourIconToSet);
    

    这应该是从单选按钮上的action listener调用的ofc

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