【问题标题】:use radio button to move image使用单选按钮移动图像
【发布时间】:2017-08-05 01:50:40
【问题描述】:

我创建了一个带有两个 jbutton 的 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);
    }

}

【问题讨论】:

    标签: java swing


    【解决方案1】:

    我可能只是将 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 );
    

    【讨论】:

    • 所以我不需要从设置 jbutton 上的图标开始
    • 没关系,忽略之前的声明,这对我来说有点愚蠢。
    • @user3803668,我原来的评论说在创建按钮时设置图标。查看更新的代码。
    【解决方案2】:

    您可以将以下代码添加到您的操作侦听器中:

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

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      不需要 ‍‍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);
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2020-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-03
        • 1970-01-01
        相关资源
        最近更新 更多