Adding components by constructor - java

I have problem with adding Label by constructor, when I make it by method it's no problem
private void addLabel() {
System.out.println("asd");
JLabel label = new JLabel("asd");
label.setBounds(10, 40, 100, 25);
add(label);
repaint();
validate();
System.out.println("asd2");
}
But when i try to do this same by new class and constructor i doesn't work...
Main frame:
public class Frame extends JFrame {
JButton button = new JButton("new");
AddButton button2 = new AddButton();
public Frame() {
setLayout(null);
setSize(400, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
button.setBounds(40, 10, 50, 25);
add(button);
button2.setBounds(40, 40, 100, 25);
add(button);
}
public static void main(String[] args) {
Frame ap = new Frame();
ap.setVisible(true);
}
AddButton class:
public class AddButton extends JPanel {
JLabel label = new JLabel("asd");
public AddButton() {
label.setBounds(10, 40, 100, 25);
add(label);
repaint();
validate();
}
}
Ok i got it, I tried to add "button" two times istead button and button2 :D

Your constructor doesn't make sense, that's not how you should use constructors - constructors are used to create an instance of a class.
When you write
AddButton button2 = new AddButton();
then button2 is of type AddButton, and add doesn't accept this type of object.

You can Edit like this
public class AddButton extends JPanel {
JLabel label;
public AddButton() {
label=new JLabel("asd");
label.setBounds(10, 40, 100, 25);
add(label);
repaint();
validate();
}
}

Related

Change the properties of a JPanel component through an action event

I'm trying to setup a system where when I press a button the JLabel text will change, but I can't seem to make it work. I've already tested that the action listener works by doing 'system.out.println("test");'. It works fine, but when trying to change a JComponent text it doesn't work. I've already searched for answers and found nothing that works.
Main class:
package com.fcs.app;
public class A {
public static void main(String args[]) {
window w = new window();
w.drawWindow();
}
}
JFrame and JPanel class:
package com.fcs.app;
import java.awt.*;
import javax.swing.*;
public class window extends JPanel {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JButton b1 = new JButton();
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JLabel plus = new JLabel();
JLabel equals = new JLabel();
JLabel rt = new JLabel();
int Result = 10;
public void drawWindow() {
//JFrame setup
jf.setSize(400, 400);
jf.setUndecorated(true);
jf.setLayout(null);
jf.setContentPane(jp);
jf.setLocation(100, 100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
//JPanel setup
jp.setSize(400, 400);
jp.setLocation(0, 0);
jp.setBackground(Color.WHITE);
jp.add(b1);
jp.add(tf1);
jp.add(tf2);
jp.add(plus);
jp.add(equals);
jp.add(rt);
jp.setLayout(null);
jp.setVisible(true);
//JButton setup
b1.setFont(new Font("Times", Font.PLAIN, 15));
b1.setText("Calculate!");
b1.setSize(100, 40);
b1.setLocation(150, 350);
b1.addActionListener(new Listener());
b1.setVisible(true);
//TextField 1 setup
tf1.setSize(120, 50);
tf1.setLocation(140, 20);
tf1.setFont(new Font("Times", Font.PLAIN, 25));
tf1.setHorizontalAlignment(JTextField.CENTER);
tf1.setVisible(true);
//TextField 2 setup
tf2.setSize(120, 50);
tf2.setLocation(140, 120);
tf2.setFont(new Font("Times", Font.PLAIN, 25));
tf2.setHorizontalAlignment(JTextField.CENTER);
tf2.setVisible(true);
//Plus sign Setup
plus.setSize(120, 50);
plus.setLocation(140, 70);
plus.setHorizontalAlignment(JLabel.CENTER);
plus.setFont(new Font("Times", Font.PLAIN, 40));
plus.setText("+");
plus.setVisible(true);
//Equals sign Setup
equals.setSize(120, 50);
equals.setLocation(140, 170);
equals.setHorizontalAlignment(JLabel.CENTER);
equals.setFont(new Font("Times", Font.PLAIN, 40));
equals.setText("=");
equals.setVisible(true);
//Result text
rt.setSize(400, 50);
rt.setLocation(0, 250);
rt.setHorizontalAlignment(JLabel.CENTER);
rt.setFont(new Font("Times", Font.PLAIN, 60));
rt.setText("");
rt.setVisible(true);
}
}
ActionListener class:
package com.fcs.app;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Listener implements ActionListener {
window w = new window();
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
w.rt.setText("Test");
}
}
You are creating new references of Window like window w = new window();
it will create a new instance of window and you are trying to change newly created window.
Try to pass the window object you have created before in window class or
implement an anonymous ActionListener in the window class.
b1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
rt.setText("Test");
}
}
});

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);
}
}

RE-enable a button after closing a frame java

