Java: Clear JFrame and load new buttons - java

So, i have my main screen showing and i would like it so that when the user clicks "Login" is loads another screen. I have my second screen in another function called "fixtureLists". When i call this function it just overlays the buttons ontop of the login screen. How would i get it so that the screen is cleared and then the fixtureLists screen loads?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Main extends JFrame
{
public Main()
{
loginButton = new JButton("Login");
loginButton.setBounds( 125, 300, 100, 35);
add(loginButton);
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
fixtureList();
System.out.println("Loading the fixtures screen");
}
});
}
public void fixtureList()
{
JButton editButton;
JButton createButton;
JCheckBox chkBox;
setLayout(null);
editButton = new JButton("Edit");
editButton.setBounds( 10, 10, 100, 35);
add(editButton);
createButton = new JButton("Create");
createButton.setBounds( 140, 10, 100, 35);
add(createButton);
editButton = new JButton("Edit");
createButton.setBounds( 10, 30, 100, 35);
}
public static void main(String args[])
{
Main window = new Main();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(250, 430);
window.getContentPane().setBackground(new Color(53, 56, 64));
window.setVisible(true);
window.setTitle("PE Timetable v1.0");
}
}

I would not define the login dialog in the main frame.
Your login dialog should pop up in a dialog over the frame.
Having said that, if you want the login on the same frame, then use Cardlayout. On one Cardlayout have your login stuff, then when you login, switch the cardlayout to another panel.

Related

Why doesn't this button appear?

Hi I have created this button in a Java program and for some reason the button doesn't appear. (This button is part of a larger program and there are more buttons and they are put in the exact same way and they do appear, I have took them out just to make it easier to read the code).
import javax.swing.*;
import java.awt.event.*;
public class GUI extends JFrame {
public GUI() {
JButton btnNewButton = new JButton("Button");
add(btnNewButton);
btnNewButton.setBounds(518, 272, 216, 45);
}
public static void main(String[] args) {
GUI menu = new GUI();
menu.setVisible(true);
menu.setTitle("GUI");
menu.setBounds(0, 0, 780, 500);
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu.setLayout(null);
}
}
Use your setVibile after you have done everything else
import javax.swing.*;
import java.awt.event.*;
public class GUI extends JFrame {
public GUI() {
JButton btnNewButton = new JButton("Button");
add(btnNewButton);
btnNewButton.setBounds(518, 272, 216, 45);
}
public static void main(String[] args) {
GUI menu = new GUI();
menu.setTitle("GUI");
menu.setBounds(0, 0, 780, 500);
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu.setLayout(null);
menu.setVisible(true);
}
}
You forgot to pack() the button to your JFrame. Try this:
public GUI() {
JButton btnNewButton = new JButton("Button");
add(btnNewButton);
btnNewButton.setBounds(518, 272, 216, 45);
pack();
}

Set Layout of panel within CardLayout

I am trying to create a UI for an imaginary vehicle that has both Automatic and Manual modes. When the user sets the vehicle into one of the modes, it should only display the controls relevant to that mode, and I've accomplished this using a CardLayout.
However, I'd also like to be able to specify the location of the various elements of the layout for each card manually - for a static layout I'd do something along the lines of mainPanel.setLayout(null), but this simply gives a blank window when used on a CardLayout (hence the two commented-out lines in the code below).
How would I achieve both of these things? My current code is below:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UI extends JFrame implements ActionListener{
public UI() {
initUI();
}
private JPanel cardPanel;
private CardLayout cardLayout = new CardLayout();
public final void initUI() {
cardPanel = new JPanel();
cardPanel.setLayout(cardLayout);
JPanel manualPanel = new JPanel();
getContentPane().add(manualPanel);
//manualPanel.setLayout(null);
cardPanel.add(manualPanel, "manual");
JPanel autoPanel = new JPanel();
//autoPanel.setLayout(null);
cardPanel.add(autoPanel, "auto");
JButton startButton = new JButton("START/STOP");
startButton.setBounds(50, 150, 200, 50);
startButton.addActionListener(new startListener());
manualPanel.add(startButton);
autoPanel.add(startButton);
JButton autoButton = new JButton("SWITCH TO AUTO");
autoButton.setBounds(50, 250, 200, 50);
autoButton.addActionListener(new autoListener());
manualPanel.add(autoButton);
JButton upButton = new JButton("^");
upButton.setBounds(125, 320, 50, 50);
upButton.addActionListener(new returnListener());
manualPanel.add(upButton);
JButton downButton = new JButton("\\/");
downButton.setBounds(125, 380, 50, 50);
downButton.addActionListener(new returnListener());
manualPanel.add(downButton);
JButton ccwButton = new JButton("<-");
ccwButton.setBounds(55, 350, 50, 50);
ccwButton.addActionListener(new returnListener());
manualPanel.add(ccwButton);
JButton cwButton = new JButton("->");
cwButton.setBounds(195, 350, 50, 50);
cwButton.addActionListener(new returnListener());
manualPanel.add(cwButton);
JButton ngzButton = new JButton("SOMETHING ELSE");
ngzButton.setBounds(50, 450, 200, 50);
ngzButton.addActionListener(new returnListener());
manualPanel.add(ngzButton);
JButton manualButton = new JButton("SWITCH TO MANUAL");
manualButton.setBounds(50, 250, 200, 50);
manualButton.addActionListener(new manualListener());
autoPanel.add(manualButton);
JButton returnButton = new JButton("SOMETHING ELSE");
returnButton.setBounds(50, 350, 200, 50);
returnButton.addActionListener(new returnListener());
autoPanel.add(returnButton);
setTitle("UI");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(cardPanel, BorderLayout.NORTH);
}
public static void main(String[] args) {
UI ui = new UI();
ui.setVisible(true);
}
public void actionPerformed(ActionEvent e){
}
private class returnListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
}
}
private class autoListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
cardLayout.show(cardPanel, "auto");
}
}
private class startListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
}
}
private class manualListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
cardLayout.show(cardPanel, "manual");
}
}
}
In your example, you create a startButton, but you then attempt to add the same instance to two different panels. Because a component can only occupy one container, you'll need to create two buttons, one for each panel.
As an aside, instead of using a null layout, give each panel BorderLayout, add the buttons to a JPanel having the default FlowLayout, and add the button panel to the SOUTH. You can then nest your illustrations in the CENTER using whatever layout is appropriate.
Addendum: As #Frakcool comments, using a layout will improve the cross-platform appearance of your buttons. Invoke pack() on the enclosing window, and override getPreferredSize() on the nested illustration panel to give it the needed size. In this related example, the CENTER panel is used for drawing only; having no components, its layout then becomes irrelevant.

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

