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.
Related
So hello there :), this is my fist post here.
Lets get right into it:
My problem:
I put a calculator as a background image, than added a JButton to it.
My Problem(s):
When I start the program the calculator is quickly shown, then it vanishes and the button is shown
When I resize the window, the calculator shows up and the button dissapears
How can I make this work?
Heres my code:
import javax.swing.*;
import javax.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame {
private ImageIcon image;
private JLabel label;
Calculator() {
image = new ImageIcon(getClass().getResource("TStiny.png"));
label = new JLabel(image);
add(label);
}
public static void main (String args[]) {
Calculator gui = new Calculator();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("Texas Instruments TI-30XIIS");
gui.pack();
gui.setVisible(true);
JPanel panel = new JPanel();
gui.add(panel);
JButton button7 = new JButton();
button7.setIcon(new ImageIcon(Calculator.class.getResource("button_7.png")));
button7.setVisible(true);
button7.setBorderPainted(false);
button7.setBounds(90, 445, 45, 35);
panel.add(button7);
}
Thanks in advance! :)
You need to specify the layout and eventual position to which you want to add the component. JFrame uses BorderLayout by default.
If you want to use the default behavior, you should put a position on which to place the component:
public class Calculator extends JFrame {
private ImageIcon image;
private JLabel label;
Calculator() {
image = new ImageIcon(getClass().getResource("TStiny.png"));
label = new JLabel(image);
add(label, BorderLayout.LINE_START);
}
public static void main(String args[]) {
Calculator gui = new Calculator();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("Texas Instruments TI-30XIIS");
gui.pack();
gui.setVisible(true);
JPanel panel = new JPanel();
gui.add(panel, BorderLayout.LINE_END);
JButton button7 = new JButton();
button7.setIcon(new ImageIcon(Calculator.class.getResource("button_7.png")));
button7.setVisible(true);
button7.setBorderPainted(false);
button7.setBounds(90, 445, 45, 35);
panel.add(button7);
}
}
Another option is to specify a layout that does not want to specify a position - such as FlowLayout:
public class Calculator extends JFrame {
private ImageIcon image;
private JLabel label;
Calculator() {
setLayout(new FlowLayout());
image = new ImageIcon(getClass().getResource("TStiny.png"));
label = new JLabel(image);
add(label);
}
public static void main(String args[]) {
Calculator gui = new Calculator();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("Texas Instruments TI-30XIIS");
gui.pack();
gui.setVisible(true);
JPanel panel = new JPanel();
gui.add(panel);
JButton button7 = new JButton();
button7.setIcon(new ImageIcon(Calculator.class.getResource("button_7.png")));
button7.setVisible(true);
button7.setBorderPainted(false);
button7.setBounds(90, 445, 45, 35);
panel.add(button7);
}
}
This code displays nothing, I have exhausted many avenues but it does not display anything on the GUI (I have a main class that calls this as well already). Please help. I am trying to put the two JButtons horizontally at the bottom of the page and the JTextField and JLabel at the center of the screen.
package test;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
private JLabel label;
private JButton clear;
private JButton copy;
private JTextField textfield;
public Gui(){
super("test");
clear = new JButton("Clear");
copy = new JButton("Copy");
label = new JLabel("");
textfield = new JTextField("enter text here");
JPanel bottom = new JPanel(new BorderLayout());
JPanel subBottom = new JPanel();
subBottom.add(copy);
subBottom.add(clear);
JPanel centre = new JPanel (new BorderLayout());
JPanel subCentre = new JPanel();
subCentre.add(label);
subCentre.add(textfield);
bottom.add(subBottom, BorderLayout.PAGE_END);
centre.add(subCentre, BorderLayout.CENTER);
}
}
Your code is a bit over-complicated. You only need two panels, centre, and buttons. There are two reasons your UI is not showing up:
You never added the panels to the frame
You never set visible to true(Achieve this by using setVisible(true)), unless you did this in the class you ran it in.
One simple way to achieve your desired UI is like so(I added a main method to show the window):
import javax.swing.*;
import java.awt.*;
public class test extends JFrame {
public Test() {
super("test");
JPanel buttons = new JPanel();
JPanel centre = new JPanel();
add(buttons, BorderLayout.SOUTH); //these lines add the
add(centre, BorderLayout.CENTER); //panels to the frame
JButton clear = new JButton("Clear"); // No need
JButton copy = new JButton("Copy"); // to declare
JLabel label = new JLabel("Label"); // these
JTextField textfield = new JTextField("enter text here"); // privately
buttons.add(copy);
buttons.add(clear);
centre.add(label);
centre.add(textfield);
pack();
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
} //end constructor
//added main method to run the UI
public static void main(String[] args) {
new Experiments();
} //end main
} //end class
And it shows the window:
I got closer but its not pretty code, the JFrame is 500x500 so this works based on that... any better suggestions than what I have?
package lab6;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
private JLabel label;
private JButton clear;
private JButton copy;
private JTextField textfield;
public Gui(){
super("test");
clear = new JButton("Clear");
copy = new JButton("Copy");
label = new JLabel("label");
textfield = new JTextField("enter text here");
JPanel masterPanel = new JPanel(new BorderLayout());
JPanel top = new JPanel();
top.setPreferredSize(new Dimension(100, 200));
JPanel bottom = new JPanel();
JPanel subBottom = new JPanel();
subBottom.add(copy);
subBottom.add(clear);
JPanel centre = new JPanel ();
JPanel subCentre = new JPanel();
subCentre.add(label);
subCentre.add(textfield);
bottom.add(subBottom);
centre.add(subCentre);
masterPanel.add(bottom, BorderLayout.PAGE_END);
masterPanel.add(top, BorderLayout.PAGE_START);
masterPanel.add(centre, BorderLayout.CENTER);
add(masterPanel);
}
}
I'm trying to create a simple 500x500 applet with a button, 2-label, and textfield. The applet opens up but it is just blank no components are displaying nor will the color change. Not sure what is happening or what I'm missing exactly.
import java.applet.*;
import java.awt.Color;
import javax.swing.*;
public class Greeting {
private JFrame frame;
private JPanel panel;
private JLabel label1;
private JTextField textbox1;
private JButton button1;
private JLabel label2;
public Greeting(){
gui();
}
public void gui(){
frame = new JFrame("Greeting");
frame.setVisible(true);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
panel = new JPanel();
panel.setBackground(Color.YELLOW);
label1 = new JLabel ("Please enter your name");
textbox1 = new JTextField(20);
button1 = new JButton ("Greet");
panel.add(label1);
panel.add(button1);
panel.add(textbox1);
frame.getContentPane().add(panel);
frame.add(panel);
}
public static void main(String[] args) {
new Greeting();
}
}
If you are planning on displaying frame with all the components, then move the frame.setVisible(true) line to the end of the method:
public void gui() {
...
frame.add(panel);
frame.setVisible(true);
}
This allows for all the components to be added to the JFrame before it is displayed on the screen.
I can't get the JLabel pieces to both show up on the same JFrame. I've tried resizeing; but it didn't work. Do I need to place the JLabel's in a JPanel? I want both JLabels' to show on the same JFrame. Any help is appreciated. Thanks.
public class HelloWorldFrame extends JFrame
{
private TitledBorder title;
private TitledBorder title2;
public HelloWorldFrame()
{
super("Hello World! ");
JFrame helloWorld = new JFrame();
JLabel label = new JLabel();
title = BorderFactory.createTitledBorder("Language");
title.setTitleJustification(TitledBorder.LEFT);
label.setBorder(title);
add(label);
setSize(300, 200);
JRadioButton button1 = new JRadioButton("English");
JRadioButton button2 = new JRadioButton("French");
JRadioButton button3 = new JRadioButton("Spanish");
label.setLayout(new FlowLayout());
label.add(button1);
label.add(button2);
label.add(button3);
JLabel label2 = new JLabel();
title2 = BorderFactory.createTitledBorder("Greeting");
title2.setTitleJustification(TitledBorder.LEFT);
label2.setBorder(title2);
label2.setSize(100, 100);
add(label2);
helloWorld.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//The main begins below
import javax.swing.JFrame;
public class HelloWorldApp
{
public static void main(String[] args)
{
JFrame helloWorld = new HelloWorldFrame();
helloWorld.setVisible(true);
}
}
You need layout manager like in this example.
The structure you should have is : JFrame > JPanel > JLabel.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel label_1 = new JLabel("some text");
JLabel label_2 = new JLabel("other text");
panel.add(label_1);
panel.add(label_2);
frame.add(panel);
frame.pack();
frame.setVisible(true);
In your code you have some JButton: add them to the JPanel, not to the JLabel !
I'm trying to use a grid layout to make a GUI window. I add all my components and it compiles but when it runs it doesn't show anything. I'm trying to make a simple layout grouped and stacked like this.
{introduction message}
{time label
time input text}
{gravity label
gravity input text}
{answer label
answer text box}
{calculate button clear button}
Here is my code
import javax.swing.*;
import java.awt.*;
public class TurnerRandyFallingGUI extends JFrame
{
final int WINDOW_HEIGHT=500;
final int WINDOW_WIDTH=500;
public TurnerRandyFallingGUI()
{
setTitle("Falling Distance Calculator");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 5));
//labels
JLabel introMessage = new JLabel("Welcome to the Falling distance"+
"calculator");
JLabel timeLabel = new JLabel("Please enter the amount of time "+
"in seconds the object was falling.");
JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+
"forced onto the object");
JLabel answerLabel = new JLabel("Answer");
//text fields
JTextField fTime = new JTextField(10);
JTextField gForce = new JTextField(10);
JTextField answerT = new JTextField(10);
//buttons
JButton calculate = new JButton("Calculate");
JButton clr = new JButton("clear");
//panels
JPanel introP = new JPanel();
JPanel timeP = new JPanel();
JPanel gravityP = new JPanel();
JPanel answerP = new JPanel();
JPanel buttonsP = new JPanel();
//adding to the panels
//intro panel
introP.add(introMessage);
//time panel
timeP.add(timeLabel);
timeP.add(fTime);
//gravity panel
gravityP.add(gravityLabel);
gravityP.add(gForce);
//answer panel
answerP.add(answerLabel);
answerP.add(answerT);
//button panel
buttonsP.add(calculate);
buttonsP.add(clr);
setVisible(true);
}
public static void main(String[] args)
{
new TurnerRandyFallingGUI();
}
}
You've added nothing to the JFrame that your class above extends. You need to add your components to containers whose hierarchy eventually leads to the top level window, to the this if you will. In other words, you have no add(someComponent) or the functionally similar this.add(someComponent)method call in your code above.
Consider adding all of your JPanels to a single JPanel
Consider adding that JPanel to the JFrame instance that is your class by calling add(thatJPanel).
Even better would be to not extend JFrame and just to create one when needed, but that will likely be the subject of another discussion at another time.
Before setVisible (true) statement add following statements:
add (introP);
add (timeP);
add (gravityP);
add (answerP);
add (buttonsP);
There is nothing in your JFrame. That is the reason
import javax.swing.*;
import java.awt.*;
public class TurnerRandyFallingGUI extends JFrame
{
final int WINDOW_HEIGHT=500;
final int WINDOW_WIDTH=500;
public TurnerRandyFallingGUI()
{
//labels
JLabel introMessage = new JLabel("Welcome to the Falling distance"+
"calculator");
JLabel timeLabel = new JLabel("Please enter the amount of time "+
"in seconds the object was falling.");
JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+
"forced onto the object");
JLabel answerLabel = new JLabel("Answer");
//text fields
JTextField fTime = new JTextField(10);
JTextField gForce = new JTextField(10);
JTextField answerT = new JTextField(10);
//buttons
JButton calculate = new JButton("Calculate");
JButton clr = new JButton("clear");
//panels
JPanel introP = new JPanel();
JPanel timeP = new JPanel();
JPanel gravityP = new JPanel();
JPanel answerP = new JPanel();
JPanel buttonsP = new JPanel();
//adding to the panels
//intro panel
introP.add(introMessage);
//time panel
timeP.add(timeLabel);
timeP.add(fTime);
//gravity panel
gravityP.add(gravityLabel);
gravityP.add(gForce);
//answer panel
answerP.add(answerLabel);
answerP.add(answerT);
//button panel
buttonsP.add(calculate);
buttonsP.add(clr);
setLayout(new GridLayout(5, 1));
this.add(introP);
this.add(timeP);
this.add(gravityP);
this.add(answerP);
this.add(buttonsP);
setTitle("Falling Distance Calculator");
this.pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
this.validate();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TurnerRandyFallingGUI();
}
});
}
}
Consider the following
In GridLayout, the first parameter is Rows, Second is columns
Never set the size of JFrame manually. Use pack() method to decide
the size
Use SwingUtilities.InvokeLater() to run the GUI in another thread.