Reputation: 3
Sorry about the long code. My problem is in the action performer. (This program is supposed to generate a random number, then you guess it, using a gui.) so if a guess the wrong number, it'll turn grey, which i want. but if its right it's supposed to turn yellow, but all it does is make the grey background disappear. i've played around with it and at one point they overlapped.
*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);
}
}*
Upvotes: 0
Views: 70
Reputation: 347332
Basically, JPanel
has a default preferred size of 0x0
. When you add components to it, the panel (via it's layout manager) calculates the new size based on the needs of the child components, this is why when the wrong
is visible, you can see the sp
pane, but once it's no longer visible, the panel resorts back to it's default preferred size, because there is nothing to actually show.
Instead, you should supply "wrong" and "right" text to the label. But, before you do that, you should also stop adding labels repeatedly to the panel, simply add one and update it's text.
Also, there is no need that you class needs to extend JFrame
, it's just confusing matters
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();
}
});
}
}
Upvotes: 2