JFrame not displaying, nothing is displaying - java

I have no idea why my JFrame isn't displaying and its really bugging me and i feel like I'm missing something easy and I'm just not seeing it. So any help would be awesome!
here is the code:
import java.awt.*;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.GroupLayout.*;
public class Lorenzo_ChatClient_class extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Lorenzo_ChatClient_class frame = new Lorenzo_ChatClient_class();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/** Create the frame. */
public Lorenzo_ChatClient_class() {
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JFrame frame = new JFrame();
frame.setSize(100,100);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField_1 = new JTextField();
textField_1.setBounds(0, 244, 450, 34);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton = new JButton("Send");
btnNewButton.setBounds(351, 6, 99, 122);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Quit");
btnNewButton_1.setBounds(351, 124, 99, 122);
contentPane.add(btnNewButton_1);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBorder(UIManager.getBorder("EditorPane.border"));
textArea_1.setBounds(6, 6, 345, 240);
contentPane.add(textArea_1);
JTextArea textArea = new JTextArea();
textField = new JTextField();
textField.setColumns(10);
}
}

In your main method ,
Lorenzo_ChatClient_class frame = new Lorenzo_ChatClient_class();
In your constructor,
setBounds(100, 100, 450, 300);
JFrame frame = new JFrame();
frame.setSize(100,100);
You have taken class reference and JFrame names are same "frame".

So apparently I read over your code too quickly, because you do have Lorenzo_ChatClient_class extending JFrame. That changes the solution drastically.
I think your main method looks fine. You shouldn't be calling overridable methods in a constructor; it leads to a lot of bugs. Try changing the code to this:
import java.awt.*;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.GroupLayout.*;
import javax.swing.border.*;
class Lorenzo_ChatClient_class extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Lorenzo_ChatClient_class frame = new Lorenzo_ChatClient_class();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Lorenzo_ChatClient_class() {
super();
frameSetup();
setVisible(true); // or just keep it in your main method
}
private void frameSetup() {
this.setBounds(100, 100, 450, 300);
this.setSize(100, 100);
this.contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField_1 = new JTextField();
textField_1.setBounds(0, 244, 450, 34);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton = new JButton("Send");
btnNewButton.setBounds(351, 6, 99, 122);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Quit");
btnNewButton_1.setBounds(351, 124, 99, 122);
contentPane.add(btnNewButton_1);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBorder(UIManager.getBorder("EditorPane.border"));
textArea_1.setBounds(6, 6, 345, 240);
contentPane.add(textArea_1);
JTextArea textArea = new JTextArea();
textField = new JTextField();
textField.setColumns(10);
}
}
I just tried it out and this works for me, although it's a small frame. Not sure what your reasoning for having setbounds(100, 100, 450, 300) followed immediately by setSize(100,100). You're setting the size to 450x300, and then immediately resetting the size to 100x100. I deleted the setSize(100, 100) line and the JFrame opened up a lot nicer.

Related

Java - Unable to open a new frame from ActionListener

