簡體   English   中英

根據JRadioButton選擇更改JButton的圖標

[英]Changing the icon of a JButton based on a JRadioButton selection

我有一個帶有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()

只是簡單

yourJButton.setIcon(yourIconToSet);

這應該是通過單選按鈕上的動作監聽器調用的

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

您可以提供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的內聯定義,而是在您的框架(或您使用的其他類)中實現它,以訪問該類中使用的Buttons和Labels等。 如果您沒有太多要聽的項目,它將變得更具可讀性。

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之后哪個按鈕應該更改為什么狀態

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM