gpt4 book ai didi

java - 更改 Java Swing 的背景

转载 作者:行者123 更新时间:2023-12-02 04:05:41 25 4
gpt4 key购买 nike

抱歉代码太长。我的问题在于 Action 执行者。 (这个程序应该生成一个随机数,然后你用图形用户界面来猜测它。)所以如果猜错了数字,它会变成灰色,这就是我想要的。但如果它是正确的,它应该变成黄色,但它所做的只是使灰色背景消失。我已经尝试过它并且在某一时刻它们重叠了。

*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。当您向其中添加组件时,面板(通过其布局管理器)会根据子组件的需求计算新的尺寸,这就是为什么当 wrong 可见时,您可以看到 sp Pane ,但一旦它不再可见,面板就会恢复到默认的首选大小,因为没有任何实际显示的内容。

相反,您应该为标签提供“错误”和“正确”文本。但是,在执行此操作之前,您还应该停止向面板重复添加标签,只需添加一个标签并更新其文本即可。

此外,您的类不需要扩展 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();
}
});
}

}

关于java - 更改 Java Swing 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34301713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com