CardLayout showing blank JPanel in Java - java

I've searched up different tutorials and looked at the Class Profile for CardLayout and JPanel but I can't seem to get my window to show up. Currently it opens a frame with the proper dimensions and title but nothing in the actual container.
This is the code I have(P.S. I know it's a hot mess)
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class Casino extends JFrame implements ActionListener {
private JButton start, settings, scenario, music;
/**
* Constructor method
*/
public Casino(){
JPanel mainUI, startUI, settingsUI, scenarioUI, blackjackUI, oddorevenUI, tcmUI, overorunderUI, slotsUI;
JPanel menus = new JPanel(new CardLayout());
CardLayout GUI = (CardLayout) menus.getLayout();
mainUI = new JPanel();
getContentPane().add(mainUI);
mainUI.setBackground(new Color(53, 9, 9));
//Background items
JLabel title = new JLabel(new ImageIcon("title.png"));
title.setBounds(0,-280,780,700);
mainUI.add(title);
JLabel border = new JLabel(new ImageIcon("mainscreenborder.png"));
border.setBounds(0, 180, 780, 700);
mainUI.add(border);
//Main menu buttons
settings = new JButton();
ImageIcon s = new ImageIcon("settings-button.png");
settings.setBounds(320, 200, 122, 63);
settings.setIcon(s);
mainUI.add(settings);
music = new JButton();
ImageIcon m = new ImageIcon("music-button.png");
music.setBounds(320, 268, 122, 63);
music.setBackground(new Color(53, 9, 9));
music.setIcon(m);
mainUI.add(music);
scenario = new JButton();
ImageIcon sc = new ImageIcon("scenario-button.png");
scenario.setBounds(320, 336, 122, 63);
scenario.setBackground(new Color(53, 9, 9));
scenario.setIcon(sc);
mainUI.add(scenario);
start = new JButton();
ImageIcon st = new ImageIcon("start-button.png");
start.setBounds(320, 404, 122, 63);
start.setBackground(new Color(53, 9, 9));
start.setIcon(st);
mainUI.add(start);
menus.add(mainUI, "Main Menu");
GUI.show(menus, "Main Menu");
setSize(780, 700);
setResizable(false);
setLayout(GUI);
setTitle("White Lily Casino");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Casino wlc = new Casino();
}
}
Note: It worked before when I was using the Container c method instead of using a JPanel and CardLayout. I am trying to switch it to card layout now because I want to use buttons to navigate to multiple screens

Try adding the mainUI to the JFrame
getContentPane().add(mainUI)
or
add(mainUI)

Related

JSpinner Arrows in odd positioning when used with JLabel

Hello So i have created a JFrame with a JSpinner inside (as you can see in the picture). Right now, the BorderLabel is showing in the Jspinner (as it should) but the arrows on the JSpinner are there as a part of the entire thing instead of just the JSpinner field. I would like help to find out how to put the JSpinner arrows on the bar. Thank you.
For you who asked for code,
Also I miss stated JLabel Earlier. I meant TitledBorder
import java.awt.ComponentOrientation;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class ex extends JFrame{
public static void main(String[] args){
new ex();
}
ex(){
super("test");
setSize(200,100);
SpinnerModel sm = new SpinnerNumberModel(3,1,25,1);
JSpinner shiftIn = new JSpinner(sm);
JPanel p = new JPanel();
shiftIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
shiftIn.setBorder(new CompoundBorder(new TitledBorder(new EmptyBorder(0,
0, 0, 0), "Shift Key"), shiftIn
.getBorder()));
p.add(shiftIn);
add(p);
setVisible(true);
}
}
shiftIn.setBorder(new CompoundBorder(
new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Shift Key"),
shiftIn.getBorder()));
This is not the sort of thing that a titled border was made for!
Use a JLabel and put it in the PAGE_START of a BorderLayout, put the JSpinner in the PAGE_END. Add that container (the panel) where the spinner is currently added. (Then add a mnemonic for the label and make it the 'label for' the spinner.)
This is how to use that idea inside another layout (GridLayout in this example)..
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class ex extends JFrame {
public static void main(String[] args) {
new ex();
}
ex() {
super("test");
// imagine this is actually using GridBagLayout
JPanel ui = new JPanel(new GridLayout(0, 3,4,4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
SpinnerModel sm = new SpinnerNumberModel(3, 1, 25, 1);
JSpinner shiftIn = new JSpinner(sm);
JLabel l = new JLabel("Shift Key");
JPanel p = new JPanel(new BorderLayout());
p.add(l, BorderLayout.PAGE_START);
p.add(shiftIn, BorderLayout.PAGE_END);
add(ui);
for (int ii=0; ii<9; ii++) {
if (ii==4) {
ui.add(p);
} else {
ui.add(new JButton("Button"));
}
}
pack();
setVisible(true);
}
}
You appear to be running into risk by messing with the JSpinner's border. Myself, I would wrap my JSpinner in a JPanel and then give that wrapper JPanel the desired border. For example:
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
#SuppressWarnings("serial")
public class Ex extends JFrame {
public static void main(String[] args) {
new Ex();
}
Ex() {
super("test");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
SpinnerModel sm = new SpinnerNumberModel(3, 1, 25, 1);
JSpinner shiftIn = new JSpinner(sm);
JPanel spinnerWrapper = new JPanel(new BorderLayout());
spinnerWrapper.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEmptyBorder(0, 0, 0, 0), "Shift Key"));
spinnerWrapper.add(shiftIn);
shiftIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
add(spinnerWrapper);
pack();
setVisible(true);
}
}

How do I set an image to a background

I'm trying to create a simple JFrame with the content pane JPanel. I want to know how to set an IMAGE as a background. I know that a lot of people have a already asked this, but I get unresolved compilation errors whenever I try.
In the end, I created a whole new class in my class, but that had errors too.
How do I do this? Please help.
package menu;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.ImageObserver;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import models.AskTheAdmiralFrame;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
/**
*
* #author Russell
*
*/
public class HistoraskMenu extends JFrame {
private static final long serialVersionUID = 8106152174535131551L;
private static JPanel panel;
private JButton ATA = new JButton("Ask The Admiral");
public Color oldPaper = new Color(255, 230, 179);
private JLabel label = new JLabel();
private final JLabel label_1 = new JLabel("");
private final ImageIcon bg = new ImageIcon("/Users/Russell/Desktop/Russell/Java/Coding"
+ "/eclipse workspace/Historask/resources/bg.jpg");
Image img = bg.getImage();
public HistoraskMenu(){
setBounds(50, 50, 700, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Historask");
panel = new JPanel();
setContentPane(panel);
label.setHorizontalAlignment(SwingConstants.CENTER); //Sets position of Historask sign
label.setVerticalAlignment(SwingConstants.TOP);
label.setBackground(new Color(238, 232, 170)); //Sets the colour of the foreground and background of Historask sign
label.setForeground(oldPaper);
label.setIcon(new ImageIcon("/Users/Russell/Desktop/Russell/Java/Coding"
+ "/eclipse workspace/Historask/resources/title.png"));
label.setOpaque(true);
label.setBounds(50, 10, 500, 50);
getContentPane().add(label);
panel.add(label_1);
ATA.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) { //What happens when you press ATA button
new AskTheAdmiralFrame();
}
});
getContentPane().add(ATA);
setVisible(true);
}
public class panel extends JPanel{
setLayout(new GridLayout(0, 1, 0, 0));
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, this);
super.paintComponents(g);
}
}
If you are referring to panel object in the HistoraskMenu() constructor it wont draw image because you are innitializing this to JPanel directly. Rather try panel = new panel();

