很抱歉长代码。我的问题在于表演者。 (这个程序应该生成一个随机数,然后你猜它,使用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);
}
}*
答案 0 :(得分:2)
基本上,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();
}
});
}
}