Problems with having a "this" statement in my source - java

so this is the current code and it has been so far working except for the background color, anything that has "//" before it is what causes errors between other things
package com.Luminate.luminatemedia;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Window extends JFrame implements ActionListener{
static JFrame mediaform = new JFrame();
public static void main(String[] args) {
}
public static void load() {
ImageIcon fileIcon = new ImageIcon ("assets/icon.png");
ImageIcon exitIcon = new ImageIcon ("assets/exit.png");
JButton exitButton = new JButton();
JLabel text = new JLabel("Luminate Media");
JPanel panel = new JPanel();
mediaform.add(panel);
panel.add(exitButton);
panel.add(text);
mediaform.setTitle("Luminate");
mediaform.setIconImage(fileIcon.getImage());
mediaform.setBounds(0, 688, 1366, 40);
//mediaform.setLayout(null);
mediaform.setUndecorated(true);
mediaform.setResizable(false);
mediaform.setVisible(true);
mediaform.setAlwaysOnTop(true);
//mediaform.getContentPane().setBackground(Color.DARK_GRAY);
mediaform.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text.setFont(new Font("dandelion in the spring", Font.PLAIN, 32));
text.setBounds(0, 0, 0, 0);
text.setHorizontalAlignment(SwingConstants.CENTER);
text.setSize(0, 0);
text.setForeground(Color.black);
text.setHorizontalAlignment(0);
exitButton.setBorder(null);
exitButton.setBounds(1326, 0, 40, 40);
exitButton.addActionListener(null);
exitButton.setIcon(exitIcon);
//BELOW IS MY ISSUE
exitButton.addActionListener(this);
panel.setLayout(null);
panel.setBackground(DARK_GRAY);
}
#Override
public void actionPerformed(ActionEvent e) {
//code to close the whole program
}
}
so there's an error with the emphasized text where it says "this"
also what would the code be to close the main class at the click of the button and am I doing this correctly ?

