我正在创建一个想成为百万富翁游戏的人,并创建了一个半按钮,我想使用它来删除两个JButton的答案。以下是作为答案选项的两个JButton的代码。
enter code here: Answer2 = new JButton("B");
Answer2.setBackground(Color.YELLOW);
Answer2.setHorizontalAlignment(SwingConstants.LEFT);
Answer2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Answer2.setBackground(Color.RED);
Answer2.setForeground(Color.WHITE);
}
});
Answer2.setBounds(220, 105, 188, 25);
panel.add(Answer2);
Answer1 = new JButton("A");
Answer1.setBackground(Color.YELLOW);
Answer1.setHorizontalAlignment(SwingConstants.LEFT);
Answer1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Answer1.setBackground(Color.RED);
Answer1.setForeground(Color.WHITE);
}
});
Answer1.setBounds(20, 105, 188, 25);
panel.add(Answer1);
为了实现这个目的,我做了一些并找到了这个方法并尝试了它,但它对我不起作用。这是显示我尝试用半个半按钮
的代码 btnNextQuestion.setBounds(296, 204, 115, 23);
panel.add(btnNextQuestion);
btnHalfAndHalf = new JButton("Half and half");
btnHalfAndHalf.setForeground(new Color(0, 0, 0));
btnHalfAndHalf.setBackground(new Color(255, 255, 51));
btnHalfAndHalf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnHalfAndHalf.remove(Answer1);
btnHalfAndHalf.remove(Answer2);//This is the method I tried
}
});
btnHalfAndHalf.setBounds(22, 204, 115, 23);
panel.add(btnHalfAndHalf);
请让我知道我能做些什么才能让我按照我的问题使用代码。 亲切的问候,
答案 0 :(得分:3)
你可以简单地做
Answer1.setVisible(false);
Answer2.setVisible(false);
您无需删除按钮。您可以轻松隐藏它们。或者,如果您愿意,在此项目中您还可以禁用按钮。
Answer1.setEnabled(false);
Answer2.setEnabled(false);
答案 1 :(得分:2)
您尝试从Answer1
移除Answer2
和btnHalfAndHalf
,但这些内容未包含在btnHalfAndHalf
中。只需Answer1.setVisible(false); Answer2.setVisible(false);
或Answer1.setEnabled(false); Answer2.setEnabled(false);