Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaSwing上更改背景_Java_Swing - Fatal编程技术网

在JavaSwing上更改背景

在JavaSwing上更改背景,java,swing,Java,Swing,对不起,代码太长了。我的问题在于动作表演者。(这个程序应该生成一个随机数,然后你用gui猜它。)因此,如果猜错了数字,它会变成灰色,这是我想要的。但如果它是正确的,它应该变成黄色,但它所做的只是使灰色背景消失。我已经试过了,但有一点是重叠的 *import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.a

对不起,代码太长了。我的问题在于动作表演者。(这个程序应该生成一个随机数,然后你用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);
    }
}*

基本上,
JPanel
的默认首选大小为
0x0
。将组件添加到面板中时,面板(通过其布局管理器)会根据子组件的需要计算新的大小,这就是为什么当
错误的
可见时,可以看到
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();
            }
        });
    }

}

“避免说谢谢”??不,谢谢你,兄弟!lol然而,为什么要在结果JLabel中添加空格(“”)?带有
“”
JLabel
(emprty)文本与没有文本的
JLabel
是一样的。我之所以使用
是为了测试,我只是想看看标签是否真的会出现