AWT按钮背景色修改异常求助:Safe25程序背景色错误设置到Frame
Hey there! Let's get your Safe25 program working right. I see two key things to fix here: the background color getting applied to the Frame instead of the buttons, and adding the logic to track the input sequence "15032018" to close the program when entered correctly.
The Core Problem
Your commented line (originally line 75) was targeting the Frame itself with setBackground() instead of the specific button that was clicked. We need to adjust that to modify the button's background instead. Plus, we need to add logic to track the input sequence and handle correct/incorrect entries.
Corrected Full Code
Here's the updated code with fixes and the necessary sequence tracking logic:
import java.awt.event.*; import java.awt.*; import javax.swing.*; public class Safe25 extends Frame implements ActionListener { JButton[] buttons; private String correctSequence = "15032018"; // The target input sequence private int currentInputIndex = 0; // Tracks where we are in the sequence Safe25() { // Konstruktor setSize(250, 300); setLocation(300, 300); setTitle("Safe25"); buttons = new JButton[10]; for (int i = 0; i < 10; i++) { // 10 Knöpfe im Array buttons[i] = new JButton("" + i); buttons[i].setFont(new Font("Courier", Font.BOLD, 34)); buttons[i].addActionListener(this); // Make sure buttons are opaque so background color shows buttons[i].setOpaque(true); buttons[i].setBorderPainted(false); } Panel panel0 = new Panel(); panel0.setLayout(new GridLayout(1, 1)); panel0.add(buttons[0]); Panel panelRest = new Panel(); panelRest.setLayout(new GridLayout(3, 3)); for (int i = 1; i < 10; i++) { panelRest.add(buttons[i]); } setLayout(new GridLayout(2, 1)); add(panel0); // Panel mit 0-Knopf add(panelRest); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent wv) { System.exit(0); } }); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { JButton clickedButton = (JButton) e.getSource(); String buttonText = clickedButton.getText(); // Check if current input matches the sequence if (buttonText.equals(String.valueOf(correctSequence.charAt(currentInputIndex)))) { // Correct input: turn button green clickedButton.setBackground(Color.GREEN); currentInputIndex++; // If we've completed the full sequence, close the program if (currentInputIndex == correctSequence.length()) { System.exit(0); } } else { // Wrong input: turn button red and reset sequence progress clickedButton.setBackground(Color.RED); currentInputIndex = 0; // Optional: reset all button colors to default after wrong input // for (JButton btn : buttons) { // btn.setBackground(null); // } } } public static void main(String[] args) { new Safe25(); } }
Key Fixes & Additions
- Button Background Targeting: Instead of using
setBackground()on the Frame, we cast the event source toJButton((JButton) e.getSource()) and modify its background color directly. This fixes your original line 75 issue. - Sequence Tracking: Added
correctSequenceandcurrentInputIndexto keep track of where the user is in the required input sequence. - Opaque Buttons: Set buttons to
opaque(true)and turned off border painting so the background color is visible (Swing buttons don't show background by default otherwise). - Input Logic:
- Correct input: Turns the button green and moves to the next character in the sequence. When the full sequence is entered, the program closes.
- Wrong input: Turns the button red and resets the sequence progress (you can uncomment the optional code to reset all button colors if needed).
How It Works
Now when you click buttons in the order 1 → 5 → 0 → 3 → 2 → 0 → 1 → 8, each correct button turns green, and once the last button is clicked, the program closes automatically. Any wrong click turns that button red and starts the sequence over.
内容的提问来源于stack exchange,提问作者J.Dupla

