I created a JFrame with the Swing framework, in which I added a JButton that will display a form created on a JPanel on the frame.
I added the action listener on the button and it is displaying panel as I expected;
But the boxes of components like radio buttons and checkboxes cannot be seen.
They appear only when I hover mouse over them.
Look at the other option in radio button and checkboxes:
Here is the code:
package codes;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class JFrameBackground extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
JLabel l1,l2,l3,l4;
JTextField t1;
JButton b1;
JRadioButton r1;
JComboBox com1;
JCheckBox chk1;
JPanel p2;
JButton b;
public JFrameBackground()
{
p2=new JPanel();
p2.setLayout(null);
p2.setBounds(250, 0, 400, 400);
add(p2);
l1 = new JLabel("Name:");
l1.setBounds(100,10,70,30);
p2.add(l1);
t1 = new JTextField();
t1.setBounds(100,50,70,30);
p2.add(t1);
l2 = new JLabel("Gender:");
l2.setBounds(100,130,70,30);
p2.add(l2);
r1 = new JRadioButton("Male");
r1.setBounds(100,150,70,30);
p2.add(r1);
r1 = new JRadioButton("Female");
r1.setBounds(150,150,70,30);
p2.add(r1);
l3 = new JLabel("Course:");
l3.setBounds(100,180,70,30);
p2.add(l3);
chk1= new JCheckBox("Bca");
chk1.setBounds(100, 200, 70, 30);
p2.add(chk1);
chk1= new JCheckBox("BBA");
chk1.setBounds(150, 200, 70, 30);
p2.add(chk1);
chk1= new JCheckBox("BCOM");
chk1.setBounds(200, 200, 70, 30);
p2.add(chk1);
l4 = new JLabel("Country:");
l4.setBounds(100,220,70,30);
p2.add(l4);
String name[] = {"India","USA","UK","Rus"};
com1=new JComboBox(name);
com1.setBounds(100, 250, 70, 30);
p2.add(com1);
b1= new JButton("Submit");
b1.setBounds(140, 300, 90, 30);
p2.add(b1);
b =new JButton("Show form");
b.setBounds(0, 4, 190, 40);
b.setFocusPainted(false);
b.setBorderPainted(false);
b.addActionListener(this);
add(b);
b.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
p2.setVisible(true);
}
});
Container c=getContentPane();
c.setBackground(Color.gray);
setBounds(170, 100, 1250, 500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args)
{
new JFrameBackground();
}
public void actionPerformed(ActionEvent arg0)
{
}
}
This seems to work better.
I fixed radiobutton and checkbox positions.
I used different references for checkbox and radiobutton objects (r1, r2, chk1, chk2, chk3)
I added a buttongroup for radiobuttons.
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.ButtonGroup;
public class JFrameBackground extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
JLabel l1,l2,l3,l4;
JTextField t1;
JButton b1;
JRadioButton r1, r2;
JComboBox com1;
JCheckBox chk1, chk2, chk3;
JPanel p2;
JButton b;
public JFrameBackground()
{
p2=new JPanel();
p2.setLayout(null);
p2.setBounds(250, 0, 400, 400);
add(p2);
l1 = new JLabel("Name:");
l1.setBounds(100,10,70,30);
p2.add(l1);
t1 = new JTextField();
t1.setBounds(100,50,70,30);
p2.add(t1);
l2 = new JLabel("Gender:");
l2.setBounds(100,130,70,30);
p2.add(l2);
r1 = new JRadioButton("Male");
r1.setBounds(100,150,70,30);
p2.add(r1);
r2 = new JRadioButton("Female");
r2.setBounds(180,150,70,30);
p2.add(r2);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);
bg.add(r2);
l3 = new JLabel("Course:");
l3.setBounds(100,180,70,30);
p2.add(l3);
chk1= new JCheckBox("Bca");
chk1.setBounds(100, 200, 70, 30);
p2.add(chk1);
chk2= new JCheckBox("BBA");
chk2.setBounds(180, 200, 70, 30);
p2.add(chk2);
chk3= new JCheckBox("BCOM");
chk3.setBounds(260, 200, 70, 30);
p2.add(chk3);
l4 = new JLabel("Country:");
l4.setBounds(100,220,70,30);
p2.add(l4);
String name[] = {"India","USA","UK","Rus"};
com1=new JComboBox(name);
com1.setBounds(100, 250, 70, 30);
p2.add(com1);
b1= new JButton("Submit");
b1.setBounds(140, 300, 90, 30);
p2.add(b1);
b =new JButton("Show form");
b.setBounds(0, 4, 190, 40);
b.setFocusPainted(false);
b.setBorderPainted(false);
b.addActionListener(this);
getContentPane().add(b);
b.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
p2.setVisible(true);
}
});
Container c=getContentPane();
c.setBackground(Color.gray);
setBounds(170, 100, 1250, 500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args)
{
new JFrameBackground();
}
public void actionPerformed(ActionEvent arg0)
{
}
}
The Components to the left are too large and therefore overlap the checkboxes. Thats why they are not shown correctly. So move the right components like that:
r1.setBounds(170, 150, 70, 30);
chk1.setBounds(170, 200, 70, 30);
chk1.setBounds(240, 200, 70, 30);
and it will work.
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:
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();
}
}
}
i am developing an application for which i have created different application windows. Now i'm in a confusion on how i can link one to another using a button, that is when i click a button, it has to navigate to the other application window. I have tried it with frames from same file, but that doesnt satisfy my need.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.SystemColor;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.UIManager;
import java.awt.event.*;
public class Start {
private JFrame frame;
private JTextField txtLogInTo;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Start window = new Start();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(SystemColor.inactiveCaption);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("USER");
btnNewButton.setBackground(SystemColor.activeCaption);
btnNewButton.setBounds(152, 81, 132, 23);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("BUS ADMIN");
btnNewButton_1.setBackground(SystemColor.activeCaption);
btnNewButton_1.setBounds(152, 131, 132, 23);
frame.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("SYSTEM ADMIN");
btnNewButton_2.setBackground(SystemColor.activeCaption);
btnNewButton_2.setBounds(152, 179, 132, 23);
frame.getContentPane().add(btnNewButton_2);
txtLogInTo = new JTextField();
txtLogInTo.setBounds(95, 27, 252, 36);
txtLogInTo.setBackground(SystemColor.inactiveCaption);
txtLogInTo.setHorizontalAlignment(SwingConstants.CENTER);
txtLogInTo.setFont(new Font("Tekton Pro Cond", Font.BOLD | Font.ITALIC, 20));
txtLogInTo.setText("LOG IN TO ENTER THE SYSTEM");
frame.getContentPane().add(txtLogInTo);
txtLogInTo.setColumns(10);
class handler implements ActionListener
{
//must implement method
//This is triggered whenever the user clicks the login button
public void actionPerformed(ActionEvent ae)
{
if(btnNewButton.isEnabled())
{User u;
}
}//if
}//method
}//inner class
}
and this above program is where i have to navigate when i click a button from the below program
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.SystemColor;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class User {
private JFrame frame,frame2;
private JTextField txtWelcomeUser;
private JTextField txtEnterYourDetails;
private JTextField txtName;
private JTextField txtAge;
private JTextField txtPhoneNo;
private JTextField txtMailId;
private JTextField txtStart;
private JTextField txtDestination;
private JTextArea textArea_4;
private JTextArea textArea_5;
private JTextField txtEn;
private ActionListener action;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
User window = new User();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public User() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(0, 0, 750, 550);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
txtWelcomeUser = new JTextField();
txtWelcomeUser.setBackground(SystemColor.inactiveCaption);
txtWelcomeUser.setFont(new Font("Tahoma", Font.PLAIN, 26));
txtWelcomeUser.setText("Welcome user");
txtWelcomeUser.setBounds(176, 11, 173, 52);
frame.getContentPane().add(txtWelcomeUser);
txtWelcomeUser.setColumns(10);
txtEnterYourDetails = new JTextField();
txtEnterYourDetails.setBackground(SystemColor.activeCaption);
txtEnterYourDetails.setFont(new Font("Tahoma", Font.PLAIN, 20));
txtEnterYourDetails.setText("Enter your details");
txtEnterYourDetails.setBounds(176, 74, 173, 36);
frame.getContentPane().add(txtEnterYourDetails);
txtEnterYourDetails.setColumns(10);
txtName = new JTextField();
txtName.setBackground(SystemColor.inactiveCaption);
txtName.setText("Name");
txtName.setBounds(93, 136, 86, 20);
frame.getContentPane().add(txtName);
txtName.setColumns(10);
txtAge = new JTextField();
txtAge.setBackground(SystemColor.inactiveCaption);
txtAge.setText("Age");
txtAge.setBounds(93, 167, 86, 20);
frame.getContentPane().add(txtAge);
txtAge.setColumns(10);
txtPhoneNo = new JTextField();
txtPhoneNo.setBackground(SystemColor.inactiveCaption);
txtPhoneNo.setText("Phone No");
txtPhoneNo.setBounds(93, 198, 86, 20);
frame.getContentPane().add(txtPhoneNo);
txtPhoneNo.setColumns(10);
txtMailId = new JTextField();
txtMailId.setBackground(SystemColor.inactiveCaption);
txtMailId.setText("Mail id");
txtMailId.setBounds(93, 229, 86, 20);
frame.getContentPane().add(txtMailId);
txtMailId.setColumns(10);
JTextArea textArea = new JTextArea();
textArea.setBounds(236, 134, 124, 20);
frame.getContentPane().add(textArea);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBounds(236, 165, 124, 20);
frame.getContentPane().add(textArea_1);
JTextArea textArea_2 = new JTextArea();
textArea_2.setBounds(236, 196, 124, 20);
frame.getContentPane().add(textArea_2);
JTextArea textArea_3 = new JTextArea();
textArea_3.setBounds(236, 227, 124, 20);
frame.getContentPane().add(textArea_3);
txtStart = new JTextField();
txtStart.setBackground(SystemColor.inactiveCaption);
txtStart.setText("Start ");
txtStart.setBounds(93, 260, 86, 20);
frame.getContentPane().add(txtStart);
txtStart.setColumns(10);
txtDestination = new JTextField();
txtDestination.setBackground(SystemColor.inactiveCaption);
txtDestination.setText("Destination");
txtDestination.setBounds(93, 291, 86, 20);
frame.getContentPane().add(txtDestination);
txtDestination.setColumns(10);
textArea_4 = new JTextArea();
textArea_4.setBounds(236, 258, 124, 20);
frame.getContentPane().add(textArea_4);
textArea_5 = new JTextArea();
textArea_5.setBounds(236, 289, 124, 20);
frame.getContentPane().add(textArea_5);
JPanel panel = new JPanel();
panel.setForeground(new Color(0, 0, 0));
panel.setBackground(SystemColor.inactiveCaption);
panel.setBounds(433, 213, 124, 115);
frame.getContentPane().add(panel);
txtEn = new JTextField();
txtEn.setBackground(SystemColor.inactiveCaption);
txtEn.setText("Enter Bus No");
txtEn.setBounds(93, 322, 86, 20);
frame.getContentPane().add(txtEn);
txtEn.setColumns(10);
JTextArea textArea_6 = new JTextArea();
textArea_6.setBounds(236, 320, 124, 20);
frame.getContentPane().add(textArea_6);
JButton btnBookTicket = new JButton("Book Ticket");
btnBookTicket.setBackground(SystemColor.activeCaption);
btnBookTicket.setBounds(176, 376, 116, 23);
frame.getContentPane().add(btnBookTicket);
JPopupMenu popupMenu_1 = new JPopupMenu();
popupMenu_1.setBounds(327, 376, 200, 50);
frame.getContentPane().add(popupMenu_1);
if(!textArea.isEnabled()||!textArea_2.isEnabled()||!textArea_3.isEnabled()||!textArea_3.isEnabled()||!textArea_4.isEnabled()||!textArea_5.isEnabled()||!textArea_6.isEnabled())
{
JOptionPane.showMessageDialog(null, "You have not entered a few details","UnSuccessful",
JOptionPane.INFORMATION_MESSAGE);
}
if(btnBookTicket.isEnabled())
{
btnBookTicket.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "You have booked successfully","Success",
JOptionPane.INFORMATION_MESSAGE);
JButton button = (JButton) e.getSource();
if (button == btnBookTicket)
{
frame2 = new JFrame("FRAME 2");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setLocationByPlatform(true);
JPanel contentPane2 = new JPanel();
contentPane2.setBackground(Color.DARK_GRAY);
JButton btnBookTicket1 = new JButton("Cancel Ticket");
btnBookTicket1.setBackground(SystemColor.activeCaption);
btnBookTicket1.setBounds(0, 30, 35, 23);
contentPane2.add(btnBookTicket1);
frame2.getContentPane().add(contentPane2);
frame2.setSize(600, 600);
frame2.setVisible(true);
frame.setVisible(false);
}
btnBookTicket.addActionListener(action);
}
});
}
}
}
Why is my GUI not showing any buttons, labels, or text fields?
I think I have it all setup, but when I run it, only the frame shows, and none of the contents appear.
package BasicGame;
import java.awt.Container;
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.JTextField;
import javax.swing.SwingConstants;
public class Gui extends JFrame{
private static final long serialVersionUID = 1L;
private JLabel label;
private JTextField textField;
private JButton button;
private buttonHandler bHandler;
public Gui(){
setTitle("Basic Gui");
setResizable(false);
setSize(500, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(null);
button = new JButton("button");
button.setBounds(50, 60, 50, 70);
bHandler = new buttonHandler();
button.addActionListener(bHandler);
label = new JLabel("Hello", SwingConstants.RIGHT);
label.setBounds(50, 60, 50, 70);
textField = new JTextField(10);
textField.setBounds(50, 60, 50, 70);
pane.add(button);
pane.add(label);
pane.add(textField);
}
public class buttonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
#SuppressWarnings("unused")
public static void main(String[] args){
Gui gui = new Gui();
}
}
Move your setVisible() to the end of the constructor. You are adding all of your components after you set your JFrame up and make is visible, so you don't see any of the changes.
This should show your JFrame with all the components:
public Gui(){
setTitle("Basic Gui");
setResizable(false);
setSize(500, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(null);
button = new JButton("button");
button.setBounds(50, 60, 50, 70);
bHandler = new buttonHandler();
button.addActionListener(bHandler);
label = new JLabel("Hello", SwingConstants.RIGHT);
label.setBounds(50, 60, 50, 70);
textField = new JTextField(10);
textField.setBounds(50, 60, 50, 70);
pane.add(button);
pane.add(label);
pane.add(textField);
setVisible(true); // Move it to here
}
Here's what the frame's looked like before and after I moved the setVisible statement and compiled your code.
Before:
After:
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