Why isn't anything showing up in my JFrame? - java

Why is my GUI not showing any buttons, labels, or text fields?
I think I have it all setup, but when I run it, only the frame shows, and none of the contents appear.
package BasicGame;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Gui extends JFrame{
private static final long serialVersionUID = 1L;
private JLabel label;
private JTextField textField;
private JButton button;
private buttonHandler bHandler;
public Gui(){
setTitle("Basic Gui");
setResizable(false);
setSize(500, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(null);
button = new JButton("button");
button.setBounds(50, 60, 50, 70);
bHandler = new buttonHandler();
button.addActionListener(bHandler);
label = new JLabel("Hello", SwingConstants.RIGHT);
label.setBounds(50, 60, 50, 70);
textField = new JTextField(10);
textField.setBounds(50, 60, 50, 70);
pane.add(button);
pane.add(label);
pane.add(textField);
}
public class buttonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
#SuppressWarnings("unused")
public static void main(String[] args){
Gui gui = new Gui();
}
}

Move your setVisible() to the end of the constructor. You are adding all of your components after you set your JFrame up and make is visible, so you don't see any of the changes.
This should show your JFrame with all the components:
public Gui(){
setTitle("Basic Gui");
setResizable(false);
setSize(500, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(null);
button = new JButton("button");
button.setBounds(50, 60, 50, 70);
bHandler = new buttonHandler();
button.addActionListener(bHandler);
label = new JLabel("Hello", SwingConstants.RIGHT);
label.setBounds(50, 60, 50, 70);
textField = new JTextField(10);
textField.setBounds(50, 60, 50, 70);
pane.add(button);
pane.add(label);
pane.add(textField);
setVisible(true); // Move it to here
}
Here's what the frame's looked like before and after I moved the setVisible statement and compiled your code.
Before:
After:

Related

How to refresh JInternalFrame or JPanel form on button click where JPanel is a separate class and used in JInternalFrame

Iam trying to build a desktop application with multiple screens inside one single JFrame.
So each button click event will take us to the separate screen with refreshed components in the screen. So far this approach is working for me but the problem I am facing is even after using ".dispose(), .repaint(), .revalidate(), .invalidate()" functions. JInternalFrame or Jpanel seems to not refresh its components.
Which works something like below gif.
Tabbed Style
I do know JtabbedPane exists but for my method JtabbedPane is not viable.
Below I am posting minified code by replicating the problem I am facing.
MainMenu.Java(file with Main Class)
package test;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JInternalFrame;
public class MainMenu extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainMenu frame = new MainMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainMenu() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 841, 522);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 807, 63);
contentPane.add(panel);
panel.setLayout(new GridLayout(1, 0, 0, 0));
JButton Tab1 = new JButton("Tab1");
panel.add(Tab1);
JButton Tab2 = new JButton("Tab2");
panel.add(Tab2);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 88, 807, 387);
contentPane.add(scrollPane);
JInternalFrame internalFrame1 = new JInternalFrame();
JInternalFrame internalFrame2 = new JInternalFrame();
Tab1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Panel1 panel1 = new Panel1();
if(internalFrame1 !=null) {
internalFrame1.dispose();
panel1.invalidate();
panel1.revalidate();
panel1.repaint();
}
internalFrame1.setTitle("Panel 1");
scrollPane.setViewportView(internalFrame1);
internalFrame1.getContentPane().add(panel1);
internalFrame1.setVisible(true);
}
});
Tab2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Panel2 panel2 = new Panel2();
if(internalFrame2 !=null) {
internalFrame2.dispose();
panel2.invalidate();
panel2.revalidate();
panel2.repaint();
}
internalFrame2.setTitle("Panel 2");
scrollPane.setViewportView(internalFrame2);
internalFrame2.getContentPane().add(panel2);
internalFrame2.setVisible(true);
}
});
}
}
and the corresponding Jpanel class files where JInternal Frames
Panel1.java
package test;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
public class Panel1 extends JPanel {
private JTextField textField;
/**
* Create the panel.
*/
public Panel1() {
setLayout(null);
textField = new JTextField();
textField.setBounds(10, 60, 430, 19);
add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("Example Button");
btnNewButton.setBounds(10, 156, 430, 21);
add(btnNewButton);
}
}
Panel2.java
package test;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
public class Panel2 extends JPanel {
private JTextField textField;
/**
* Create the panel.
*/
public Panel2() {
setLayout(null);
textField = new JTextField();
textField.setBounds(10, 60, 430, 19);
add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("New button2");
btnNewButton.setBounds(21, 157, 419, 21);
add(btnNewButton);
}
}
P.S: This is my first time asking question in Stackoverflow so forgive me and if possible guide me if i miss anything
Thank you :)
Edit:
The problem I am facing is on the surface it looks like the Jpanel has been refreshed but the components like JtextField Still hides the previously written text in it and only show the text when i click on that JTextField
Below I am Attaching another gif which show highlights the issue. I have highlighted the part where I am facing issue.
Issue I am Facing
The dispose() method does not remove components so you keep adding components to the internal frame when you use the following:
internalFrame1.getContentPane().add(panel1);
Instead you might do something like:
Container contentPane = internalFrame1.getContentPane();
contentPane.removeAll();
contentPane.add( panel1 );
contentPane.revalidate();
contentPane.repaint();
You can use the JPanels in the Jframes and then use the CardLayout to change the panel ( which could than act like the different screens )

How do I open a JFrame from a button click

I making a game but I have a main meny and too outer menys and I want to open one of the menys by clicking on the respective button in the JFrame.
code of main.java
package main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import game.client.mainClient;
import game.server.mainServer;
public class main extends JPanel {
/**
*
*/
private static final long serialVersionUID = 6590770928148744094L;
private JLabel jcomp1;
private JButton jcomp5;
private JButton jcomp6;
private JLabel jcomp8;
private JLabel jcomp9;
private JLabel jcomp10;
public main() {
//construct components
jcomp1 = new JLabel ("test game");
jcomp5 = new JButton ("singel player");
jcomp6 = new JButton ("multiplayer");
jcomp8 = new JLabel ("this game is made by kebe_");
jcomp9 = new JLabel ("gui is made in guigenie");
jcomp10 = new JLabel ("game verision dev 1.0");
//adjust size and set layout
setPreferredSize (new Dimension (681, 466));
setLayout (null);
//add components
add (jcomp1);
add (jcomp5);
add (jcomp6);
add (jcomp8);
add (jcomp9);
add (jcomp10);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (295, 5, 70, 25);
jcomp5.setBounds (265, 30, 115, 30);
jcomp6.setBounds (265, 65, 115, 30);
jcomp8.setBounds (0, 430, 180, 25);
jcomp9.setBounds (0, 415, 155, 20);
jcomp10.setBounds (0, 445, 140, 25);
// close and open
// singleplayer
jcomp5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
mainClient mc = new mainClient();
mc.setVisible(true);
}
});
// multiplayer
jcomp6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
mainServer ms = new mainServer();
ms.setVisible(true);
}
});
}
public static void main (String[] args) {
JFrame frame = new JFrame ("Test game");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new main());
frame.pack();
frame.setVisible (true);
}
}
code of mainClient.java
package game.client;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class mainClient extends JPanel
{
/**
*
*/
private static final long serialVersionUID = -1271816540338950462L;
private JLabel jcomp1;
private JComboBox jcomp2;
private JButton jcomp3;
public mainClient() {
//construct preComponents
String[] jcomp2Items = {"save 1", "save 2", "save 3", "save 4", "save 5"};
//construct components
jcomp1 = new JLabel ("singel player");
jcomp2 = new JComboBox (jcomp2Items);
jcomp3 = new JButton ("play selected save");
//adjust size and set layout
setPreferredSize (new Dimension (681, 466));
setLayout (null);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (290, 5, 85, 25);
jcomp2.setBounds (280, 30, 100, 25);
jcomp3.setBounds (255, 70, 150, 25);
jcomp3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public static void main (String[] args) {
JFrame frame = new JFrame ("Singel player");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new mainClient());
frame.pack();
frame.setVisible (true);
}
}
code of mainServer
package game.server;
import java.awt.*;
import javax.swing.*;
public class mainServer extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 2726545572728204122L;
private JLabel jcomp1;
private JButton jcomp2;
private JButton jcomp3;
public mainServer() {
//construct components
jcomp1 = new JLabel ("multiplayer");
jcomp2 = new JButton ("host game");
jcomp3 = new JButton ("join game");
//adjust size and set layout
setPreferredSize (new Dimension (681, 466));
setLayout (null);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (300, 0, 75, 25);
jcomp2.setBounds (285, 25, 100, 25);
jcomp3.setBounds (285, 50, 100, 25);
}
public static void main (String[] args) {
JFrame frame = new JFrame ("Multiplayer");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new mainServer());
frame.pack();
frame.setVisible (true);
}
}
how can I open the JFrame in mainClient.java when I click on the singleplayer button, and the JFrame in mainServer.java when I click on the multiplayer button?
Create ActionListeners for your buttons. Define your frames, and you can access them with your class name. For example;
class mainClient extends JPanel {
JFrame mainClientFrame = new JFrame();
}
and then;
mainClient mc = new mainClient();
mc.mainClientFrame.setVisible(true);
if you want to use this frames in your main void, define them static.
class mainClient extends JPanel {
static JFrame mainClientFrame = new JFrame();
}

JPanel gets compressed and disappears when trying to move to the bottom

I have to do a project in Java and thought a GUI Text Adventure would be cool. My Problem is that when I create a JPanel and move it further down on the screen, the panel first changes its size and then disappears completely at one point.
On the GameScreen there should be a panel for choice Options to be put on but it refuses to go further down than about half the size of the Screen.
Here's the code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Yeet {
JFrame epicOfYeet;
Container con;
JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel;
JLabel titleNameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
Font normalFont = new Font ("Times New Roman", Font.PLAIN, 55);
JButton startButton;
JButton choice1;
JButton choice2;
JButton choice3;
JButton choice4;
JTextArea mainTextArea;
TitleScreenHandler tsHandler = new TitleScreenHandler();
public static void main(String[] args) {
new Yeet();
}
public Yeet() {
epicOfYeet = new JFrame();
epicOfYeet.setSize(1200, 1000);
epicOfYeet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
epicOfYeet.getContentPane().setBackground(Color.black);
epicOfYeet.setLayout(null);
con = epicOfYeet.getContentPane();
titleNamePanel = new JPanel();
titleNamePanel.setBounds(190, 100, 800, 230);
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("EPIC OF YEET");
titleNameLabel.setForeground(Color.red);
titleNameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(400, 500, 400, 100);
startButtonPanel.setBackground(Color.black);
startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
startButton.setFocusPainted(false);
titleNamePanel.add(titleNameLabel);
startButtonPanel.add(startButton);
con.add(titleNamePanel);
con.add(startButtonPanel);
epicOfYeet.setVisible(true);
}
public void createGameScreen(){
titleNamePanel.setVisible(false);
startButtonPanel.setVisible(false);
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 1000, 400);
mainTextPanel.setBackground(Color.green);
con.add(mainTextPanel);
mainTextArea = new JTextArea("You come to your senses again.\nThe dewy grass you're laying on is gleaming with moonlight.\nYour body aches as you get up and catch a \nglimpse of your surroundings.\n");
mainTextArea.setBounds(100, 100, 1000, 250);
mainTextArea.setBackground(Color.blue);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
choiceButtonPanel = new JPanel();
choiceButtonPanel.setBounds(300, 500, 600, 550);
choiceButtonPanel.setBackground(Color.red);
con.add(choiceButtonPanel);
}
public class TitleScreenHandler implements ActionListener{
#Override
public void actionPerformed(ActionEvent event) {
createGameScreen();
}
}
}
Here is a basic implementation using layout mangers, avoiding the bad practice of null layout manager.
It is not an optimal one, but should get you started:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Yeet {
JFrame epicOfYeet;
Container con;
JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel;
JLabel titleNameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
Font normalFont = new Font ("Times New Roman", Font.PLAIN, 55);
JButton startButton, coice1, choice2, choice3, choice4;
JTextArea mainTextArea;
TitleScreenHandler tsHandler = new TitleScreenHandler();
public static void main(String[] args) {
SwingUtilities.invokeLater(()->new Yeet());
}
public Yeet() {
epicOfYeet = new JFrame();
//epicOfYeet.setSize(1200, 1000); // do not set size. let layout manager calcualte it
epicOfYeet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
epicOfYeet.getContentPane().setBackground(Color.black);
//epicOfYeet.setLayout(null); //aviod null layouts Jframe uses BorderLayout by default
con = epicOfYeet.getContentPane();
titleNamePanel = new JPanel();
//titleNamePanel.setBounds(190, 100, 800, 230);
titleNamePanel.setPreferredSize(new Dimension(800, 230));//set preferred size to be used by layout manager
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("EPIC OF YEET");
titleNameLabel.setForeground(Color.red);
titleNameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
//startButtonPanel.setBounds(400, 500, 400, 100);
startButtonPanel.setPreferredSize(new Dimension(400, 100));
startButtonPanel.setBackground(Color.black);
startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
startButton.setFocusPainted(false);
titleNamePanel.add(titleNameLabel);
startButtonPanel.add(startButton);
con.add(titleNamePanel, BorderLayout.PAGE_START); //set to top
con.add(startButtonPanel, BorderLayout.PAGE_END); //set to bottom
epicOfYeet.pack();
epicOfYeet.setVisible(true);
}
public void createGameScreen(){
//titleNamePanel.setVisible(false);
//startButtonPanel.setVisible(false);
con.remove(titleNamePanel);
con.remove(startButtonPanel);
mainTextPanel = new JPanel();
//mainTextPanel.setBounds(100, 100, 1000, 400);
mainTextPanel.setBackground(Color.green);
con.add(mainTextPanel); // added to center position of the BorderLayout (the default)
mainTextArea = new JTextArea(10, 20); //size in rows, cols
mainTextArea.setText("You come to your senses again.\nThe dewy grass you're laying on is gleaming with moonlight.\nYour body aches as you get up and catch a \nglimpse of your surroundings.\n");
//mainTextArea.setBounds(100, 100, 1000, 250);
mainTextArea.setBackground(Color.blue);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
choiceButtonPanel = new JPanel();
//choiceButtonPanel.setBounds(300, 500, 600, 550);
choiceButtonPanel.setPreferredSize(new Dimension(600, 550));
choiceButtonPanel.setBackground(Color.red);
con.add(choiceButtonPanel, BorderLayout.PAGE_END);//add to bottom
epicOfYeet.pack();
}
public class TitleScreenHandler implements ActionListener{
#Override
public void actionPerformed(ActionEvent event) {
createGameScreen();
}
}
}