How to show(add) JTextArea after button click?

i want to show my JTextArea after button CONNECT_BUTTON is clicked. What should I do ? Furthermore i'm trying to add some grid in my JTextArea because I want to use it for show some records from databse. Any ideas how to do that ?
package DataBase_Hospital;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test extends JFrame implements ActionListener {
private JButton FIND_BUTTON;
private JButton MESSAGE_BUTTON;
private JButton CONNECT_BUTTON;
private JButton CLEAR_BUTTON;
private JButton ADD_BUTTON;
private JButton RAPORT_BUTTON;
private JButton EDIT_BUTTON;
private JButton DOWNLOAD_BUTTON;
private JTextArea DATABASE_FIELD;
private DatabaseManagement DATABASE;
public test(){
setTitle("Hospital Management");
setSize(900,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
JLabel background=new JLabel(new ImageIcon("/Users/Dominik/Desktop/j.jpg"));
add(background);
DATABASE_FIELD = new JTextArea();
JScrollPane scrollPane = new JScrollPane(DATABASE_FIELD);
scrollPane.setBounds(50, 50, 800, 400);
background.add(scrollPane);
DATABASE_FIELD.setEditable(true);
DATABASE_FIELD.setLayout(new GridLayout(3, 3));
//DATABASE_FIELD.setVisible(false);
CONNECT_BUTTON = new JButton();
CONNECT_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/connect_no.png"));
CONNECT_BUTTON.setBounds(165, 500, 60, 60);
background.add(CONNECT_BUTTON);
CONNECT_BUTTON.addActionListener(this);
CONNECT_BUTTON.setToolTipText("Connect to Data Base");
FIND_BUTTON = new JButton();
FIND_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/search.png"));
FIND_BUTTON.setBounds(240, 500, 60, 60);
background.add(FIND_BUTTON);
FIND_BUTTON.addActionListener(this);
FIND_BUTTON.setToolTipText("Find record in Data Base");
ADD_BUTTON = new JButton();
ADD_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/user_add.png"));
ADD_BUTTON.setBounds(315, 500, 60, 60);
background.add(ADD_BUTTON);
ADD_BUTTON.addActionListener(this);
ADD_BUTTON.setToolTipText("Add record to Data Base");
RAPORT_BUTTON = new JButton();
RAPORT_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/raport.png"));
RAPORT_BUTTON.setBounds(392, 500, 60, 60);
background.add(RAPORT_BUTTON);
RAPORT_BUTTON.addActionListener(this);
RAPORT_BUTTON.setToolTipText("Generates raport");
EDIT_BUTTON = new JButton();
EDIT_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/user_edit.png"));
EDIT_BUTTON.setBounds(467, 500, 60, 60);
background.add(EDIT_BUTTON);
EDIT_BUTTON.addActionListener(this);
EDIT_BUTTON.setToolTipText("Edit record from Data Base");
CLEAR_BUTTON = new JButton();
CLEAR_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/delete.png"));
CLEAR_BUTTON.setBounds(544, 500, 60, 60);
background.add(CLEAR_BUTTON);
CLEAR_BUTTON.addActionListener(this);
CLEAR_BUTTON.setToolTipText("Clear all Data Base");
MESSAGE_BUTTON = new JButton();
MESSAGE_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/message.png"));
MESSAGE_BUTTON.setBounds(619, 500, 60, 60);
background.add(MESSAGE_BUTTON);
MESSAGE_BUTTON.addActionListener(this);
MESSAGE_BUTTON.setToolTipText("Send message to another user");
DOWNLOAD_BUTTON = new JButton();
DOWNLOAD_BUTTON.setIcon(new ImageIcon("/Users/Dominik/Desktop/download.png"));
DOWNLOAD_BUTTON.setBounds(694, 500, 60, 60);
background.add(DOWNLOAD_BUTTON);
DOWNLOAD_BUTTON.addActionListener(this);
DOWNLOAD_BUTTON.setToolTipText("Download Data Base to a text file");
validate();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
test window = new test();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object EVENT_SOURCE = e.getSource();
DATABASE = new DatabaseManagement();
if (EVENT_SOURCE == CLEAR_BUTTON)
{
System.out.println("siema");
}
else if (EVENT_SOURCE == DOWNLOAD_BUTTON)
{
dispose();
}
else if (EVENT_SOURCE == CONNECT_BUTTON)
{
DATABASE_FIELD.setText("");
//** TRZEBA ZROBIC SIATKE !!! **//
DATABASE_FIELD.append("IMIE ");
DATABASE_FIELD.append("NAZWISKO ");
DATABASE_FIELD.append("PESEL \n");
DATABASE_FIELD.append(DATABASE.showDataBase());
}
}
}
you have to set proper LayoutManager in the case that you want to use JLabel as container (Grid or FlowLayout)
don't to use NullLayout
JTextArea isn't designated to be a container remove DATABASE_FIELD.setLayout(new GridLayout(3, 3));
use JTextArea (10, 15) as intial size instead of any sizing (then valid for JScrollPane too)
add JScrollPane with JTextArea to JFrames CENTER area or to change JLabels LayoutManager to BorderLayout, then to put JButtons to another separate JLabel (Grid or FlowLayout)
setTitle("Hospital Management");
setSize(900,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
to move those code lines to the end of constructor, then remove validate, as aside should be revalidate() and repaint(), becuase you didn't stop for Image repainting
call pack() instead of setSize
See Initial Thread
only 1quater of possible issues, just about most important things

JPanel didn't show rest of it's component

I have frame with a button and a JPanel as I named panel, I want after I clicked the button add an inner panel to my panel. But this but there is a problem with this! because after adding second panel it didn't add any other panel.
Code
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import javax.swing.JTextField;
public class DrawImages extends JFrame{
int i;
public DrawImages() {
JButton btnNewButton = new JButton("New button");
i = 0;
getContentPane().add(btnNewButton, BorderLayout.SOUTH);
setMinimumSize(new Dimension(1000,150));
final JPanel panel = new JPanel();
panel.setSize(995, 145);
getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
final JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(0, 0, 46, 14);
panel.add(lblNewLabel);
btnNewButton.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent paramActionEvent) {
JPanel panel_1 = new JPanel();
//getContentPane().add(panel_1, BorderLayout.NORTH);
panel_1.setLayout(null);
JLabel imagelable = new JLabel(new ImageIcon("c:\\good.jpg"));
imagelable.setBounds(70, 5, 105, 65);
panel_1.add(imagelable);
JLabel lblNewLabel_4 = new JLabel("Up Label");
lblNewLabel_4.setBounds(5, 5, 65, 35);
panel_1.add(lblNewLabel_4);
JLabel lblNewLabel_2 = new JLabel("Down Label");
lblNewLabel_2.setBounds(5, 25, 65, 65);
panel_1.add(lblNewLabel_2);
lblNewLabel.setText(""+i);
panel_1.setBounds(5+170*i, 5, 170+170*i, 70);
panel.add(panel_1);
i++;
}
});
panel.setMinimumSize(new Dimension(995,150));
}
public static void main(String[]args)
{
DrawImages drawImages = new DrawImages();
drawImages.setVisible(true);
}
}
The problem is with the statement:
panel_1.setLayout(null);
panel_1 doesn't have any preferred size so will not appear (or will appear as a tiny dot).
Swing was designed to use layout managers. You could use GridLayout in this particular case.
Read: Doing Without a Layout Manager
I agree with Reimeus. Just to test your code, I used
panel_1.setLayout(new FlowLayout());
And I could see the panels being added without calling repaint() on the parent panel.
Thanks for your answers but my problem is I added third arguman in every circle:
panel_1.setBounds(5+170*i, 5, 170+170*i, 70);
so my panel get bigger and bigger (event biger than my monitor) so the correct is:
panel_1.setBounds(5+170*i, 5, 170, 70);

