使用单选按钮移动图像

时间:2017-03-14 16:02:03

标签: java swing

我用两个jbuttons创建了一个gui,并在其中一个按钮上添加了一个图标。现在我想通过添加两个单选按钮来添加一些功能,并使图标从一个jbutton移动到另一个(从左到右,从右到左)。我知道我需要在单选按钮上有一个动作监听器,我想这是一个if else语句。但我无法弄清楚如何将我的if语句指向当前持有该图标的jbutton。

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

        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new EmptyBorder(0, 0, 12, 0));
        contentPane.add(panel_1, BorderLayout.NORTH);

        JLabel lblSomeGui = new JLabel("SOME GUI");
        panel_1.add(lblSomeGui);

        JPanel panel = new JPanel();
        panel.setBorder(new EmptyBorder(12, 12, 12, 12));
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(new GridLayout(1, 0, 12, 0));

        JButton btnRight = new JButton("");
        btnRight.setIcon(new ImageIcon(Testing.class.getResource("/btn/resources/balloon80.jpg")));
        btnRight.setBackground(Color.BLACK);
        panel.add(btnRight);

        JButton btnLeft = new JButton("");
        btnLeft.setBackground(Color.BLACK);


        panel.add(btnLeft);

        JPanel panel_2 = new JPanel();
        panel_2.setBorder(new EmptyBorder(12, 0, 0, 0));
        contentPane.add(panel_2, BorderLayout.SOUTH);

        JRadioButton radioButtonLeft = new JRadioButton("Left");
        radioButtonLeft.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(btnRight.setIcon() == ("/btn/resources/balloon80.jpg") ){

                }

            }
        });
        buttonGroup.add(radioButtonLeft);
        panel_2.add(radioButtonLeft);

        JRadioButton radioButtonRight = new JRadioButton("Right");
        radioButtonRight.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        buttonGroup.add(radioButtonRight);
        panel_2.add(radioButtonRight);
    }

}

3 个答案:

答案 0 :(得分:0)

我可能只是将Icon定义为您班级中的实例变量:

private Icon balloon = new ImageIcon(Testing.class.getResource("/btn/resources/balloon80.jpg"));

然后,当您创建按钮时,您可以使用:

JButton btnRight = new JButton("");
btnRight.setIcon( balloon );

甚至更容易:

JButton btnRight = new JButton( balloon );

最后,在“左”单选按钮的ActionListener中,您可以使用:

btnLeft.setIcon( balloon );
btnRight.setIcon( null );

答案 1 :(得分:0)

您可以将以下代码添加到动作侦听器:

if(btnRight.getIcon() == null){
 // set icon to btnLeft
} else {
 // set icon to btnRight
}

希望这有帮助。

答案 2 :(得分:0)

无需if else声明。

final ImageIcon icon = new ImageIcon(Testing.class.getResource("/btn/resources/balloon80.jpg"));
radioButtonRight.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
      btnRight.setIcon(icon);
      btnLeft.setIcon(null);
   }
});

radioButtonLeft.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        btnRight.setIcon(null);
        btnLeft.setIcon(icon);
    }
});