Try this
public class Window extends JFrame implements ActionListener{
public static void main(String[] args) {
new Window();
}
public Window() {
ImageIcon fileIcon = new ImageIcon ("assets/icon.png");
ImageIcon exitIcon = new ImageIcon ("assets/exit.png");
JButton exitButton = new JButton("Label");
JLabel text = new JLabel("Luminate Media");
JPanel panel = new JPanel();
setTitle("Luminate");
setIconImage(fileIcon.getImage());
setBounds(0, 688, 1366, 40);
setLayout(null);
setUndecorated(true);
setResizable(false);
setVisible(true);
setAlwaysOnTop(true);
getContentPane().setBackground(Color.DARK_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text.setFont(new Font("dandelion in the spring", Font.PLAIN, 32));
text.setBounds(0, 0, 0, 0);
text.setHorizontalAlignment(SwingConstants.CENTER);
text.setSize(0, 0);
text.setForeground(Color.black);
text.setHorizontalAlignment(0);
exitButton.setBorder(null);
exitButton.setBounds(1326, 0, 40, 40);
exitButton.setIcon(exitIcon);
exitButton.addActionListener(this);
panel.setLayout(null);
panel.setBackground(DARK_GRAY);
panel.add(exitButton);
panel.add(text);
add(panel);
}
#Override
public void actionPerformed(ActionEvent e) {
//code to close the whole program
}
}

Related

How to switch between two JPanels without CardLayout?

I build a game in swing and I have two JPanels for menu and the game itself. I want to switch from the menu panel to the game panel and from the game to the menu using JButtons. but every time I switch from the menu to the game, I want to start over the game and send the name of the player from some textField in the menu panel. so I need to make now instance of the game panel. How can I do this?
Here's the menu panel:
public class MenuPanel extends JPanel {
private JPanel contentPane;
private JTextField txtName;
public MenuPanel(JPanel panel) {
setLayout(null);
contentPane = panel;
JButton btnStart = new JButton("Click to start the game");
btnStart.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.show(contentPane, "Game Panel");
}
});
btnStart.setFont(new Font("Baskerville Old Face", btnStart.getFont().getStyle(), 25));
btnStart.setBounds(290, 510, 290, 70);
add(btnStart);
txtName = new JTextField();
txtName.setBounds(450, 460, 160, 20);
add(txtName);
txtName.setColumns(10);
JLabel lblName = new JLabel("Enter your name:");
lblName.setForeground(SystemColor.text);
lblName.setFont(new Font("Bradley Hand ITC", Font.BOLD, 20));
lblName.setBounds(260, 460, 180, 20);
add(lblName);
JLabel lblGame = new JLabel("RUMMIKUB GAME");
lblGame.setForeground(new Color(0, 0, 0));
lblGame.setHorizontalAlignment(SwingConstants.CENTER);
lblGame.setFont(new Font("Goudy Old Style", lblGame.getFont().getStyle(), 50));
lblGame.setBounds(180, 100, 500, 140);
add(lblGame);
}
}
Here's the game panel:
public class GamePanel extends JPanel {
private JPanel contentPane;
private JButton btnReturnToMenu;
private TileButton[][] buttons = new TileButton[Constants.ROWS_IN_PLAYER_BOARD][Constants.COLUMNS_IN_PLAYER_BOARD];
private JLabel lblHello;
private TileButton btnThrownByPlayer;
private TileButton btnThrownByOpponent;
private Point clickedButton;
private JButton btnPullFromPool;
private JButton btnFinish;
private Game game;
public GamePanel(String firstPlayerName, String secondPlayerName, JPanel panel) {
setLayout(null);
contentPane = panel;
clickedButton = null;
game = new Game(firstPlayerName, secondPlayerName);
lblHello = new JLabel("hello " + firstPlayerName + " and " + secondPlayerName);
lblHello.setBounds(10, 10, 250, 40);
add(lblHello);
//initiate exit button.
btnReturnToMenu = new JButton("Return To Menu");
btnReturnToMenu.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.show(contentPane, "Menu Panel");
}
});
btnReturnToMenu.setFont(new Font("Mongolian Baiti", Font.BOLD, 16));
btnReturnToMenu.setBounds(10, 70, 200, 40);
add(btnReturnToMenu);
// initiate finish button.
btnFinish = new JButton("Finish");
btnFinish.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (btnFinish.isEnabled()) {
if (game.finish()) {
JOptionPane.showMessageDialog(null,
"You finish the game.\nCongratulations, you are the winner!!!");
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.show(contentPane, "Finish Panel");
} else {
btnFinish.setEnabled(false);
JOptionPane.showMessageDialog(null, "You didn't finish the game yet");
}
}
}
});
btnFinish.setFont(new Font("Mongolian Baiti", Font.BOLD, 16));
btnFinish.setBounds(750, 50, 100, 40);
add(btnFinish);
}
}
Here's the frame:
public class MainFrame extends JFrame {
private JPanel contentPane;
private GamePanel gamePanel;
private MenuPanel menuPanel;
private FinishPanel finishPanel;
private MainFrame() {
//super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(233, 55, 900, 627);
setResizable(false);
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
contentPane.setLayout(new CardLayout());
menuPanel = new MenuPanel(contentPane);
gamePanel = new GamePanel("", "", contentPane);
finishPanel = new FinishPanel(contentPane);
contentPane.add(menuPanel, "Menu Panel");
contentPane.add(gamePanel, "Game Panel");
contentPane.add(finishPanel, "Finish Panel");
getContentPane().add(contentPane, BorderLayout.CENTER);
pack();
setLocationByPlatform(true);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainFrame();
}
});
}
}
Thanks for the help!

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

Position Not Moving Despite of Key Listener