How to close main frame when open new one

Ive created two frames my main frame is Home and the second one is Selectie
On home there is a button which open the frame selectie, but i want when i click this button the main frame home wil dissapear and only selectie will be shown. The code for the button ive make in a other package and i dont want it in the same class as my main (home)
Code from Home:
package View;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import Controller.HomeController;
import music.PlaySound;
public class Home extends JFrame {
private JLabel label, label1, label2;
private JPanel panel;
private JButton logo, logo1, logo2, logo3, logo4, logo5, selectie;
private Container window = getContentPane();
private HomeController Controller;
public Home (){
initGUI();
Controller = new HomeController();
}
public void addHomeListener(ActionListener a){
selectie.addActionListener(a);
}
public void initGUI(){
setLayout(null);
setTitle("");
setPreferredSize(new Dimension(800,600));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel();
label.setBounds(0, 0, 266, 800);
label.setBackground(Color.WHITE);
label.setOpaque(true);
window.add(label);
label1 = new JLabel();
label1.setBounds(267, 0, 266, 800);
label1.setBackground(Color.RED);
label1.setOpaque(true);
window.add(label1);
label2 = new JLabel();
label2.setBounds(533, 0, 266, 800);
label2.setBackground(Color.WHITE);
label2.setOpaque(true);
window.add(label2);
logo = new JButton(new ImageIcon("../Ajax/src/img/logotje.gif"));
logo.setBorderPainted(false);
logo.setBounds(40, 150, 188, 188);
label1.add(logo);
logo1 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
logo1.setBorderPainted(false);
logo1.setBounds(10, 50, 82, 82);
label1.add(logo1);
logo2 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
logo2.setBorderPainted(false);
logo2.setBounds(92, 20, 82, 82);
label1.add(logo2);
logo3 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
logo3.setBorderPainted(false);
logo3.setBounds(174, 50, 82, 82);
label1.add(logo3);
logo4 = new JButton(new ImageIcon("../Ajax/src/img/shirt.png"));
logo4.setBorderPainted(false);
logo4.setBounds(50, 50, 135, 182);
label.add(logo4);
logo5 = new JButton(new ImageIcon("../Ajax/src/img/uitshirt.png"));
logo5.setBorderPainted(false);
logo5.setBounds(65, 50, 138, 190);
label2.add(logo5);
selectie = new JButton("Selectie");
selectie.setBounds(60, 500, 99, 25);
selectie.setActionCommand("selectie");
label.add(selectie);
pack();
addHomeListener(new HomeController());
}
}
Code from the button:
package Controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import View.Home;
import View.Selectie;
public class HomeController implements ActionListener {
public void actionPerformed (ActionEvent e){
Selectie selectie = new Selectie();
selectie.setVisible(true);
}
}
Please do give valid attention to what #kleopatra and #mKorbel, has to say, they are very much right in pointing that out to you to make things easier.
Here I had added some comments in the code, do check this out :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//import Controller.HomeController;
//import music.PlaySound;
public class Home extends JFrame {
private JLabel label, label1, label2;
private JPanel panel;
private JButton logo, logo1, logo2, logo3, logo4, logo5, selectie;
private Container window = getContentPane();
private HomeController Controller;
public Home (){
initGUI();
}
public void addHomeListener(ActionListener a){
selectie.addActionListener(a);
}
public void initGUI(){
setLayout(null);
setTitle("");
setPreferredSize(new Dimension(800,600));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel();
label.setBounds(0, 0, 266, 800);
label.setBackground(Color.WHITE);
label.setOpaque(true);
window.add(label);
label1 = new JLabel();
label1.setBounds(267, 0, 266, 800);
label1.setBackground(Color.RED);
label1.setOpaque(true);
window.add(label1);
label2 = new JLabel();
label2.setBounds(533, 0, 266, 800);
label2.setBackground(Color.WHITE);
label2.setOpaque(true);
window.add(label2);
logo = new JButton(new ImageIcon("../Ajax/src/img/logotje.gif"));
logo.setBorderPainted(false);
logo.setBounds(40, 150, 188, 188);
label1.add(logo);
logo1 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
logo1.setBorderPainted(false);
logo1.setBounds(10, 50, 82, 82);
label1.add(logo1);
logo2 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
logo2.setBorderPainted(false);
logo2.setBounds(92, 20, 82, 82);
label1.add(logo2);
logo3 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
logo3.setBorderPainted(false);
logo3.setBounds(174, 50, 82, 82);
label1.add(logo3);
logo4 = new JButton(new ImageIcon("../Ajax/src/img/shirt.png"));
logo4.setBorderPainted(false);
logo4.setBounds(50, 50, 135, 182);
label.add(logo4);
logo5 = new JButton(new ImageIcon("../Ajax/src/img/uitshirt.png"));
logo5.setBorderPainted(false);
logo5.setBounds(65, 50, 138, 190);
label2.add(logo5);
selectie = new JButton("Selectie");
selectie.setBounds(60, 500, 99, 25);
selectie.setActionCommand("selectie");
label.add(selectie);
pack();
/*
* You are making a new object again,
* when you already had declared it as
* an instance Variable. So I used the
* one declared as instance variable..
* To this we will send the object of Home
* class, means the object of this class..
* And as we know that object of the
* class we are in is by default known
* as this, so passing this to HomeController class.
*/
Controller = new HomeController(this);
addHomeListener(Controller);
setVisible(true);
}
public static void main(String... args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Home();
}
});
}
}
class HomeController implements ActionListener {
/*
* Here we declared a Home class's variable,
* that we will use to dispose that JFrame.
*/
private Home home;
public HomeController(Home home)
{
this.home = home;
}
public void actionPerformed (ActionEvent e){
home.dispose();
Selectie selectie = new Selectie();
selectie.setVisible(true);
}
}
class Selectie extends JFrame
{
public Selectie()
{
initGUI();
}
public void initGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationByPlatform(true);
setSize(300, 300);
}
}
I'd suggest to use CardLayout, most easiest, very confortable for (+1 for SSCCE) your code posted here
never create, re_create bunch of another JFrames, only in the cases that you have got very important reasons, then use JDialog with parent to the JFrame

Categories

Resources