我一直在尝试这几个小时。我想将每个JButton
组件更改为图像按钮(它们是单选按钮,这就是为什么变量名称没有意义)。内部的for循环只显示按钮下的所有图像。每个图像都被称为Option1,Option2等。所以我一直试图摆脱for循环,只有10个按钮,上面有你可以点击的图像。
如果有人能帮我解决这个问题会很棒。我所能做的就是使用图像创建按钮,但按钮出现在完全不同的窗口上。
public class JDialog2 extends JDialog {
private final JPanel contentPanel = new JPanel();
/**
* Create the dialog.
*/
public JDialog2(Quiz quiz) {
setBounds(100, 100, 450, 600);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JLabel lblNewLabel_1 = new JLabel("2、Favourite subject at Hogwarts");
lblNewLabel_1.setFont(new Font("Arial", Font.PLAIN, 14));
lblNewLabel_1.setBounds(94, 10, 248, 15);
contentPanel.add(lblNewLabel_1);
ButtonGroup btGroup = new ButtonGroup();
for(int i=1; i<=7; i++) {
ImageIcon image1 = new ImageIcon("Option" + i + ".jpg");
image1.setImage(image1.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
JLabel imageLablel1=new JLabel(image1);
contentPanel.add(imageLablel1);
imageLablel1.setBounds(29, 75 * i - 25, 50, 50);
}
JButton radioButton_1 = new JButton("1.Care of Magical Creatures");
radioButton_1.setBounds(29, 25, 226, 23);
contentPanel.add(radioButton_1);
JButton radioButton_2 = new JButton("2.Charms");
radioButton_2.setBounds(29, 50, 158, 23);
contentPanel.add(radioButton_2);
JButton radioButton_3 = new JButton("3.Defense Against the Dark Arts");
radioButton_3.setBounds(29, 75, 255, 23);
contentPanel.add(radioButton_3);
JButton radioButton_4 = new JButton("4.Divination");
radioButton_4.setBounds(29, 100, 121, 23);
contentPanel.add(radioButton_4);
JButton radioButton_5 = new JButton("5.Herbology");
radioButton_5.setBounds(29, 125, 179, 23);
contentPanel.add(radioButton_5);
JButton radioButton_6 = new JButton("6.History of Magic");
radioButton_6.setBounds(29, 150, 158, 23);
contentPanel.add(radioButton_6);
JButton radioButton_7 = new JButton("7.Muggle Studies");
radioButton_7.setBounds(29, 175, 121, 23);
contentPanel.add(radioButton_7);
JButton radioButton_8 = new JButton("8.Potions");
radioButton_8.setBounds(29, 200, 121, 23);
contentPanel.add(radioButton_8);
JButton radioButton_9 = new JButton("9.Study of Ancient Runes");
radioButton_9.setBounds(29, 220, 255, 23);
contentPanel.add(radioButton_9);
JButton radioButton_10 = new JButton("10.Transfiguration");
radioButton_10.setBounds(29, 245, 179, 23);
contentPanel.add(radioButton_10);
btGroup.add(radioButton_1);
btGroup.add(radioButton_2);
btGroup.add(radioButton_3);
btGroup.add(radioButton_4);
btGroup.add(radioButton_5);
btGroup.add(radioButton_6);
btGroup.add(radioButton_7);
btGroup.add(radioButton_8);
btGroup.add(radioButton_9);
btGroup.add(radioButton_10);
JButton btnCommit = new JButton("Commit");
btnCommit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Question question = quiz.getNextQuestion();
int choice = 0;
Enumeration<AbstractButton> en = btGroup.getElements();
while (en.hasMoreElements()) {
AbstractButton ab = en.nextElement();
choice++;
if (ab.isSelected()) {
break;
}
}
question.setSelectedAnswer(choice - 1);
JDialog3 dialog3 = new JDialog3(quiz);
dialog3.setVisible(true);
exit();
}
});
btnCommit.setBounds(300, 138, 93, 23);
contentPanel.add(btnCommit);
}
public void exit(){
this.setVisible(false);
}
}