so im making a program for my project.
and when i clicked a button it must open anohter frame and make the button unclickable. and when you closed the popup frame the button must re enable. so this is
my main frame
package Option2;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainMenu {
int intCtr1 = 0;
JFrame frame1 = new JFrame("EXD LAN PARTY");
JButton Button1 = new JButton();
JButton Button2 = new JButton();
JButton Button3 = new JButton();
JButton Button4 = new JButton();
JLabel Label1 = new JLabel();
public void MainMenu(){
//BUTTON1
Button1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PI2.jpg")));
Button1.setBackground(Color.white);
Button1.setBounds(50, 350, 150, 150);
Button1.setToolTipText("Personal Info");
//BUTTON1 END
//BUTTON2
Button2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PC.jpg")));
Button2.setBackground(Color.white);
Button2.setBounds(250, 350, 150, 150);
Button2.setToolTipText("PC INFO");
//BUTTON2 END
//BUTTON3
Button3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Games.jpg")));
Button3.setBackground(Color.white);
Button3.setBounds(450, 350, 150, 150);
Button3.setToolTipText("Games");
//BUTTON3 END
//BUTTON4 END
Button4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Players.jpg")));
Button4.setBackground(Color.white);
Button4.setBounds(650, 350, 150, 150);
Button3.setToolTipText("Players");
//BUTTON4 END
//LABEL1
Label1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/EXD.jpg")));
Label1.setBounds(50, 50, 800, 250);
//LABEL1 END
//Frame1
frame1.getContentPane().setBackground(Color.black);
frame1.setResizable(false);
frame1.setLayout(null);
frame1.setSize(870,650);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.add(Label1);
frame1.add(Button1);
frame1.add(Button2);
frame1.add(Button3);
frame1.add(Button4);
//Frame1 END
Button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
PersonalInfo objPI = new PersonalInfo();
objPI.Menu1();
Button1.setEnabled(false);
}
});
Button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
PCInfo objPCI = new PCInfo();
objPCI.Menu2();
}
});
Button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
Games objGames = new Games();
objGames.Menu3();
}
});
Button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
Players objPlayers = new Players();
objPlayers.Menu4();
}
});
}
public void dim1(){
if(intCtr1 == 1){
MainMenu objMM = new MainMenu();
objMM.Button1.setEnabled(true);
System.out.println("SD");
}
}
}
**and this is my sub frame**
package Option2;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PersonalInfo {
String[] arrSex = {"Male","Female"};
JFrame frame1 = new JFrame("Personal Info");
JLabel label1 = new JLabel("ID");
JLabel label2 = new JLabel("Last Name");
JLabel label3 = new JLabel("First Name");
JLabel label4 = new JLabel("Middle Name");
JLabel label5 = new JLabel("SEX");
JLabel label6 = new JLabel();
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JTextField tf3 = new JTextField();
JTextField tf4 = new JTextField();
JComboBox CB1 = new JComboBox(arrSex);
JButton Button1 = new JButton("NEW");
JButton Button2 = new JButton("SAVE");
JButton Button3 = new JButton("EDIT");
JButton Button4 = new JButton("CANCEL");
JButton Button5 = new JButton();
JButton Button6 = new JButton();
JButton Button7 = new JButton();
JButton Button8 = new JButton();
public void Menu1(){
//Frame1
frame1.add(label6);
frame1.add(label1);
frame1.add(tf1);
frame1.add(label2);
frame1.add(tf2);
frame1.add(label3);
frame1.add(tf3);
frame1.add(label4);
frame1.add(tf4);
frame1.add(label5);
frame1.add(CB1);
frame1.add(Button1);
frame1.add(Button2);
frame1.add(Button3);
frame1.add(Button4);
frame1.add(Button5);
frame1.add(Button6);
frame1.add(Button7);
frame1.add(Button8);
frame1.setVisible(true);
frame1.getContentPane().setBackground(Color.black);
frame1.setSize(600,600);
frame1.setResizable(false);
frame1.setLayout(null);
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
MainMenu objMM = new MainMenu();
objMM.intCtr1=1;
objMM.dim1();
}
});
//Frame1 End
//LABEL6
label6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PI.jpg")));
label6.setBounds(30, 5, 800, 250);
//LABEL6 END
//LABEl1
label1.setBounds(100, 220, 50,50);
label1.setForeground(Color.white);
label1.setFont(new Font("Serif", Font.BOLD, 18));
//Label1 end
//Tf
tf1.setBounds(130, 230, 400,30);
tf1.setEnabled(false);
SmartC objSMC = new SmartC();
tf1.setText(objSMC.SmartCounter("ABC123415XYZS"));
//tf end
//label2
label2.setBounds(35, 255, 120,50);
label2.setForeground(Color.white);
label2.setFont(new Font("Serif", Font.BOLD, 18));
//label2 end
//Tf2
tf2.setBounds(130, 270, 400,30);
//tf2 end
//label3
label3.setBounds(35, 295, 120,50);
label3.setForeground(Color.white);
label3.setFont(new Font("Serif", Font.BOLD, 18));
//label3 end
//Tf3
tf3.setBounds(130, 310 , 400, 30);
//tf3 end
//label4
label4.setBounds(15, 335, 120,50);
label4.setForeground(Color.white);
label4.setFont(new Font("Serif", Font.BOLD, 18));
//label4 end
//Tf4
tf4.setBounds(130, 350 , 400, 30);
//tf4 end
//label4
label5.setBounds(85, 375, 120,50);
label5.setForeground(Color.white);
label5.setFont(new Font("Serif", Font.BOLD, 18));
//label4 end
//cb1
CB1.setBounds(130, 390, 100, 30);
CB1.setBackground(Color.white);
//cb1 end
//button1
Button1.setBounds(35, 450, 100, 30);
Button1.setBackground(Color.white);
//
//
Button2.setBounds(150, 450, 100, 30);
Button2.setBackground(Color.white);
//
//
Button3.setBounds(335, 450, 100, 30);
Button3.setBackground(Color.white);
//
//
Button4.setBounds(450, 450, 100, 30);
Button4.setBackground(Color.white);
//
//
Button5.setBounds(35, 500, 100, 50);
Button5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/First.jpg")));
Button5.setBackground(Color.white);
//
//
Button6.setBounds(150, 500, 100, 50);
Button6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Previous.jpg")));
Button6.setBackground(Color.white);
//
//
Button7.setBounds(335, 500, 100, 50);
Button7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Next.jpg")));
Button7.setBackground(Color.white);
//
//
Button8.setBounds(450, 500, 100, 50);
Button8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Last.jpg")));
Button8.setBackground(Color.white);
//
//
}
}
Any Suggestions about my coding is accepted. Sorry Still learning about Java :)
and when i clicked a button it must open anohter frame
You should NOT be creating another JFrame.
Instead you should be creating a modal JDialog. The dialog will not allow you to click on the frame until the dialog is closed.
Any Suggestions about my coding is accepted
Follow Java naming conventions. Variable names should NOT start with an upper case character. Sometimes you follow this guideline and sometimes you don't. Be consistent!
Don't use setBounds(...). Swing was designed to be used with layout managers!
First, please read Java naming conventions.
You need to pass the reference of MainMenu to PersonalInfo in order to achieve what you need, here:
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
PersonalInfo objPI = new PersonalInfo(this);
objPI.menu1();
button1.setEnabled(false);
}
});
And you need to add a constructor to PersonalInfo:
private MainMenu m;
public PersonalInfo(MainMenu m) {
this.m = m;
}
Add a public method to MainMenu:
public void enableMyButton() {
button1.setEnabled(true);
}
Now you can add an event listener to PersonalInfo to enable the button of the MainMenu frame:
frame1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
this.m.enableMyButton();
}
});