I am currently designing a login screen, but I ran into a strange issue. I already designed my GUI with the help of swing, and it was time to make functional buttons. I wanted to test my login button and if it would take me to the frame I want, but it is unable to. I can set a JOptionPane.showMessageDialog for example, which works just fine, but I am unable to open another frame from the button. I tried with New JFrameName().setVisible(true), and also JFrameName test = new JFrameName(); test.setVisible(true);, but the methods show up in red. Here is my code.
package com.edu4java.swing.tutrial3;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginView {
public static void main(String[] args) {
JFrame frame = new JFrame("Bus Tour Booking System");
frame.setSize(300, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel titleLabel = new JLabel("Bus Tour Booking System");
titleLabel.setBounds(70,15,150,25);
panel.add(titleLabel);
JLabel userLabel = new JLabel("Username: ");
userLabel.setBounds(30, 50, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(120, 50, 130, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password: ");
passwordLabel.setBounds(30, 80, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(120, 80, 130, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(100, 125, 80, 25);
panel.add(loginButton);
ActionListener myButtonListener = new MyButtonListener();
loginButton.addActionListener(myButtonListener);
}
private static class MyButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
//can't access a new frame from here :(
}
}
}
I would be very grateful if someone could help me out, I read a lot on Stackoverflow and Reddit, but just can't find the solution. I am also new to Java, so that doesn't help a lot either :D. Thanks in advance!
P.S. As far as the actual functionality for the login screen, I am going to do that in a later stage.
This is your login class. I put the JFrame frame in the global scope, so you can manipulate it from the ButtonListener method. I also created a SomeFrame class, just to demonstrate the new JFrame that would be created when you click the button. When an action is performed(the button is clicked) a new object of SomeFrame is created. Since SomeFrame extends JFrame we can use the method setVisible() to a SomeFrame object. The SomeFrame frame appears and the LoginView is no longer visible.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginView {
public static JFrame frame = new JFrame("Bus Tour Booking System");
public static void main(String[] args) {
frame.setSize(300, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel titleLabel = new JLabel("Bus Tour Booking System");
titleLabel.setBounds(70,15,150,25);
panel.add(titleLabel);
JLabel userLabel = new JLabel("Username: ");
userLabel.setBounds(30, 50, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(120, 50, 130, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password: ");
passwordLabel.setBounds(30, 80, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(120, 80, 130, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(100, 125, 80, 25);
panel.add(loginButton);
ActionListener myButtonListener = new MyButtonListener();
loginButton.addActionListener(myButtonListener);
}
private static class MyButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
SomeFrame newFrame = new SomeFrame();
newFrame.setVisible(true);
frame.setVisible(false);
}
}
}
This is the SomeFrame class.
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SomeFrame extends JFrame {
public SomeFrame(){
super("something");
this.setSize(300, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
this.add(panel);
this.setVisible(true);
}
}

Scrolling Text Pane JFrame Java

I'm trying to create a sort of log of all the keys hit, at the moment I just need to figure out how to either:
Link the position of the "text" to the scroll bar to the right
OR
Add a different component which is suited better to hold large amounts of multiple line text.
What am I doing wrong here? Thanks!
public class MacroMakerGui extends JFrame {
public static final long serialVersionUID = 1L;
public static JPanel contentPane;
public static JTextField textField = new JTextField();;
public static MacroKeyListener keylistener = new MacroKeyListener(textField);
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MacroMakerGui frame = new MacroMakerGui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MacroMakerGui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 126, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JButton btnNewButton = new JButton("Record Macro");
btnNewButton.setBounds(10, 220, 99, 30);
contentPane.add(btnNewButton, null);
textField.setBounds(10, 189, 99, 20);
contentPane.add(textField);
textField.setColumns(10);
JEditorPane editorPane = new JEditorPane();
editorPane.setBounds(10, 11, 84, 153);
contentPane.add(editorPane);
JScrollBar scrollBar = new JScrollBar();
scrollBar.setBounds(93, 11, 17, 153);
contentPane.add(scrollBar);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnNewButton.addKeyListener(keylistener);
}
});
}
}
Instead of JScrollBar, use JScrollPanel. Add that to the contentPane, and add your editorPane as a chiled of the JScrollPanel.

How to layout this GUI?

I am creating a GUI in java. Currently i have an empty JFrame and am trying to add a JPanel to it. The JPanel contains buttons, text etc. However none of this is being properly displayed. My code is as follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class memoDisplayUI {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea jTextBox = new JTextArea();
JScrollPane scroll = new JScrollPane();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
memoDisplayUI frame = new memoDisplayUI();
frame.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public memoDisplayUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame.getContentPane().setBackground(new Color(255, 255, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 270, 400);
frame.setUndecorated(true); //REMOVES MENU BAR
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblMemos = new JLabel("MEMOS");
lblMemos.setForeground(new Color(100, 149, 237));
lblMemos.setFont(new Font("Moire", Font.BOLD, 30));
lblMemos.setBounds(16, 16, 234, 37);
panel.add(lblMemos);
JButton button = new JButton("");
button.setBackground(new Color(100, 149, 237));
button.setBounds(7, 350, 40, 40);
panel.add(button);
button.setIcon(new ImageIcon("back.png"));
JButton button_1 = new JButton("");
button_1.setBackground(new Color(100, 149, 237));
button_1.setBounds(113, 350, 40, 40);
panel.add(button_1);
button_1.setIcon(new ImageIcon("Edit.png"));
JButton button_2 = new JButton("");
button_2.setBackground(new Color(100, 149, 237));
button_2.setBounds(220, 350, 40, 40);
panel.add(button_2);
button_2.setIcon(new ImageIcon("memo.png"));
JButton btnExit = new JButton("");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btnExit.setBorder(null);
btnExit.setIcon(new ImageIcon("Exit.jpg"));
btnExit.setBounds(216, 19, 40, 40);
panel.add(btnExit);
jTextBox = new JTextArea();
scroll.setViewportView(jTextBox); // add scroll panel
jTextBox.setTabSize(4);
jTextBox.setLineWrap(true);
jTextBox.setBackground(new Color(192, 192, 192));
jTextBox.setBounds(8, 60, 255, 286);
panel.add(jTextBox);
frame.setContentPane(panel);
}
}
My desired layout is something very similar to this:
Could someone advise as to why this is.
Thanks for any help :)

How to restart the screen with action listener?

I was looking in to my Java project when I realized I have yet to make my title screen. But one problem came to mind: How do I make it in where when they press on the new file button it will clear everything on the screen and put the new stuff in?
In simpler words, how to make a action listener so when they click the button the screen will clear and put a new screen on?
package Main_Config;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class SET_UP extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JLabel consol;
public static Dimension size = new Dimension(800, 700);
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SET_UP frame = new SET_UP();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SET_UP() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(370, 70, 0, 0);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setSize(size);
setLocationRelativeTo(null);
textField = new JTextField();
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = textField.getText();
consol.setText(input);
}
});
textField.setBounds(10, 452, 243, 20);
contentPane.add(textField);
textField.setColumns(10);
JButton enter = new JButton("Enter");
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
enter.setBounds(253, 452, 89, 20);
contentPane.add(enter);
JLabel consol = new JLabel("");
consol.setBounds(0, 483, 335, 189);
contentPane.add(consol);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(352, 451, 200, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button");
btnNewButton_1.setBounds(584, 451, 200, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button");
btnNewButton_2.setBounds(0, 0, 89, 23);
contentPane.add(btnNewButton_2);
}
}
To clear the screen, I do this:
Button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
frame.remove(panelTwo);
frame.remove(panelThree);
frame.add(panelFour);
frame.setVisible(true);
}
});

