I'm just starting to use GUI programming. I'm trying to customize the frame but nothing is appearing so far when I add it to gameView, like the buttons or cat gif, so all I get is the frame window after I run it. Here's my code so far:
package experiment10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class mainFrame extends JFrame
{
public static int MAX = 500; // maximum number to guess
private JButton submitBtn; // Submit button
private JButton clearBtn; // Clear Input button
private JTextField inputNumber; //input guessed number
private int magicNumber; //randomly generated number
public static void main(String[] arg) {
//Show frame
JFrame frame = new JFrame();
frame.setVisible(true);
}
public mainFrame() {
setSize(400, 400);
setResizable (false);
setTitle("Let's Play Hi Lo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
magicNumber = (int)(Math.random()*MAX);
Container gameView = getContentPane();
gameView.setLayout(new FlowLayout());
//Customizations
submitBtn = new JButton("Submit"); //create submit button
clearBtn = new JButton("Clear"); //create clear button
gameView.add(submitBtn); //adds submit button
gameView.add(clearBtn); //adds clear button
JLabel imageLabel = new JLabel(new ImageIcon("cat.gif")); //creates image
gameView.add(imageLabel); //adds image
JLabel textLabel = new JLabel("Please enter your guess"); //creates JLabel
gameView.add(textLabel); //adds JLabel
JTextField input = new JTextField(); //creates JTextField
gameView.add(input); //adds JTextField
}
}
Any help/tips are very much appreciated. Thank you.
You will need to call your mainFrame() from your main method:
mainFrame frame = new mainFrame();
You can also remove the following code from the main:
JFrame frame = new JFrame();
frame.setVisible(true);
You will then need to add setVisible(true);
to your mainFrame() constructor.