Java Panel Not Displaying

public class Test extends JFrame
{
private static final long serialVersionUID = 1L;
public static void main(String [] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
MyPanel p = new MyPanel();
p.setVisible(true);
}
});
}
}
The Panel code is dictates how this MyPanel should look.
public class MyPanel extends JPanel
{
private static final long serialVersionUID = 1L;
private JTextField txtUsername;
public MyPanel()
{
setLayout(null);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
panel.setBackground(SystemColor.control);
panel.setBounds(0, 0, 500, 500);
add(panel);
ImageIcon icon= new ImageIcon("C:/Users/Admin/Desktop/testPic.jpg");
JLabel wlabel = new JLabel(icon);
wlabel.setBounds(20, 10, 400, 222);
panel.add(wlabel);
JPanel panel_1 = new JPanel();
panel_1.setBounds(36, 244, 614, 159);
panel.add(panel_1);
panel_1.setLayout(null);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(40, 40, 100, 20);
panel_1.add(lblUsername);
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 18));
txtUsername = new JTextField();
txtUsername.setBounds(179, 52, 195, 30);
panel_1.add(txtUsername);
txtUsername.setColumns(10);
JButton btnSubmit = new JButton("SUBMIT");
btnSubmit.setBounds(424, 65, 145, 44);
panel_1.add(btnSubmit);
btnSubmit.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnSubmit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
}
});
}
}
Why don't I see the actual panel? The code compiles and runs but I don't see anything on my screen.
You've got to add your JPanel to your JFrame. It's the JFrame that is the top level window that displays the entire GUI. Without a top level window either created directly, as described above, or indirectly, such as when you create a JOptionPane, the JPanel will never be seen.
So, rather than this:
public void run()
{
MyPanel p = new MyPanel();
p.setVisible(true);
}
Do this:
public void run()
{
Test test = new Test();
test.setVisible(true);
}
And then create your MyPanel in the Test constructor, and add it to Test there via a call to add(...).
Next we'll talk about why null layouts and setBounds(...) is a very bad thing.
Key tutorial links:
Using Swing Components
Using Top Level Windows
Laying Out Components in a Container

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