How should I make a "back button" to switch JPanels on a main JFrame class work with multiple JPanel classes?

I'm currently working on a game for school, and I've hit a brick wall. I've created a main class that sets up the JFrame, and in that JFrame have JPanel buttons that open a server JPanel class, a client JPanel class, and buttons for options, and exiting the game. Now where I'm stuck is how I should make buttons to go back to the main JPanel using a back button on the server/client JPanel class. Here's the code I have at the moment:
MainUI.class (a different class runs this):
public class MainUI extends JFrame {
private JPanel contentPane;
private JTextField textField;
public MainUI() {
// Sets up the frame
setTitle("Pong Legacy | Prototype v0.1.0");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 500);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Starts the Server window
JButton btnStartServer = new JButton("Start Server");
btnStartServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel server = new ServerUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(server);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartServer.setBounds(97, 364, 100, 25);
contentPane.add(btnStartServer);
// Starts the Client window
JButton btnStartClient = new JButton("Start Client");
btnStartClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel client = new ClientUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(client);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartClient.setBounds(97, 400, 100, 25);
contentPane.add(btnStartClient);
// Opens the Options menu
// (To Do)
JButton btnOptions = new JButton("Options");
btnOptions.setBounds(37, 436, 100, 25);
contentPane.add(btnOptions);
// Quits the game
JButton btnQuitGame = new JButton("Quit Game");
btnQuitGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnQuitGame.setBounds(157, 436, 100, 25);
contentPane.add(btnQuitGame);
// Username Field
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(121, 45, 52, 14);
contentPane.add(lblUsername);
textField = new JTextField();
textField.setBounds(104, 67, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
}
}
ServerUI.class:
public class MainUI extends JFrame {
private JPanel contentPane;
private JTextField textField;
public MainUI() {
// Sets up the frame
setTitle("Pong Legacy | Prototype v0.1.0");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 500);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Starts the Server window
JButton btnStartServer = new JButton("Start Server");
btnStartServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel server = new ServerUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(server);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartServer.setBounds(97, 364, 100, 25);
contentPane.add(btnStartServer);
// Starts the Client window
JButton btnStartClient = new JButton("Start Client");
btnStartClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel client = new ClientUI();
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(client);
getContentPane().validate();
getContentPane().repaint();
}
});
btnStartClient.setBounds(97, 400, 100, 25);
contentPane.add(btnStartClient);
// Opens the Options menu
// (To Do)
JButton btnOptions = new JButton("Options");
btnOptions.setBounds(37, 436, 100, 25);
contentPane.add(btnOptions);
// Quits the game
JButton btnQuitGame = new JButton("Quit Game");
btnQuitGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnQuitGame.setBounds(157, 436, 100, 25);
contentPane.add(btnQuitGame);
// Username Field
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(121, 45, 52, 14);
contentPane.add(lblUsername);
textField = new JTextField();
textField.setBounds(104, 67, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
}
}
ClientUI.class:
public class ClientUI extends JPanel {
private JTextField textField;
public ClientUI() {
setLayout(null);
JButton btnConnect = new JButton("Connect");
btnConnect.setBounds(47, 400, 200, 25);
add(btnConnect);
JButton btnBack = new JButton("Back");
btnBack.setBounds(117, 436, 60, 25);
add(btnBack);
JRadioButton rdbtnSelectAServer = new JRadioButton("Select a server from the list:");
rdbtnSelectAServer.setBounds(66, 25, 161, 25);
add(rdbtnSelectAServer);
JRadioButton rdbtnManualConnection = new JRadioButton("Manual Connection:");
rdbtnManualConnection.setBounds(87, 325, 120, 25);
add(rdbtnManualConnection);
textField = new JTextField();
textField.setBounds(47, 355, 200, 25);
add(textField);
textField.setColumns(10);
}
}
I've heard that I could use a CardLayout, too, but I want to see if I can do it this way.

Categories