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
Related
I wrote a little something here. It's working if I don't backPanel.setLayout(new GridBagLayout);
But without the grid bag, the content stays in the top left I maximise the screen.
With the grid bag I only get the red backPanel in the frame. Well, there is a gray pixel in the middle of the screen. I'm assuming that's my panel, but I can't make it bigger. I tried setSize but it doesn't change. Also, I had the panel.setBounds(0, 0, getWidth(),getHeight());. I'm not sure why I removed it.
My main is in the other file. The only thing it does at the moment is to call the LoginFrame.
Here is the code:
package first;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class LoginFrame extends JFrame implements ActionListener {
private JTextField textField;
private JPasswordField passwordField;
public LoginFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 300);
JPanel backPanel = new JPanel(new GridBagLayout());
backPanel.setBackground(Color.RED);
JPanel panel = new JPanel();
panel.setSize(500, 300);
panel.setBackground(Color.LIGHT_GRAY);
panel.setLayout(null);
JLabel label;
panel.add(label = new JLabel("Username:"));
label.setBounds(20, 100, 100, 25);
panel.add(textField = new JTextField());
textField.setBounds(140, 100, 200, 25);
panel.add(label = new JLabel("Password:"));
label.setBounds(20, 145, 100, 25);
panel.add(passwordField = new JPasswordField());
passwordField.setBounds(140, 145, 200, 25);
panel.add(label = new JLabel("CTC Bank"));
label.setFont(new Font("New Times Roman", Font.BOLD, 50));
label.setBounds(0, 0, getWidth(), 100);
label.setHorizontalAlignment(JLabel.CENTER);
JButton button;
panel.add(button = new JButton("Login"));
button.setBounds(140, 200, 100, 25);
button.addActionListener(this);
button = defaultActionKeyEnter(button, KeyEvent.VK_ENTER);
panel.add(button = new JButton("Register"));
button.setBounds(240, 200, 100, 25);
button.addActionListener(this);
button = defaultActionKeyEnter(button, KeyEvent.VK_ENTER);
//add(panel);
backPanel.add(panel);
add(backPanel, BorderLayout.CENTER);
revalidate();
repaint();
setLocationRelativeTo(null);
setVisible(true);
}
public static JButton defaultActionKeyEnter(JButton button, int desiredKeyCode) {
InputMap inputMap = button.getInputMap(JComponent.WHEN_FOCUSED);
KeyStroke spaceKeyPressed = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false);
KeyStroke spaceKeyReleased = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true);
KeyStroke desiredKeyPressed = KeyStroke.getKeyStroke(desiredKeyCode, 0, false);
KeyStroke desiredKeyReleased = KeyStroke.getKeyStroke(desiredKeyCode, 0, true);
inputMap.put(desiredKeyPressed, inputMap.get(spaceKeyPressed));
inputMap.put(desiredKeyReleased, inputMap.get(spaceKeyReleased));
inputMap.put(spaceKeyPressed, "none");
inputMap.put(spaceKeyReleased, "none");
return button;
}
// Unfinished code dont worry bout it...
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Login")) {
if (textField.getText().equals("Heinz")
&& (new String(passwordField.getPassword()).equals("password123"))) {
// color = Color.GREEN;
} else {
JOptionPane.showMessageDialog(this, "Wrong Username or Password", "Error", JOptionPane.WARNING_MESSAGE);
// color = Color.RED;
}
} else {
JOptionPane.showMessageDialog(this, "Cya");
dispose();
setVisible(false);
}
// panel.setBackground(color);
}
}
I have seen questions about this but none of the answers were helpful in my case.
Calling the following didn't help.
revalidate();
repaint();
Did I maybe add it in the wrong order?
And how does the code look like to you? Would you consider this clean?
The layout of backPanel will mis-calculate the dimensions of "panel" because "panel" does not participate in layout management properly, without a layout manager of its own.
One solution to this is to use setLayout(null) also on the "backPanel", or add "panel" directly to the JFrame.
With the first suggestion ("backPanel.setLayout(null);" just after it is created), plus the following main method:
public static void main(String[] args) {
new LoginFrame();
}
I get this:
this is my code
This code gives me a frame with text displayed inside it.
I may need to modify the text as necessary, and then there are two buttons. One pushes the code further, the other one closes the application.
What I want to do is add listener to okButt, within the createOverviewButtonsPanel method, that would retrieve the value of txtMenuOverview in order to pass it further is this even possible?
MenuOverviewFrame class:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class MenuOverviewFrame extends JFrame{
private static final long serialVersionUID = -5908534022988507381L;
private static final Font FONT = new Font("Courier", Font.BOLD, 16);
private static final Font MENU_FONT = new Font(Font.MONOSPACED,Font.BOLD,14);
private static final Color BLUE_STEEL = new Color(70, 107, 176);
private static final Dimension INITIAL_SIZE = new Dimension(500, 300);
private static final Dimension MINIMUM_SIZE = new Dimension(275, 150);
public static void main(String[] args) {
System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
SwingUtilities.invokeLater(() -> {
MenuOverviewFrame frame = new MenuOverviewFrame("Test test\ntest\ntest");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
public MenuOverviewFrame(String menuOutput) {
super("Daily Menu");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
getContentPane().setBackground(Color.WHITE);
setSize(INITIAL_SIZE); // The initial frame size
setMinimumSize(MINIMUM_SIZE);
JTextArea txtMenuOverview = new JTextArea(menuOutput);
txtMenuOverview.setFont(MENU_FONT);
JScrollPane taScroll = new JScrollPane(txtMenuOverview);
add(taScroll);
JPanel overviewButtonsPanel = createOverviewButtonsPanel();
overviewButtonsPanel.setPreferredSize(new Dimension(overviewButtonsPanel.getPreferredSize().width, overviewButtonsPanel.getPreferredSize().height + 30));
getContentPane().add(overviewButtonsPanel, BorderLayout.SOUTH); // at the center
}
private JPanel createOverviewButtonsPanel() {
JPanel panel = new JPanel();
//panel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 1));
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0,0,0,20);
panel.setBackground(BLUE_STEEL);
JButton okButt = new JButton("Send menu");
JButton koButt = new JButton("Abort mission");
panel.add(okButt,gbc);
panel.add(koButt);
okButt.setVerticalAlignment(SwingConstants.CENTER);
return panel;
}
}
Thank you
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)
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();
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