【发布时间】: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);
}
}
【问题讨论】: