JButton is displaying behind image - java

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);
}
}

Related

Background color not changing JFrame

I need help on Java Swing for GUI. I have included frame.getcontentpane().setBackground(color.cyan); to the code but frame background color doesn't change.
import javax.swing.*;
import java.awt.*;
public class LoginOne {
private static JLabel lblUsr;
private static JButton btnNext;
private static JTextField txtUsr;
private static JFrame frame;
private static JPanel panel;
public static void main(String[] args) {
frame = new JFrame("Home Page");
frame.setSize(800,600);
frame.getContentPane().setBackground(Color.cyan);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
frame.add(panel);
panel.setLayout(null);
lblUsr = new JLabel("Username"); //Username Label
lblUsr.setBounds(10,20,80,25);
panel.add(lblUsr);
txtUsr = new JTextField(20); //Username input field
txtUsr.setBounds(80,20,80,25);
panel.add(txtUsr);
btnNext = new JButton("Next");
btnNext.setBounds(80,90,80,25);
panel.add(btnNext);
frame.setVisible(true);
}
}
Use panel.setBackground(Color.CYAN); instead.

GUI Programming - Customizing frame

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.

Java Button Placement using BorderLayout

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);
}
}

Java JFrame components

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 want 2 JLabels to show on the same JFrame in Java

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 !

Categories