Not adding Card Layout in JFrame

Can anyone see the code ? I want to make a page that has a banner and a pannel in which cards will change on the requirement. I added the Banner in JFrame (That is working fine) but The problem is that " CardLayout Panel is not adding in the JFrame".
Actually, I need this.
When button is pressed only card1 change to card2 but banner will remain same.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class gui extends JFrame{
private static final long serialVersionUID = 1L;
JPanel
basic_panel,
card_Layout_panel,
banner_panel,
welcome_authenticaion_panel_card1;
CardLayout basic2;
JLabel
logo_label,
name_label;
public gui(){
server_login_gui();
add(basic_panel);
standard_gui();
}
public void server_login_gui(){
basic_panel = new JPanel();
basic_panel.setLayout(null);
basic_panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
banner_panel = new JPanel();
banner_panel.setLayout(null);
banner_panel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
banner_panel.setSize(680, 200);//(400,100,400,100);
//////Banner inner things//////////////////////////////////////////////////
logo_label = new JLabel("Logo");
logo_label.setBounds(30,40,100,100);
logo_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(logo_label);
name_label = new JLabel(" Name..... ");
name_label.setFont(new Font("Times new Roman", Font.BOLD | Font.ITALIC,25));
name_label.setBounds(200,80,400,50);
name_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(name_label);
////////////////////////////////////////////////////////////////////////
// basic_panel.add(banner_panel,BorderLayout.NORTH);
///////// Card Layout//////////////
basic2 = new CardLayout();
card_Layout_panel = new JPanel(basic2);
card_Layout_panel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 5));
basic_panel.add(card_Layout_panel,BorderLayout.CENTER);
welcome_authenticaion_panel_card1 = new JPanel();
welcome_authenticaion_panel_card1.setLayout(null);
welcome_authenticaion_panel_card1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
welcome_authenticaion_panel_card1.setSize(680, 200);//(400,100,400,100);
welcome_authenticaion_panel_card1.setBounds(0,200,680,460);
card_Layout_panel.add(welcome_authenticaion_panel_card1, "1");
basic_panel.add(card_Layout_panel,BorderLayout.CENTER);
/////////////////////////////////////////////////////////////////////////
}
public void standard_gui(){
setSize(700,700);
setTitle("System");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
I want to make a page that has a banner and a pannel in which cards
will change on the requirement.
your component aren't focusable, there is required some event (JButton, Swing Timer) for switching the view by using CardLayout
for more info about CardLayout to read Oracle tutorial, for working code exampes, tons code examples are here
you code works without NullLayout (by set BorderLayout to parent JPanel), default LayoutManager for Jpanel is FlowLayout (accepts only getPreferredSize, childs aren't resizable with its parent/s)
my question is for why reason is there code line basic_panel.add(card_Layout_panel, BorderLayout.CENTER); twice, and another ...
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Gui extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel basic_panel, card_Layout_panel,
banner_panel, welcome_authenticaion_panel_card1;
private CardLayout basic2;
private JLabel logo_label, name_label;
public Gui() {
server_login_gui();
add(basic_panel);
standard_gui();
}
public void server_login_gui() {
basic_panel = new JPanel();
basic_panel.setLayout(new BorderLayout(10, 10));
basic_panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
banner_panel = new JPanel();
//banner_panel.setLayout(null);
banner_panel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
banner_panel.setSize(680, 200);//(400,100,400,100);
//////Banner inner things//////////////////////////////////////////////////
logo_label = new JLabel("Logo");
//logo_label.setBounds(30, 40, 100, 100);
logo_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(logo_label);
name_label = new JLabel(" Name..... ");
name_label.setFont(new Font("Times new Roman", Font.BOLD | Font.ITALIC, 25));
//name_label.setBounds(200, 80, 400, 50);
name_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(name_label);
////////////////////////////////////////////////////////////////////////
basic_panel.add(banner_panel, BorderLayout.NORTH);
///////// Card Layout//////////////
basic2 = new CardLayout();
card_Layout_panel = new JPanel(basic2);
card_Layout_panel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 5));
basic_panel.add(card_Layout_panel, BorderLayout.CENTER);
welcome_authenticaion_panel_card1 = new JPanel();
welcome_authenticaion_panel_card1.setLayout(null);
welcome_authenticaion_panel_card1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
welcome_authenticaion_panel_card1.setSize(680, 200);//(400,100,400,100);
//welcome_authenticaion_panel_card1.setBounds(0, 200, 680, 460);
card_Layout_panel.add(welcome_authenticaion_panel_card1, "1");
basic_panel.add(card_Layout_panel, BorderLayout.CENTER);
/////////////////////////////////////////////////////////////////////////
}
public void standard_gui() {
setSize(700, 700);
setTitle("System");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Gui();
}
});
}
}
you're doing this basic_panel.add(card_Layout_panel,BorderLayout.CENTER); twice, hence the error. ( check before and after the welcome_authentication_panel_card )