I made the Card Layout Working completely fine but what happened now is I added a keylistener to the object character which is a JLabel so whenever the person presses up the character should move up but it does absolutely nothing!
I also tried replacing it with a button which moves it when clicked and it worked completely fine. Also I tried changing the event meaning I changed that if they press up then the image of the town map would change but still no effect so it seems there is something wrong with my KeyListener
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.*;
import javax.swing.*;
#SuppressWarnings({ "unused", "serial" })
public class FinalBlowzXC extends JFrame implements KeyListener{
public static JPanel game=new JPanel();
public static JPanel mainmenu=new JPanel(null);
public static JPanel loginpanel=new JPanel(null);
public static JPanel tutorial=new JPanel(null);
public static JPanel registration=new JPanel(null);
public static JPanel town_map=new JPanel(null);
public JTextField username= new JTextField("Username");
public JTextField password=new JTextField("Password");
public JLabel bglogin=new JLabel();
public JLabel character=new JLabel();
public JButton log_in=new JButton();
public JButton register=new JButton();
CardLayout page= new CardLayout();
public String level="";
public int charx=350;
public int chary=435;
public static void main(String []args)
{
new FinalBlowzXC().setVisible(true);
}
public FinalBlowzXC()
{
super("Final Blowz Xchanged");
setSize(640,510);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
game.setLayout(page);
game.add(mainmenu, "1");
game.add(loginpanel, "2");
game.add(tutorial, "3");
game.add(registration, "4");
game.add(town_map, "5");
add(game);
opening();
}
public void opening()
{
page.show(game, "1");
JLabel bgmainmenu;
JButton start;
JButton exit;
bgmainmenu = new JLabel();
start = new JButton();
exit = new JButton();
bgmainmenu.setIcon(new ImageIcon(getClass().getResource("/FF-XV.jpg")));
bgmainmenu.setBounds(0,0,640,480);
mainmenu.add(bgmainmenu);
mainmenu.add(start);
start.setBounds(280, 360, 70, 20);
start.setBorder(null);
start.setBorderPainted(false);
start.setContentAreaFilled(false);
start.setOpaque(false);
start.addActionListener(new Start());
exit.setBounds(280, 385, 70, 20);
mainmenu.add(exit);
exit.setBorder(null);
exit.setBorderPainted(false);
exit.setContentAreaFilled(false);
exit.setOpaque(false);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
class Start implements ActionListener{
public void actionPerformed(ActionEvent e) {
login();
}
}
public void login()
{
page.show(game, "2");
bglogin.setIcon(new ImageIcon(getClass().getResource("/FF-XV Login.jpg")));
bglogin.setBounds(0, 0, 640, 480);
username.setBounds(300, 285, 85, 15);
username.setBorder(null);
username.setForeground(Color.WHITE);
username.setOpaque(false);
password.setBounds(300, 310, 85, 20);
password.setBorder(null);
password.setForeground(Color.WHITE);
password.setOpaque(false);
log_in.setBounds(280, 335, 50, 45);
log_in.setBorder(null);
log_in.setBorderPainted(false);
log_in.setContentAreaFilled(false);
log_in.setOpaque(false);
log_in.addActionListener(new Log_in());
register.setBounds(335, 335, 55, 45);
register.setBorder(null);
register.setBorderPainted(false);
register.setContentAreaFilled(false);
register.setOpaque(false);
register.addActionListener(new Register());
loginpanel.add(username);
loginpanel.add(password);
loginpanel.add(bglogin);
loginpanel.add(log_in);
loginpanel.add(register);
}
class Log_in implements ActionListener{
public void actionPerformed(ActionEvent e)
{
Tutorial();
}
}
class Register implements ActionListener{
public void actionPerformed(ActionEvent e)
{
page.show(game, "4");
}
}
public void Tutorial()
{
page.show(game, "3");
JLabel bgtutorial=new JLabel();
JLabel animeforward=new JLabel();
JLabel animeright=new JLabel();
JLabel animeleft=new JLabel();
JButton next=new JButton();
JLabel animebackward=new JLabel();
bgtutorial.setIcon(new ImageIcon(getClass().getResource("/FF-XV Tutorial.jpg")));
bgtutorial.setBounds(0, 0, 640, 480);
animeforward.setIcon(new ImageIcon(getClass().getResource("walkanimofficialfront.gif")));
animeforward.setBounds(115, 230, 30, 30);
animeright.setIcon(new ImageIcon(getClass().getResource("walkanimofficialright.gif")));
animeright.setBounds(190, 315, 30, 30);
animeleft.setIcon(new ImageIcon(getClass().getResource("walkanimofficialleft.gif")));
animeleft.setBounds(45, 315, 30, 30);
animebackward.setIcon(new ImageIcon(getClass().getResource("walkanimofficialback.gif")));
animebackward.setBounds(115, 405, 30, 30);
next.setBounds(530, 430, 100, 30);
next.setBorder(null);
next.setBorderPainted(false);
next.setContentAreaFilled(false);
next.setOpaque(false);
next.addActionListener(new Next());
tutorial.add(next);
tutorial.add(animeright);
tutorial.add(animeleft);
tutorial.add(animebackward);
tutorial.add(animeforward);
tutorial.add(bgtutorial);
}
class Next implements ActionListener{
public void actionPerformed(ActionEvent e)
{
town();
}
}
public void town()
{
page.show(game, "5");
JLabel map=new JLabel();
map.setIcon(new ImageIcon(getClass().getResource("/FF-XV Town.jpg")));
map.setBounds(0, 0, 640, 480);
character.setIcon(new ImageIcon(getClass().getResource("standfront.png")));
character.setBounds(charx, chary, 30, 35);
town_map.add(character);
town_map.add(map);
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP)
{
character.setIcon(new ImageIcon(getClass().getResource("walkanimofficialfront.gif")));
character.setLocation(character.getX(), character.getY()+5);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
character.setIcon(new ImageIcon(getClass().getResource("walkanimofficialright.gif")));
character.setLocation(character.getX()+5, character.getY());
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
character.setIcon(new ImageIcon(getClass().getResource("walkanimofficialleft.gif")));
character.setLocation(character.getX()-5, character.getY()+5);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
character.setIcon(new ImageIcon(getClass().getResource("walkanimofficialback.gif")));
character.setLocation(character.getX(), character.getY()-5);
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
Add following code in your programme.
public static JPanel mainmenu = new JPanel();
public static JPanel loginpanel = new JPanel();
or initialize mainmenu & loginpanel before adding them into the game panel.
when you call game.add(loginPanel, "2") loginPanel is null

How to use CardLayout with multiple JButtons?

I need to make a GUI that asks the users for details and then save them in a linked list. I wanted to use the CardLayout to switch from one frame to another, which is something I'm doing for the first time. I have done probably less half of what I need to do and here I am quite lost in this part. The code below compiles and executes but when I click the buttons, the desired change does not happen . What could be wrong?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDatabaseWindow extends JPanel{
public static final String FRONT_PAGE = "Front Page";
public static final String BROWSE_MEMORIES = "Browse Memories";
public static final String ADD_EDIT = "Add Edit";
public static final String VIEW_MEMORY = "View Memory";
public static void createAndShowGUI() {
final MyDatabaseWindow mdbw = new MyDatabaseWindow();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
final JButton addButton = new JButton("Add");
final JButton editButton = new JButton("Edit");
final JButton deleteButton = new JButton("Delete");
final JButton browseButton = new JButton("Browse");
final JButton searchButton = new JButton("Search");
addButton.setBounds(100, 400, 100, 100);
editButton.setBounds(200, 400, 100, 100);
deleteButton.setBounds(300, 400, 100, 100);
browseButton.setBounds(400, 400, 100, 100);
searchButton.setBounds(500, 400, 100, 100);
buttonPanel.add(addButton);
buttonPanel.add(editButton);
buttonPanel.add(deleteButton);
buttonPanel.add(browseButton);
buttonPanel.add(searchButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToAddPage();
}
});
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToSearch();
}
});
JFrame frame = new JFrame("Memory Files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 540);
frame.setLocation(250, 100);
frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private CardLayout cardLayout = new CardLayout();
private JPanel cardShowingPanel = new JPanel(cardLayout);
public MyDatabaseWindow() {
Window1 win1 = new Window1();
cardShowingPanel.add(win1, FRONT_PAGE);
Window2 win2 = new Window2();
cardShowingPanel.add(win2, BROWSE_MEMORIES);
Window3 win3 = new Window3();
cardShowingPanel.add(win3, ADD_EDIT);
Window4 win4 = new Window4();
cardShowingPanel.add(win4, VIEW_MEMORY);
setLayout(new BorderLayout());
add(cardShowingPanel, BorderLayout.NORTH);
}
public void goToAddPage() {
cardLayout.first(cardShowingPanel);
}
public void goToBrowse() {
cardLayout.first(cardShowingPanel);
cardLayout.next(cardShowingPanel);
}
public void goToSearch() {
cardLayout.last(cardShowingPanel);
}
public void showCard(String key) {
cardLayout.show(cardShowingPanel, key);
}
}
class Window1 extends JPanel {
public Window1() {
init();
}
private void init() { //dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window2 extends JPanel {
public Window2() {
init();
}
private void init() { //dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window3 extends JPanel {
public Window3() {
init();
}
private void init() {//dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window4 extends JPanel {
public Window4() {
init();
}
private void init() {//dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
The main problem is the use of null-layouts and these lines of code:
frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);
First you add the panel using the CardLayout to BorderLayout.CENTER, then you "overlay" it with your buttonPanel, which is using null-layout.
I would go with a simple FlowLayout (the default layout-manager for a JPanel) for the buttonPanel and add it to the BorderLayout.SOUTH of the contentPane. I would also strongly recommend reading this tutorial.
So remove the following lines of code:
buttonPanel.setLayout(null);
...
addButton.setBounds(100, 400, 100, 100);
editButton.setBounds(200, 400, 100, 100);
deleteButton.setBounds(300, 400, 100, 100);
browseButton.setBounds(400, 400, 100, 100);
searchButton.setBounds(500, 400, 100, 100);
and change frame.getContentPane().add(buttonPanel); to frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);.
Also forget about the null-layout / setBounds() in your Window-classes.
(Note that you still won't see the text change if you press a button because you always add a JLabel with the same text ("Memory Files") to your Windows.)

Blank Frame for no apperent reason [duplicate]

This question already has answers here:
Java null layout results in a blank screen
(2 answers)
Closed 8 years ago.
Program is Compiling and running but it is not displaying anything on the frame just a blank screen.
i am making main menu for my game using cardLayout, i have made different classes for every panel and then added them in cardlayout in Frame
Kindly some one please pinpoint the mistake i am doing i cant find it..
thankyou
enter code here
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class MainMenu extends JFrame implements ActionListener{
static CardLayout cl2;
static JPanel cardPanel;
Name p1;
MainM p2;
Robot p3;
High p4;
Inst p5;
gamepanel gp;
public static int height;
public static int width;
public static void main(String args[]){
MainMenu cl = new MainMenu();
cl.What();
}
public void What() {
// get the screen size as a java dimension
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
setTitle("EROBOT");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// get height, and width
height = screenSize.height ;
width = screenSize.width ;
// set the jframe height and width
setSize(new Dimension(width, height));
cardPanel = new JPanel();
p1= new Name();
p2= new MainM();
p3= new Robot();
p4= new High();
p5= new Inst();
gp= new gamepanel();
cl2 = new CardLayout();
cardPanel.setLayout(cl2);
cardPanel.add(p1, "name");
cardPanel.add(p2, "menu");
cardPanel.add(p3, "robo");
cardPanel.add(p4, "inst");
cardPanel.add(p5, "high");
cardPanel.add(gp, "start");
getContentPane().add(cardPanel);
//name
Name.done.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl2.show(cardPanel, "menu" );
}
});
// Main
MainM.newg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl2.show(cardPanel, "start" );
}
});
MainM.high.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl2.show(cardPanel, "high" );
}
});
MainM.robo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl2.show(cardPanel, "robo" );
}
});
MainM.inst.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl2.show(cardPanel, "inst" );
}
});
/////////////////////
//Robot
Robot.go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl2.show(cardPanel, "start" );
}
});
Robot.back1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl2.show(cardPanel, "menu" );
}
});
//high score
High.back2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl2.show(cardPanel, "menu" );
}
});
//how to play
Inst.back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl2.show(cardPanel, "menu" );
}
});
}
public void actionPerformed(ActionEvent e){}
}
class Name extends JPanel{
JLabel enter;
static JButton done;
JTextField name;
JPanel side;
JPanel up;
JLabel title;
Name(){
Color h3= new Color(255,229,204);
side= new JPanel();
up= new JPanel();
setLayout(null);
MainMenu mm= new MainMenu();
setBackground(h3);
title = new JLabel("E-ROBOT");
title.setFont(new Font("Ravie", Font.PLAIN, 40)); //setting the style and the font of JLabel
title.setBounds(520, 100, 300, 50);
enter= new JLabel("ENTER YOUR NAME HERE");
enter.setFont(new Font("Ravie", Font.PLAIN, 20));
enter.setForeground(Color.orange);
enter.setBounds(370, 200, 350, 50);
name = new JTextField();
name.setBounds(390, 250, 150, 25);
done= new JButton("DONE");
done.setFont(new Font("Ravie", Font.PLAIN, 20));
done.setForeground(Color.orange);
done.setBackground(Color.YELLOW);
done.setOpaque(false);
done.setBorder(null);
done.setBounds(400, 280, 150, 30);
side.setBounds(0,40,mm.width,60);
up.setBounds(40,0,40,mm.height);
side.setBackground(Color.ORANGE);
up.setBackground(Color.ORANGE);
side.add(title);
add(enter);
add(name);
add(done);
add(side);
add(up);
}
}
class MainM extends JPanel{
JPanel l1;
JPanel l2;
JPanel l3;
JPanel l4;
JPanel contain;
JPanel side1;
JPanel up1;
static JButton newg;
static JButton high;
static JButton robo;
static JButton inst;
JLabel title;
MainM(){
MainMenu mm= new MainMenu();
Color h3= new Color(255,229,204);
setBackground(h3);
l1= new JPanel();
l2= new JPanel();
l3= new JPanel();
l4= new JPanel();
contain= new JPanel();
side1= new JPanel();
up1= new JPanel();
title = new JLabel("E-ROBOT");
title.setFont(new Font("Ravie", Font.PLAIN, 40));
title.setForeground(Color.WHITE);
title.setBounds(520, 100, 300, 50);
//custom colors
Color b1= new Color(99,230,199);
Color h= new Color(132,234,23);
Color h1= new Color(45,99,120);
Color h2= new Color(0,0,101);
Color h4= new Color(154,0,0);
l1.setBounds(480,200,10,525);
l1.setBackground(h4);
l2.setBounds(465,210,565,10);
l2.setBackground(h4);
l3.setBounds(1000,200,10,525);
l3.setBackground(h4);
l4.setBounds(465,690,565,10);
l4.setBackground(h4);
// settings style amd font of buttons
newg = new JButton("New Game");
newg.setFont(new Font("Ravie", Font.PLAIN, 25));
newg.setBackground(Color.YELLOW);
newg.setOpaque(false);
newg.setBorder(null);
newg.setForeground(h2);
high = new JButton("High Scores");
robo= new JButton("Select your Robot");
inst = new JButton("How to Play");
high.setFont(new Font("Ravie", Font.PLAIN, 25));
high.setBackground(Color.YELLOW);
high.setOpaque(false);
high.setBorder(null);
high.setForeground(h1);
robo.setFont(new Font("Ravie", Font.PLAIN, 25));
robo.setBackground(Color.YELLOW);
robo.setOpaque(false);
robo.setBorder(null);
robo.setForeground(h);
inst.setFont(new Font("Ravie", Font.PLAIN, 25));
inst.setBackground(Color.YELLOW);
inst.setOpaque(false);
inst.setBorder(null);
inst.setForeground(Color.MAGENTA);
contain.setBounds(490,220,490,460);
contain.setLayout(new GridLayout(4,1));
side1.setBounds(0,40,mm.width,60);
up1.setBounds(40,0,40,mm.height);
side1.setBackground(h4);
up1.setBackground(h4);
//adding all the JComponents to the screen
side1.add(title);
contain.add(newg);
contain.add(high);
contain.add(robo);
contain.add(inst);
add(l1);
add(l2);
add(l3);
add(l4);
add(side1);
add(up1);
add(contain);
}
}
class Robot extends JPanel{
ImageIcon a;
ImageIcon b;
ImageIcon c;
JPanel side2;
JPanel up2;
static JButton r1;
static JButton r2;
static JButton r3;
static JButton back1;
static JButton go;
JLabel title1;
Robot(){
Color h= new Color(132,234,23);
MainMenu mm= new MainMenu();
Color h3= new Color(255,229,204);
Color h4= new Color(154,0,0);
side2= new JPanel();
up2= new JPanel();
r1= new JButton();
r2= new JButton();
r3= new JButton();
back1= new JButton("Back");
go= new JButton("LEt's go");
a = new ImageIcon(getClass().getResource("a.gif"));
b = new ImageIcon(getClass().getResource("b.gif"));
c = new ImageIcon(getClass().getResource("c.gif"));
//adding the ImageIcons to the JButtons
r1 = new JButton(a);
r1.setBounds(120,120,300,300);
r1.setBackground(Color.YELLOW);
r1.setOpaque(false);
r1.setBorder(null);
r2 = new JButton(b);
r2.setBounds(460,120,300,300);
r2.setBackground(Color.YELLOW);
r2.setOpaque(false);
r2.setBorder(null);
r3 = new JButton(c);
r3.setBounds(890,120,300,300);
r3.setBackground(Color.YELLOW);
r3.setOpaque(false);
r3.setBorder(null);
back1 = new JButton("Let's Go!");
back1.setBounds(520, 500, 170, 60);
back1.setFont(new Font("Ravie", Font.PLAIN, 25));
back1.setBackground(Color.YELLOW);
back1.setOpaque(false);
back1.setBorder(null);
setLayout(null);
title1 = new JLabel("E-ROBOT");
title1.setFont(new Font("Ravie", Font.PLAIN, 40)); //setting the style and the font of JLabel
// title1.setBounds(520, 100, 300, 50);
side2.setBounds(0,40,mm.width,60);
up2.setBounds(40,0,40,mm.height);
side2.setBackground(h);
up2.setBackground(h);
side2.add(title1);
add(side2);
add(up2);
add(r1);
add(r2);
add(r3);
add(back1);
add(go);
}
}
class High extends JPanel{
JLabel title3;
static JButton back2;
JPanel side4;
JPanel up4;
High()
{
Color h= new Color(132,234,23);
MainMenu mm= new MainMenu();
Color h3= new Color(255,229,204);
side4 = new JPanel();
up4= new JPanel();
Color h1= new Color(45,99,120);
setLayout(null);
title3 = new JLabel("E-ROBOT");
title3.setFont(new Font("Ravie", Font.PLAIN, 40)); //setting the style and the font of JLabel
title3.setBounds(520, 100, 300, 50);
back2= new JButton("Back");
back2.setBounds(500,500,120,30);
back2.setFont(new Font("Ravie", Font.PLAIN, 25));
back2.setBackground(Color.YELLOW);
back2.setOpaque(false);
back2.setBorder(null);
side4.setBounds(0,40,mm.width,60);
up4.setBounds(40,0,40,mm.height);
side4.setBackground(h1);
up4.setBackground(h1);
side4.add(title3);
add(side4);
add(up4);
add(back2);
}
}
class Inst extends JPanel{
JLabel jl1;
JLabel jl2;
JLabel jl3;
JLabel jl4;
JLabel jl5;
JLabel title2;
JPanel side3;
JPanel up3;
static JButton back;
JPanel contain2;
Inst(){
MainMenu mm= new MainMenu();
contain2= new JPanel();
side3= new JPanel();
up3= new JPanel();
Color h= new Color(132,234,23);
Color h3= new Color(255,229,204);
jl1= new JLabel("Welcome aboard\n !");
jl2 = new JLabel("So the game is simple.\n");
jl3 = new JLabel("You have to catch even number only\n.");
jl4 = new JLabel("A life ends if you catch an odd number.\n");
jl5 = new JLabel("Oh and also the game gets more difficult when your lives end!\n");
jl1.setFont(new Font("Ravie", Font.PLAIN, 20));
jl2.setFont(new Font("Ravie", Font.PLAIN, 20));
jl3.setFont(new Font("Ravie", Font.PLAIN, 20));
jl4.setFont(new Font("Ravie", Font.PLAIN, 20));
jl5.setFont(new Font("Ravie", Font.PLAIN, 20));
jl1.setForeground(Color.magenta);
jl2.setForeground(Color.magenta);
jl3.setForeground(Color.magenta);
jl4.setForeground(Color.magenta);
jl5.setForeground(Color.magenta);
title2 = new JLabel("E-ROBOT");
title2.setFont(new Font("Ravie", Font.PLAIN, 40)); //setting the style and the font of JLabel
title2.setBounds(520, 100, 300, 50);
back = new JButton("Back");
back.setFont(new Font("Ravie", Font.PLAIN, 15));
back.setBackground(Color.YELLOW);
back.setOpaque(false);
back.setBorder(null);
setLayout(null);
contain2.setBounds(80,200,800,400);
contain2.setBackground(h3);
side3.setBounds(0,40,mm.width,60);
up3.setBounds(40,0,40,mm.height);
side3.setBackground(Color.MAGENTA);
up3.setBackground(Color.MAGENTA);
side3.add(title2);
contain2.add(jl1);
contain2.add(jl2);
contain2.add(jl3);
contain2.add(jl4);
contain2.add(jl5);
contain2.add(back);
add(contain2);
add(side3);
add(up3);
}
}
I'm not sure whether it helps (sorry but your code is too large and bad formatted, so I cannot understand it), but you can try to revalidate and repaint the root pane or content pane of JFrame each time when you update your GUI.
Something like this
public static void updateRootPane(JComponent aComponent) {
Window w = SwingUtilities.windowForComponent(aComponent);
if (w instanceof JFrame) {
((JFrame) w).getRootPane().revalidate();
((JFrame) w).getRootPane().repaint();
} else if (w instanceof JDialog) {
((JDialog) w).getRootPane().revalidate();
((JDialog) w).getRootPane().repaint();
}
}

Categories

Resources