Change the properties of a JPanel component through an action event - java

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

Related

Label and Textfield won't appear

So, we have a group project and we need to make a food ordering system with login and register and I'm tasked with the login layout. I used what I did on my other GUI's but I don't know why it doesn't work here. The userL and the textfield won't show. The imageIcon and it's label "LOGIN" does appear, however.
import java.awt.*;
import java.awt.event.*;
public class OOPproject implements ActionListener{
//declarations
JFrame frame;
JPanel panel;
JTextField userTF, passwordTF;
JLabel loginL, userL, passwordL, createL;
JButton loginB;
//layout
OOPproject(){
frame = new JFrame();
frame.setSize(400,400);
frame.setLayout(null);
frame.getContentPane().setBackground(Color.WHITE);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon("11.png");
loginL = new JLabel("L O G I N");
loginL.setIcon(image);
loginL.setHorizontalTextPosition(JLabel.CENTER);
loginL.setVerticalTextPosition(JLabel.BOTTOM);
loginL.setForeground(new Color(0,191,255));
loginL.setFont(new Font("Arial",Font.BOLD,35));
loginL.setIconTextGap(5);
loginL.setVerticalAlignment(JLabel.CENTER);
loginL.setHorizontalAlignment(JLabel.CENTER);
loginL.setBounds(-8, -110, 400, 400);
frame.add(loginL);
userL = new JLabel("Username");
userL.setBounds(-16, -200, 12, 12);
userL.setVerticalAlignment(JLabel.CENTER);
userL.setHorizontalAlignment(JLabel.CENTER);
userL.setFont(new Font("Arial",Font.BOLD,12));
userL.setForeground(Color.LIGHT_GRAY);
frame.add(userL);
userTF = new JTextField();
userTF.setBackground(Color.white);
userTF.setHorizontalAlignment(JLabel.CENTER);
frame.add(userTF);
frame.setVisible(true);
}
public static void main(String[] args) {
OOPproject proj = new OOPproject();
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}

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

Java swing JTextField dissapears after adding a button to BorderLayout.SOUTH [duplicate]

This question already has an answer here:
Java aligning components in panels
(1 answer)
Closed 6 years ago.
I add a JTextField to my game in the bottom left corner using a nested BorderLayout inside my main panel's BorderLayout.SOUTH. This works fine, but then when I add a button to go right next to it, my JTextField dissapears. Can someone please help?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BlackjackGUI{
private JFrame frame;
private JPanel panel, panelLeft, panelBottom;
private JButton newGameBtn, dealBtn, hitBtn, standBtn;
private JLabel placeBetLbl, playerMoneyLbl;
private JLabel playerCard1Lbl, playerCard2Lbl, playerCard3Lbl,
playerCard4Lbl, playerCard5Lbl, playerCard6Lbl, playerCard7Lbl;
private JLabel dealerCard1Lbl, dealerCard2Lbl, dealerCard3Lbl, dealerCard4Lbl,
dealerCard5Lbl, dealerCard6Lbl, dealerCard7Lbl;
private JLabel playerCardValueLbl, dealerCardValueLbl;
private JLabel spacer1, spacer2;
private JTextField betInputBox;
public BlackjackGUI(){
createForm();
addTextField();
addButtons();
addLabels();
frame.add(panel);
frame.setVisible(true);
}
public void createForm() {
frame = new JFrame("Blackjack");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200,800);
panel = new JPanel();
panel.setLayout(new BorderLayout());
Color c = new Color(0, 100, 0);
panel.setBackground(c);
panelLeft = new JPanel();
Color panelLeftBG = new Color (23, 25, 100);
panelLeft.setBackground(panelLeftBG);
panel.add(panelLeft, BorderLayout.WEST);
panelBottom = new JPanel();
Color panelBottomBG = new Color (56, 12, 10);
panelBottom.setBackground(panelBottomBG);
panelBottom.setLayout(new BorderLayout());
panel.add(panelBottom, BorderLayout.SOUTH);
}
public void addButtons() {
newGameBtn = new JButton("New Game");
panelLeft.add(newGameBtn, BorderLayout.WEST);
newGameBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
dealBtn = new JButton("Deal");
dealBtn.setPreferredSize(new Dimension (100, 50));
panelBottom.add(dealBtn, BorderLayout.WEST);
newGameBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
}
public void addTextField() {
betInputBox = new JTextField("£25.00");
betInputBox.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
betInputBox.setHorizontalAlignment(SwingConstants.RIGHT);
betInputBox.setPreferredSize(new Dimension(175,50));
panelBottom.add(betInputBox, BorderLayout.WEST);
}
public void addLabels() {
placeBetLbl = new JLabel("Place your bets!");
placeBetLbl.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
panelBottom.add(placeBetLbl);
playerMoneyLbl = new JLabel("£2,500");
playerMoneyLbl.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
panelBottom.add(playerMoneyLbl, BorderLayout.EAST);
}
public static void main(String[] args) {
new BlackjackGUI();
}
}
Excerpt from the BorderLayout javadoc:
Each region may contain no more than one component, and is identified
by a corresponding constant: NORTH, SOUTH, EAST, WEST, and
CENTER.
Your are first adding the text field and then the button to the same region (WEST), thus button just replaces the text field.
To solve the issue you can use FlowLayout for the panelBottom:
panelBottom.setLayout(new FlowLayout(FlowLayout.LEFT));

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

Categories