有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

改变Java Swing的背景

抱歉,代码太长了。我的问题在于动作表演者。(这个程序应该生成一个随机数,然后使用gui猜测。)所以,如果你猜错了号码,它会变成灰色,这是我想要的。但如果它是对的,它应该变成黄色,但它所做的只是让灰色背景消失。我已经试过了,但有一点是重叠的

*import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GuessingGameGui extends JFrame implements ActionListener{
    JFrame window = new JFrame();
    JButton button;
    JTextField input;
    JPanel cp = new JPanel();
    JPanel sp = new JPanel();
    int RN = (int) (10 * Math.random()) + 1;
    JPanel subpanel = new JPanel();
    int attempts = 1;

    public GuessingGameGui()
    {
        window.setTitle("Guessing Game");
        window.setSize(400, 300);
        boolean go = false;
        int correct;

        JPanel np = new JPanel();
        np.setLayout(new BorderLayout());
        window.add(np, BorderLayout.NORTH);
        JLabel question;
        question = new JLabel("Guess a number between 1 and 10:");
        np.add(question);

        cp.setLayout(new BorderLayout());

        button = new JButton("Guess");
        button.addActionListener(this);
        subpanel.add(button);

        input = new JTextField(2);
        subpanel.add(input);
        cp.add(subpanel);
        window.add(cp, BorderLayout.CENTER);

        sp.setLayout(new BorderLayout());
        window.add(sp, BorderLayout.SOUTH);
        JLabel result;
        result = new JLabel();

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("Guess"))
        {
            JLabel wrong = new JLabel("Sorry try again.");
            sp.add(wrong);
            wrong.setVisible(false);

            String text = input.getText();
            int number = Integer.parseInt(text);

            if (RN != number)
            {
                sp.setBackground(Color.GRAY);
                wrong.setVisible(true);
                attempts++;             
            }
            else 
            {
                sp.setBackground(Color.GREEN);
                sp.setVisible(true);
            }
        }
    }

    public static void main(String[] args)
    {
        GuessingGameGui g = new GuessingGameGui();
        g.setVisible(true);
    }
}*

共 (1) 个答案

  1. # 1 楼答案

    基本上,JPanel的默认首选大小为0x0。添加组件时,面板(通过其布局管理器)会根据子组件的需要计算新的大小,这就是为什么当wrong可见时,可以看到sp窗格,但一旦它不再可见,面板就会恢复到默认的首选大小,因为实际上没有什么可显示的

    相反,你应该为标签提供“错误”和“正确”的文本。但是,在你这么做之前,你也应该停止在面板上重复添加标签,只需添加一个标签并更新它的文本

    另外,您的类不需要扩展JFrame,这只是让人困惑

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class GuessingGameGui implements ActionListener {
    
        JFrame window = new JFrame();
        JButton button;
        JTextField input;
        JPanel cp = new JPanel();
        JPanel sp = new JPanel();
        int RN = (int) (10 * Math.random()) + 1;
        JPanel subpanel = new JPanel();
        int attempts = 1;
    
        JLabel result;
    
        public GuessingGameGui() {
    
            window.setTitle("Guessing Game");
            window.setSize(400, 300);
            boolean go = false;
            int correct;
    
            JPanel np = new JPanel();
            np.setLayout(new BorderLayout());
            window.add(np, BorderLayout.NORTH);
            JLabel question;
            question = new JLabel("Guess a number between 1 and 10:");
            np.add(question);
    
            cp.setLayout(new BorderLayout());
    
            button = new JButton("Guess");
            button.addActionListener(this);
            subpanel.add(button);
    
            input = new JTextField(2);
            subpanel.add(input);
            cp.add(subpanel);
            window.add(cp, BorderLayout.CENTER);
    
            sp.setLayout(new BorderLayout());
            window.add(sp, BorderLayout.SOUTH);
    
            result = new JLabel(" ");
            sp.add(result);
    
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setVisible(true);
    
        }
    
        public void actionPerformed(ActionEvent e) {
    
            if (e.getActionCommand().equals("Guess")) {
    
                String text = input.getText();
                int number = Integer.parseInt(text);
    
                System.out.println(RN);
                if (RN != number) {
    
                    result.setText("Sorry try again");
                    sp.setBackground(Color.GRAY);
                    attempts++;
    
                } else {
                    result.setText("Good guess well done!");
                    sp.setBackground(Color.GREEN);
                }
                sp.setVisible(true);
    
            }
    
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    GuessingGameGui g = new GuessingGameGui();
                }
            });
        }
    
    }