How to switch between two JPanels without CardLayout? - java

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!

Related

JFrame: navigate to another frame using JButton

I'm trying to navigate with buttons in Java Swing.
I want to go to another existing frame when i click on a button.
I will attach first the Menu Panel:
public class MenuPanel extends JFrame implements ActionListener {
JLabel l1,l2;
JButton btn1,btn2,btn3,btn4;
MenuPanel()
{
setVisible(true);
setSize(600, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Menu Panel For Final Project");
l1 = new JLabel("Menu Panel Bitches: ");
l2 = new JLabel("This Program Made By Ghetto K");
l1.setForeground(Color.RED);
l1.setFont(new Font("Arial", Font.BOLD, 28));
l2.setForeground(Color.RED);
l2.setFont(new Font("Arial", Font.ITALIC,22));
btn1 = new JButton("Add Child");
btn2 = new JButton("Add Worker");
btn3 = new JButton("Add Class");
btn4 = new JButton("Add Task");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
l1.setBounds(150, 30, 400, 30);
l2.setBounds(150, 80, 400, 30);
btn1.setBounds(0, 230, 120, 30);
btn2.setBounds(160, 230, 120, 30);
btn3.setBounds(465, 230, 120, 30);
btn4.setBounds(320, 230, 120, 30);
add(l1);
add(l2);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
}
public static void main(String[] args) {
new MenuPanel();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1)
{
}
else if(e.getSource()==btn2)
{
}
else if(e.getSource()==btn3)
{
}
}
I want to navigate to this panel:
public class AddWorkerPanel extends JFrame implements ActionListener {
static Connection con = DatabaseConnection.getConnection();
JLabel l1, l2, l3;
JTextField tf1, tf2;
JButton btn1, btn2;
AddWorkerPanel()
{
setVisible(true);
setSize(600, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Add Worker form in Java");
l1 = new JLabel("Add Worker Form - Type Details Below:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Worker-Name:");
l3 = new JLabel("Worker-Class-Number:");
tf1 = new JTextField();
tf2 = new JTextField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
btn1.addActionListener(this);
btn2.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
btn1.setBounds(80, 230, 200, 30);
btn2.setBounds(300, 230, 200, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(btn1);
add(btn2);
}
public static void main(String[] args) {
new AddWorkerPanel();
}
}
A JFrame is the actual frame that sits on the screen. So what you want to do is derive your MenuPanel and AddWorkerPanel classes from JPanel, so that they can both sit inside of one frame. Then, in your main frame class, you can have a CardLayout holding both of your panels, and have a button that cycles through the cards on the card layout, using the next() method in the card layout instance that you have stored.
If you just want to close one JFrame and then open the other you should be avble by just simply doing this in the calling method of your button:
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new AddWorkerPanel();
super.dispose();
}
}

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

Problems with having a "this" statement in my source

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

Moving method after JButton selection

When I select the first option and hit select the JFrame stays as is and I want it to close and move into the next method being called and open a different JFrame. Can anyone see the problem? I can't figure out where I am going wrong
public class GUI extends JFrame
{
public static void main(String args [])
{
final JFrame frame = new JFrame("Choose an option");
frame.setSize(350, 180);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0, 1, 0, 0));
final JRadioButton sb = new JRadioButton("Student");
final JRadioButton lb = new JRadioButton("Lecturer");
final JRadioButton cdb = new JRadioButton("Course Director");
final JRadioButton ab = new JRadioButton("Admin");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(sb);
buttonGroup.add(lb);
buttonGroup.add(cdb);
buttonGroup.add(ab);
JPanel panel = new JPanel();
panel.add(sb);
panel.add(lb);
panel.add(cdb);
panel.add(ab);
frame.getContentPane().add(panel);
panel.setLayout(new GridLayout(0, 1, 0, 0));
JButton select = new JButton("Select");
JButton cancel = new JButton("Cancel");
select.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(sb.isSelected())
{
frame.dispose();
StudentGUI();
}
else if(lb.isSelected())
System.out.println("Lecturer");
else if(cdb.isSelected())
System.out.println("Course Director");
else if(ab.isSelected())
System.out.println("Admin");
}
});
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
JPanel panel2 = new JPanel();
panel2.add(select);
panel2.add(cancel);
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
frame.getContentPane().add(panel2);
frame.setVisible(true);
}
public static void StudentGUI()
{
JFrame frame1 = new JFrame("Input Username");
frame1.setSize(350, 180);
frame1.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
JTextField tf = new JTextField("Input username here");
JButton submit = new JButton("Submit");
JPanel panel1 = new JPanel();
panel1.add(tf);
panel1.add(submit);
frame1.getContentPane().add(panel1);
}
You forgot to set frame1.setVisible(true); in your StudentGUI method, and you never close the window in the first method (use yourframe.dispose()).
So try:
public void actionPerformed(ActionEvent e)
{
if(sb.isSelected())
StudentGUI();
else if(lb.isSelected())
System.out.println("Lecturer");
else if(cdb.isSelected())
System.out.println("Course Director");
else if(ab.isSelected())
System.out.println("Admin");
yourframe.dispose();//don't know your frame variable
}
public static void StudentGUI()
{
JFrame frame1 = new JFrame("Input Username");
frame1.setSize(350, 180);
frame1.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
//code omitted
}

Calling JPanel from another JPanel

I know this is ill advised but I am using absolute layout and trying to go from panel to panel and was curious how to do this.
public class Mainscreen extends JFrame {
private JPanel Home;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Mainscreen frame = new Mainscreen();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Mainscreen() {
final Dataentrylog DEL = new Dataentrylog();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setBounds(100, 100, 618, 373);
Home = new JPanel();
Home.setBackground(new Color(255, 250, 250));
Home.setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setContentPane(Home);
Home.setLayout(null);
JButton btnNewButton = new JButton("Data Entry login");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnNewButton.setForeground(new Color(0, 0, 0));
btnNewButton.setBackground(UIManager.getColor("Menu.selectionBackground"));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Home.setVisible(false);
setContentPane(DEL);
setLayout(null);
}
});
btnNewButton.setBounds(44, 214, 204, 61);
Home.add(btnNewButton);
}
}
Calling this JPanel which works
import java.awt.EventQueue;
public class Dataentrylog extends JPanel {
public Dataentrylog() {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setLayout(null);
DEadmin DEA = new Deadmin();
final JButton btnSignIn = new JButton("Sign in");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}
While this works if I try an call another JPanel from Dataentry log the JFrame is blank. What can I do to call another JPanel? Any help would be great. Also, I know that layoutmanagers are the norm but for what I had to do I wasn't able to find anything that worked so I choose to use null despite my best judgement. THANKS!
Intialization of Dataentrylog should be like this
final Dataentrylog DEL = new Dataentrylog(this);
we have to pass the parent jframe.
public class Dataentrylog extends JPanel {
public Dataentrylog(final JFrame parent ) {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
final JButton btnSignIn = new JButton("Sign in");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
JPanel panel = new JPanel();
panel.add(new JButton("Solved"));
parent.setContentPane(panel);
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}

Categories

Resources