Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Nancy Melucci
Courses Plus Student 36,159 PointsJFrame interface and its component don't show.
This is a partial program but I won't be able to go any further unless someone can tell me how to make the interface appear.
I am very new to JFrame and I am actually a little curious why I am not being taught JavaFX in the class for which this is an assignment. I've worked with it and it's far more intuitive. But I am not the boss...so I need to do it the way I am required.
Thanks to anyone who can help me figure out the issue. It is not crashing but I am getting a blank grey box...
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.KeyListener;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.Color;
import static java.awt.PageAttributes.ColorType.COLOR;
public class GuessOMatic extends Frame implements ActionListener, KeyListener {
String title = "GuessOMatic";
int secretNumber;
int guess = 0;
int zipoids = 100;
int numTries = 0;
int numGames = 0;
JLabel prompt = new JLabel("Make your guess!");
TextField theGuess = new TextField(10);
JLabel bankRoll = new JLabel(zipoids + " Zipoids");
JButton newPlayer = new JButton("New Player");
JButton newNumber = new JButton("New Number");
TextField thePlayer = new TextField(20);
TextField theOutput = new TextField();
String playerName = " ";
int theNumber;
double amountRemaining = 100.0;
Random randomizer = new Random();
//theNumber = randomizer.nextInt(99) + 1;
JPanel contentPane;
public GuessOMatic(String title) {
super(title);
setLayout(new FlowLayout());
addKeyListener(this);
contentPane = new JPanel(new BorderLayout(500, 500));
contentPane.setVisible(true);
JPanel p1 = new JPanel();
p1.setSize(300,100);
p1.setVisible(true);
contentPane.add(p1, BorderLayout.NORTH);
JLabel guessPrompt = new JLabel("Make your guess!");
p1.add(guessPrompt);
p1.add(theGuess);
p1.add(bankRoll);
JPanel p2 = new JPanel();
contentPane.add(p2, BorderLayout.SOUTH);
p2.add(newPlayer);
p2.add(thePlayer);
p2.add(newNumber);
JLabel dummy1 = new JLabel();
contentPane.add(dummy1, BorderLayout.EAST);
JLabel dummy2 = new JLabel();
contentPane.add(dummy2, BorderLayout.WEST);
JScrollPane scrollArea = new JScrollPane(theOutput);
contentPane.add(scrollArea, BorderLayout.CENTER);
newPlayer.addActionListener(this);
newNumber.addActionListener(this);
thePlayer.addKeyListener(this);
theGuess.addKeyListener(this);
//GuessOMatic.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
*/
}
public static void main(String[] args) {
JFrame GuessOMatic = new JFrame();
GuessOMatic.setSize(550,400);
GuessOMatic.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
//text.setText("You guessed " + numGuesses + " times");
}
public void newPlayer(){
theOutput.setText(" ");
theGuess.setEnabled(false);
newPlayer.setEnabled(false);
newNumber.setEnabled(false);
theOutput.setEnabled(false);
theGuess.setBackground(Color.WHITE);
thePlayer.setEnabled(true);
thePlayer.setText(" ");
thePlayer.requestFocusInWindow();
thePlayer.setBackground(Color.YELLOW);
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyPressed(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
2 Answers
Jeremy Hill
29,567 PointsIn your class GuessOMatic you should probably extend JFrame, not Frame. Then, in your METHOD main() you should do it this way:
GuessOMatic guessOMatic = new GuessOMatic(); // This is already a JFrame object since you extended it.
guessOMatic.setSize(550, 550); // You can set this to whatever you need to.
guessOMatic.setVisible(true);
guessOMatic.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Also, I see where you added stuff to the JPanels, now you need to add the panels to the JFrame. If you don't add the panels they won't be visible in your JFrame- it will be a blank window.
I didn't read all of your code but if this doesn't work or if you have more trouble let us know.
Nancy Melucci
Courses Plus Student 36,159 PointsYou rock. I will stay in touch. Working on this for the better part of the day today.