Ich erstelle ein Wer möchte Millionär Spiel werden und habe eine halbe und eine halbe Taste, die ich verwenden möchte, um zwei Antworten zu entfernen, die JButtons sind. Hier ist der Code für zwei JButtons, die Antwortmöglichkeiten sind.Entfernen Sie JButtons mit einem 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);
Um dies durchzuführen ich einige tat und fand diese Methode und versuchte es, aber es funktioniert nicht für mich. Hier zeigt der Code, was ich versucht habe, mit der halb und halb Taste
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);
Bitte lassen Sie mich wissen, zu tun, was ich damit tun könnte, um es zu tun, was ich den Code in meine Frage intend verwenden. Mit freundlichen Grüßen,