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:
Related
I have to do a project in Java and thought a GUI Text Adventure would be cool. My Problem is that when I create a JPanel and move it further down on the screen, the panel first changes its size and then disappears completely at one point.
On the GameScreen there should be a panel for choice Options to be put on but it refuses to go further down than about half the size of the Screen.
Here's the code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Yeet {
JFrame epicOfYeet;
Container con;
JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel;
JLabel titleNameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
Font normalFont = new Font ("Times New Roman", Font.PLAIN, 55);
JButton startButton;
JButton choice1;
JButton choice2;
JButton choice3;
JButton choice4;
JTextArea mainTextArea;
TitleScreenHandler tsHandler = new TitleScreenHandler();
public static void main(String[] args) {
new Yeet();
}
public Yeet() {
epicOfYeet = new JFrame();
epicOfYeet.setSize(1200, 1000);
epicOfYeet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
epicOfYeet.getContentPane().setBackground(Color.black);
epicOfYeet.setLayout(null);
con = epicOfYeet.getContentPane();
titleNamePanel = new JPanel();
titleNamePanel.setBounds(190, 100, 800, 230);
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("EPIC OF YEET");
titleNameLabel.setForeground(Color.red);
titleNameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(400, 500, 400, 100);
startButtonPanel.setBackground(Color.black);
startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
startButton.setFocusPainted(false);
titleNamePanel.add(titleNameLabel);
startButtonPanel.add(startButton);
con.add(titleNamePanel);
con.add(startButtonPanel);
epicOfYeet.setVisible(true);
}
public void createGameScreen(){
titleNamePanel.setVisible(false);
startButtonPanel.setVisible(false);
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 1000, 400);
mainTextPanel.setBackground(Color.green);
con.add(mainTextPanel);
mainTextArea = new JTextArea("You come to your senses again.\nThe dewy grass you're laying on is gleaming with moonlight.\nYour body aches as you get up and catch a \nglimpse of your surroundings.\n");
mainTextArea.setBounds(100, 100, 1000, 250);
mainTextArea.setBackground(Color.blue);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
choiceButtonPanel = new JPanel();
choiceButtonPanel.setBounds(300, 500, 600, 550);
choiceButtonPanel.setBackground(Color.red);
con.add(choiceButtonPanel);
}
public class TitleScreenHandler implements ActionListener{
#Override
public void actionPerformed(ActionEvent event) {
createGameScreen();
}
}
}
Here is a basic implementation using layout mangers, avoiding the bad practice of null layout manager.
It is not an optimal one, but should get you started:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Yeet {
JFrame epicOfYeet;
Container con;
JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel;
JLabel titleNameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
Font normalFont = new Font ("Times New Roman", Font.PLAIN, 55);
JButton startButton, coice1, choice2, choice3, choice4;
JTextArea mainTextArea;
TitleScreenHandler tsHandler = new TitleScreenHandler();
public static void main(String[] args) {
SwingUtilities.invokeLater(()->new Yeet());
}
public Yeet() {
epicOfYeet = new JFrame();
//epicOfYeet.setSize(1200, 1000); // do not set size. let layout manager calcualte it
epicOfYeet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
epicOfYeet.getContentPane().setBackground(Color.black);
//epicOfYeet.setLayout(null); //aviod null layouts Jframe uses BorderLayout by default
con = epicOfYeet.getContentPane();
titleNamePanel = new JPanel();
//titleNamePanel.setBounds(190, 100, 800, 230);
titleNamePanel.setPreferredSize(new Dimension(800, 230));//set preferred size to be used by layout manager
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("EPIC OF YEET");
titleNameLabel.setForeground(Color.red);
titleNameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
//startButtonPanel.setBounds(400, 500, 400, 100);
startButtonPanel.setPreferredSize(new Dimension(400, 100));
startButtonPanel.setBackground(Color.black);
startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
startButton.setFocusPainted(false);
titleNamePanel.add(titleNameLabel);
startButtonPanel.add(startButton);
con.add(titleNamePanel, BorderLayout.PAGE_START); //set to top
con.add(startButtonPanel, BorderLayout.PAGE_END); //set to bottom
epicOfYeet.pack();
epicOfYeet.setVisible(true);
}
public void createGameScreen(){
//titleNamePanel.setVisible(false);
//startButtonPanel.setVisible(false);
con.remove(titleNamePanel);
con.remove(startButtonPanel);
mainTextPanel = new JPanel();
//mainTextPanel.setBounds(100, 100, 1000, 400);
mainTextPanel.setBackground(Color.green);
con.add(mainTextPanel); // added to center position of the BorderLayout (the default)
mainTextArea = new JTextArea(10, 20); //size in rows, cols
mainTextArea.setText("You come to your senses again.\nThe dewy grass you're laying on is gleaming with moonlight.\nYour body aches as you get up and catch a \nglimpse of your surroundings.\n");
//mainTextArea.setBounds(100, 100, 1000, 250);
mainTextArea.setBackground(Color.blue);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
choiceButtonPanel = new JPanel();
//choiceButtonPanel.setBounds(300, 500, 600, 550);
choiceButtonPanel.setPreferredSize(new Dimension(600, 550));
choiceButtonPanel.setBackground(Color.red);
con.add(choiceButtonPanel, BorderLayout.PAGE_END);//add to bottom
epicOfYeet.pack();
}
public class TitleScreenHandler implements ActionListener{
#Override
public void actionPerformed(ActionEvent event) {
createGameScreen();
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I've got a simple program to change the background of a JFrame and the foreground of a JLabel. The JFrame has a JLabel and 2 JButtons on it, one to change the words and the other to change the background.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class ColorChooserDemo extends JPanel {
static JFrame frame;
JLabel label;
JButton button1, button2;
public ColorChooserDemo() {
this.setBackground(new Color(255, 0, 255));
setLayout(null);
label = new JLabel("This is Text");
button1 = new JButton("Set Word Color");
button2 = new JButton("Set Background Color");
add(label);
add(button1);
add(button2);
label.setBounds(130, 10, 111, 15);
button1.setBounds(100, 40, 200, 35);
button2.setBounds(70, 90, 250, 35);
button1.addActionListener(buttonPressed);
button2.addActionListener(buttonPressed);
button1.setActionCommand("words");
button2.setActionCommand("back");
label.setFont(new Font("Helvetica", Font.BOLD, 20));
button1.setFont(new Font("Helvetica", Font.BOLD, 20));
button2.setFont(new Font("Helvetica", Font.BOLD, 20));
}
AbstractAction buttonPressed = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("words")) {
Color c = JColorChooser.showDialog(null, "Choose a Color", label.getForeground());
if (c != null)
label.setForeground(c);
}
if (e.getActionCommand().equals("back")) {
Color c = JColorChooser.showDialog(null, "Choose a Color", label.getForeground());
if (c != null)
frame.getContentPane().setBackground(c);
}
repaint();
}
};
public static void main(String[] args) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("ColorChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ColorChooserDemo());
frame.setSize(400, 200);
frame.setLocation((int) (screenSize.getWidth() - 400) / 2, (int) (screenSize.getHeight() - 200) / 2);
frame.setResizable(false);
frame.setVisible(true);
}
}
For whatever reasonframe.getContentPane().setBackground(c); doesn't work.
I've looked for solutions but nothing is working and I don't understand why. Please help.
--EDIT--
I tried repaint(); and it still doesn't work. Could it have to do with frame being static?
This is a good example of where something like static can stab you in the back...
So, in your code, you are doing...
frame.getContentPane().setBackground(c);
when you want to change the background color, but you have to ask yourself the question, what is the contentPane? Since your component extends from JPanel (which is opaque) and sets it's own background color (this.setBackground(new Color(255, 0, 255));) (and because by default JFrame uses a BorderLayout) you component is covering the entire content area of the frame, so any calls to frame.getContentPane().setBackground(c) are simply not visible (because your panel is covering it).
Instead, you should simple use...
setBackground(c);
For clarification...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ColorChooserDemo extends JPanel {
JLabel label;
JButton button1, button2;
public ColorChooserDemo() {
this.setBackground(new Color(255, 0, 255));
setLayout(new GridBagLayout());
label = new JLabel("This is Text");
button1 = new JButton("Set Word Color");
button2 = new JButton("Set Background Color");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(label, gbc);
add(button1, gbc);
add(button2, gbc);
button1.addActionListener(buttonPressed);
button2.addActionListener(buttonPressed);
button1.setActionCommand("words");
button2.setActionCommand("back");
label.setFont(new Font("Helvetica", Font.BOLD, 20));
button1.setFont(new Font("Helvetica", Font.BOLD, 20));
button2.setFont(new Font("Helvetica", Font.BOLD, 20));
}
AbstractAction buttonPressed = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("words")) {
Color c = JColorChooser.showDialog(null, "Choose a Color", label.getForeground());
if (c != null) {
label.setForeground(c);
}
}
if (e.getActionCommand().equals("back")) {
System.out.println("...");
Color c = JColorChooser.showDialog(null, "Choose a Color", getBackground());
if (c != null) {
setBackground(c);
}
}
repaint();
}
};
public static void main(String[] args) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame = new JFrame("ColorChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ColorChooserDemo());
frame.setSize(400, 200);
frame.setLocation((int) (screenSize.getWidth() - 400) / 2, (int) (screenSize.getHeight() - 200) / 2);
frame.setResizable(false);
frame.setVisible(true);
}
}
I would also strongly suggest you have a look at Laying Out Components Within a Container, How to Use GridBagLayout and How to Use BorderLayout for more details about how the layou management API works
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();
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
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