Swing, JLabel doesn't show up

I'm working on a little menu for a game. I already did the game itself, but I'm experiencing some problems with my menu. When I click on the buttons "Rules and Controls", "Options", and "About", the JLabels attached to them don't show up.
What am I doing wrong..?
Thanks in advance.
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class Menu extends JFrame
{
private static final long serialVersionUID = 1L;
public Menu()
{
// Creating a new JFrame and setting stuff
JFrame frame = new JFrame("BREAK THE BRICKS - MENU");
frame.setResizable(false);
frame.setBounds(43, 10, 1280, 720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//Creating a menu panel
JPanel menupanel = new JPanel();
menupanel.setLayout(null);
setContentPane(menupanel);
frame.add(menupanel);
//PRINCIPAL BUTTONS OF THE MENU
//Creating buttons
JButton buttonrules = new JButton();
JButton buttonoptions = new JButton();
JButton buttonabout = new JButton();
JButton buttonplay = new JButton("PLAY");
//Setting their bounds
buttonrules.setBounds(56, 224, 400, 83);
buttonoptions.setBounds(56, 302, 400, 82);
buttonabout.setBounds(56, 379, 400, 83);
buttonplay.setBounds(56, 486, 400, 110);
//Setting their border's color
buttonrules.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5));
buttonoptions.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5));
buttonabout.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5));
buttonplay.setBorder(BorderFactory.createLineBorder(Color.GRAY, 5));
//Setting their content and font
buttonrules.setContentAreaFilled(false);
buttonoptions.setContentAreaFilled(false);
buttonabout.setContentAreaFilled(false);
buttonplay.setFont(new Font("Mesquite Std", Font.PLAIN, 99));
//Adding them to the principal panel
menupanel.add(buttonrules);
menupanel.add(buttonoptions);
menupanel.add(buttonabout);
menupanel.add(buttonplay);
//BACKGROUND MENU'S IMAGE
//Attaching the principal background image to the principal panel
JLabel labelbackground = new JLabel();
menupanel.add(labelbackground);
labelbackground.setBounds(0, 0, 1280, 720);
Image background = new ImageIcon(this.getClass().getResource("/Menu_Principal.jpg")).getImage();
labelbackground.setIcon(new ImageIcon(background));
//BOXES ON THE RIGHT-HAND SIDE OF THE SCREEN
//RULES AND CONTROLS
//Creating a JLabel and setting stuff
JLabel labelboxrules = new JLabel();
labelboxrules.setForeground(Color.WHITE);
labelboxrules.setBounds(475, 159, 754, 500);
//Importing rules and controls' image and setting it to its label
Image rulandconimg = new ImageIcon(this.getClass().getResource("/Rules_And_Controls.jpg")).getImage();
labelboxrules.setIcon(new ImageIcon(rulandconimg));
//OPTIONS
//Creating a JLabel and setting stuff
JLabel labelboxoptions = new JLabel();
labelboxoptions.setForeground(Color.WHITE);
labelboxoptions.setBounds(475, 159, 754, 500);
//Importing options' image and setting it to its label
Image optionsimg = new ImageIcon(this.getClass().getResource("/Options.jpg")).getImage();
labelboxoptions.setIcon(new ImageIcon(optionsimg));
//ABOUT
//Creating a JLabel and setting stuff
JLabel labelboxabout = new JLabel();
labelboxabout.setForeground(Color.WHITE);
labelboxabout.setBounds(475, 159, 754, 500);
//Importing about's image and setting it to its label
Image aboutimg = new ImageIcon(this.getClass().getResource("/About.jpg")).getImage();
labelboxabout.setIcon(new ImageIcon(aboutimg));
//THEIR FUTURE BORDER
Border boxborder = BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5);
//ASSOCIATING ACTIONS WITH MENU'S BUTTONS
//Rules and Controls
buttonrules.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent a)
{
labelboxrules.setBorder(boxborder);
labelbackground.add(labelboxrules);
labelboxrules.setVisible(true);
}
});
//Options
buttonoptions.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent a)
{
labelboxoptions.setBorder(boxborder);
labelbackground.add(labelboxoptions);
labelboxoptions.setVisible(true);
}
});
//About
buttonabout.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent a)
{
labelboxabout.setBorder(boxborder);
labelbackground.add(labelboxabout);
labelboxabout.setVisible(true);
}
});
//Play
buttonplay.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent c)
{
Game.myGame();
}
});
}
}
you have to revalidate and repaint the panel after you add components .but as camicker and madprogrammer pointed out revalidate is pointless when not using layout managers.if you use layout managers then you have to call revalidate before repaint.
also by the default jlables are visible unlike jframes so calling labelboxoptions.setVisible(true); is redundant .
for example
buttonoptions.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
labelboxoptions.setBorder(boxborder);
labelbackground.add(labelboxoptions);
labelboxoptions.setVisible(true);
menupanel.repaint();
}
});
note:
don't use null layout. use layout managers. .
As suggested by Andrew and MadProgrammer
Dont use setLayout(null),
Updated and removed the not required statements

Categories

Resources