Close the current window and open the previous window?

I am a beginner to java and stuck with the below issue.
The purpose is to display the login window when log out button is pressed.
First JFrame window displayed is "Plain" with 2 fields username and password(I will be adding the log in functionality later)
When I press the submit button the JFrame "NEw Window" is displayed with the button "LOGOUT"
What I would like to do is that when the "LOGOUT" is pressed the "NEw Window" should close and the "Plain" window should open.
Present issue: When "LOGOUT" button is pressed the "NEw Window" is opening up.
Please correct the code so that I get the desired functionality
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class Test implements ActionListener{
JButton submit;
JFrame j;
JFrame jf;
public Test()
{
j = new JFrame("PLAIN");
j.setBounds(500,150,300,400);
j.setVisible(true);
j.setDefaultCloseOperation(j.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
j.add(panel);
panel.setSize(50, 50);
panel.setLayout(null);
JLabel label = new JLabel("User Name");
label.setSize(10,10);
label.setBounds(100, 30, 400, 30);
panel.add(label);
JTextField username = new JTextField(10);
username.setSize(10,10);
username.setBounds(300, 30, 400, 30);
panel.add(username);
JLabel password= new JLabel("Password");
password.setBounds(100, 90, 400, 30);
panel.add(password);
JPasswordField pass = new JPasswordField(10);
pass.setBounds(300, 90, 400, 30);
panel.add(pass);
submit = new JButton("Submit");
submit.setSize(10, 10);
submit.setBounds(300, 160, 200, 40);
panel.add(submit);
submit.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
j.setVisible(false);
jf = new JFrame("NEw Window");
jf.setVisible(true);
jf.setBounds(500,150,300,400);
JPanel panel2 = new JPanel();
panel2.setLayout(null);
jf.add(panel2);
JButton logout = new JButton("LOGOUT");
logout.setBounds(100, 30, 400, 30);
panel2.add(logout);
logout.addActionListener(this);
jf.setDefaultCloseOperation(j.EXIT_ON_CLOSE);
}
public void actionPerformed1(ActionEvent e1) {
jf.dispose();
j.setVisible(true);
}
public static void main(String args[])
{
new Test();
}
}
Some Points:
Call JFrame#setVisible() in the end after adding all the components.
Never use null layout at all instead use proper Layout Manager.
Read more A Visual Guide to Layout Managers where all the layout manger are described in detail along with sample code.
Use SwingUtilities.invokeLater() to make sure that EDT is initialized properly.
Read more
Why to use SwingUtilities.invokeLater in main method?
SwingUtilities.invokeLater
Try with WindowConstants.DISPOSE_ON_CLOSE instead of JFrame.EXIT_ON_CLOSE.
Just put j.setVisible(true) from Test() at the end, after adding all the components.
If you want to make a new form, don't do it in the same class where you already have one because it isn`t right.
Read the book named Clean code. You also have some errors in your code and useless.

Java: Button isn't showing up

So, i'm using Java and JFrame and i would like the end user to be able to click the button "Login" and it should display another saying "Edit". For some odd reason when the end-user clicks "Login" it doesn't show the "Edit" box but it displays the "System.out.println"
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
public void fixtureList()
{
JButton editButton;
editButton = new JButton("Edit");
editButton.setBounds(100, 200, 100, 100);
add(editButton);
}
public void loginPanel()
{
setLayout(null);
JButton loginButton;
loginButton = new JButton("Login");
loginButton.setBounds(10, 10, 100, 100);
add(loginButton);
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
fixtureList();
System.out.println("Loading the fixtures screen");
}
});
}
public static void main(String[] args)
{
Main window = new Main();
window.setTitle("PE Fixtures v1.0");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(250, 430);
window.loginPanel();
window.getContentPane().setBackground(new Color(53, 56, 64));
window.setVisible(true);
}
}
Call repaint in the ActionListener
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
fixtureList();
System.out.println("Loading the fixtures screen");
repaint();
}

Categories

Resources