Background color not changing JFrame - java

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.

Related

JButton is displaying behind image

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

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 !

CardLayout alignment is not proper

I am a beginner to java. In second cardpanel the username and password alignment is not coming properly. Is there any way to fix it? I would also like to know what is the disadvantage of using multiple frames.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CardLayoutTest extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel cardPanel, jp1, jp2, buttonPanel;
private JLabel jl1, jl2;
private JTextField jt1;
private JPasswordField jt2;
private JButton btn1, btn2;
private CardLayout cardLayout = new CardLayout();
public CardLayoutTest() {
setTitle("Login");
setSize(400, 300);
cardPanel = new JPanel();
buttonPanel = new JPanel();
cardPanel.setLayout(cardLayout);
jp1 = new JPanel();
jp2 = new JPanel();
jt1=new JTextField();
jt2=new JPasswordField();
jl1 = new JLabel("Username");
jl2 = new JLabel("Password");
//jp1.add(jl1);
jp2.add(jl1);
jp2.add(jt2);
jp2.add(jl2);
jp2.add(jt2);
cardPanel.add(jp1, "1");
cardPanel.add(jp2, "2");
btn2 = new JButton("Show Card 2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "2");
}
});
buttonPanel.add(btn2);
add(cardPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
CardLayoutTest frame = new CardLayoutTest();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
In second cardpanel the username and password alignment is not coming properly.
By default a JPanel uses a FlowLayout so the components are displayed on a single line.
Is there any way to fix it?
Use an appropriate layout manager (or combination of layout managers) to get the desired alignment.
Read the section from the Swing tutorial on Layout Managers for more information and examples.

Why am I getting this exception? Trying to make a simple GUI in Java

import java.awt.*;
import javax.swing.*;
public class userInput extends JFrame {
private JButton newEntry;
private JButton deleteEntry;
private JButton editEntry;
private JButton saveEntry;
private JButton cancelEntry;
private FlowLayout layout;
public userInput() {
super("My Address Book"); //sets the title!
JTextField field = new JTextField(20);
Container content = getContentPane();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(newEntry);
buttonPanel.add(deleteEntry);
buttonPanel.add(editEntry);
buttonPanel.add(saveEntry);
buttonPanel.add(cancelEntry);
add(buttonPanel, BorderLayout.SOUTH);
content.setLayout(new BorderLayout());
content.add(buttonPanel, "South");
setVisible(true);
}
}
Here is my driver program:
import javax.swing.*;
public class AddressBookGui {
public static void main (String[] args)
{
userInput addressBook = new userInput();
addressBook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //whenever you hit x you will exit the program
addressBook.setSize(750, 600);
addressBook.setVisible(true);
}
}
you have to initialize newEntry before doing
newEntry = new JButton("foo");
buttonPanel.add(newEntry);
along with the other buttons
You forgot to allocate your Buttons:
newEntry = new JButton();
deleteEntry = new JButton();
...

Categories