Java GUI Programming close the current Frame - java

guys I am new to java programming now I have to deal with GUI programming. I have this simple program for admin to create player for now. I have a view class to show the menus, so when user click on create player the interface will be shown.
then within that menu I have a actionListener which goes to another method and show the pop up menu which then show player created successfully. Now the problem is how can I re show the main menu, and not the create player menu.
There will be a few functions for the admin to deal with but now I just have to finish the first function and the rest will be similar. When clicking on the main menu(create player button) it will go to another function(JFrame) to ask for input, again go to another function to getText from the textfield and save it to file
my codes for tpublic void show() {
JFrame frame = new JFrame("Admin");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(300,300));
panel1 = new JPanel(new GridLayout(6,1));
text = new JLabel("Admin Main Menu");
text.setFont(new Font("Lucida",Font.PLAIN,24));
//set panel layout (rows,cols,hgap,vgap)
panel1.setLayout(new GridLayout(0,1,10,10));
button1 = new JButton("Create a player");
button2 = new JButton("Delete a player");
button3 = new JButton("Top up Player's Chips");
button4 = new JButton("Reset Player's password");
button5 = new JButton("Change admin's password");
button6 = new JButton("Logout");
mainPanel.add(text);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
mainPanel.add(panel1);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
button1.addActionListener(new createPlayerListener());
button1 will got to create player menu
private class createPlayerListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent event)
{
JFrame frame1 = new JFrame("Admin");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text = new JLabel("Create a Player");
text.setFont(new Font("Lucida",Font.PLAIN,24));
mainPanel = new JPanel(new GridLayout(3,1));
mainPanel.setPreferredSize(new Dimension(500,400));
panel1 = new JPanel();
//panel1.setPreferredSize(new Dimension(300,200));
panel1.setLayout(new GridLayout(4,2,10,10));
name = new JLabel("Enter new player name:");
nameTextfield = new JTextField();
pw = new JLabel("Enter new player password:");
pwTextfield = new JTextField();
chip = new JLabel("Enter new player chips:");
chipTextfield = new JTextField();
button1 = new JButton("Create Player");
mainPanel.add(text);
panel1.add(name);
panel1.add(nameTextfield);
panel1.add(pw);
panel1.add(pwTextfield);
panel1.add(chip);
panel1.add(chipTextfield);
mainPanel.add(panel1);
mainPanel.add(button1);
frame1.add(mainPanel);
frame1.pack();
frame1.setVisible(true);
button1.addActionListener(new playerListener());
}
}
private class playerListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent event)
{
String name = nameTextfield.getText();
String pw = pwTextfield.getText();
String chip = chipTextfield.getText();
int chips = Integer.parseInt(chip);
//System.out.println(text);
controller.createPlayer(name, pw, chips);
//pop up window player created
JFrame frame2 = new JFrame();
JOptionPane.showMessageDialog(frame2, "Player Created Successfully!");
}
}

Two issues with your code, creating mainPanel twice, you can do so if you define it locally not globally, otherwise change the names to mainPanel1 and mainPanel2, the second issue is you are calling frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); which closes the application once you close frame1. I tried this code and it works, see the changes I made to it,
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(300,300));
JPanel panel1 = new JPanel(new GridLayout(6,1));
JLabel text = new JLabel("Admin Main Menu");
text.setFont(new Font("Lucida",Font.PLAIN,24));
//set panel layout (rows,cols,hgap,vgap)
panel1.setLayout(new GridLayout(0,1,10,10));
JButton button1 = new JButton("Create a player");
JButton button2 = new JButton("Delete a player");
JButton button3 = new JButton("Top up Player's Chips");
JButton button4 = new JButton("Reset Player's password");
JButton button5 = new JButton("Change admin's password");
JButton button6 = new JButton("Logout");
mainPanel.add(text);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
mainPanel.add(panel1);
add(mainPanel, BorderLayout.CENTER);
button1.addActionListener(new createPlayerListener());
Here is your createPlayerListner modified, the playerListner stays the same,
private class createPlayerListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent event)
{
JFrame frame1 = new JFrame("Admin");
JLabel text = new JLabel("Create a Player");
text.setFont(new Font("Lucida",Font.PLAIN,24));
JPanel mainPanel = new JPanel(new GridLayout(3,1));
mainPanel.setPreferredSize(new Dimension(500,400));
JPanel panel1 = new JPanel();
//panel1.setPreferredSize(new Dimension(300,200));
panel1.setLayout(new GridLayout(4,2,10,10));
JLabel name = new JLabel("Enter new player name:");
nameTextfield = new JTextField();
JLabel pw = new JLabel("Enter new player password:");
pwTextfield = new JTextField();
JLabel chip = new JLabel("Enter new player chips:");
chipTextfield = new JTextField();
JButton button1 = new JButton("Create Player");
mainPanel.add(text);
panel1.add(name);
panel1.add(nameTextfield);
panel1.add(pw);
panel1.add(pwTextfield);
panel1.add(chip);
panel1.add(chipTextfield);
mainPanel.add(panel1);
mainPanel.add(button1);
frame1.add(mainPanel);
frame1.pack();
frame1.setVisible(true);
button1.addActionListener(new playerListener());
}
}

Related

null pointer error at textfield even though value is entered

I keep getting a null exception error and I cant seem to fix it. I know a null pointer error is usually raised when your trying to use a value that is null but I pass a value to the textfield so Im confused. Looking for feedback:
exact error code: (this is where I add the actionlistner to button bt28)
"AWT-EventQueue-0" java.lang.NullPointerException
at GUI$4.actionPerformed(GUI.java:231) ...etc"
P.s. this class is called in a main class which is why that's not here. I've imported awt, swing and util.
public class GUI{
CardLayout cl=new CardLayout();
private String cardNum, pin;
JLabel header1,header2,header3,header4;
JButton bt01= new JButton("");
//bt01.setBounds(100,150,100,40);
JButton bt02= new JButton("");
JButton bt03= new JButton("");
JButton bt04= new JButton("");
JButton bt05= new JButton("");
JButton bt06= new JButton("");
JButton bt07= new JButton("");
JButton bt08= new JButton("Continue");
JButton bt21= new JButton("");
JButton bt22= new JButton("");
JButton bt23= new JButton("");
JButton bt24= new JButton("");
JButton bt25= new JButton("");
JButton bt26= new JButton("");
JButton bt27= new JButton("");
JButton bt28= new JButton("Continue");
JButton bt31= new JButton("");
JButton bt32= new JButton("");
JButton bt33= new JButton("");
JButton bt34= new JButton("");
JButton bt35= new JButton("Balance Query");
JButton bt36= new JButton("Withdraw");
JButton bt37= new JButton("Deposit");
JButton bt38= new JButton("");
JButton bt41= new JButton("A");
JButton bt42= new JButton("B");
JButton bt43= new JButton("C");
JButton bt44= new JButton("D");
JButton bt45= new JButton("E");
JButton bt46= new JButton("");
JButton bt47= new JButton("Business Accounts");
JButton bt48= new JButton("Cancel");
JPanel panelCont=new JPanel();
JPanel GUI1= new JPanel(new BorderLayout());
JPanel GUI2= new JPanel(new BorderLayout());
JPanel GUI3= new JPanel(new BorderLayout());
JPanel GUI4= new JPanel(new BorderLayout());
public GUI(){
JFrame frame1 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
frame1.setPreferredSize (new Dimension(500, 200));
header1= new JLabel("JLCB Automated Banking Machine");
JPanel nPanel1= new JPanel();
nPanel1.add(header1);
GUI1.add(nPanel1,BorderLayout.NORTH);
JLabel cardnumLabel =new JLabel("Enter Card Number");
JTextField txt= new JTextField(15);
txt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cl){
cardNum=txt.getText();
txt.setText(null);
}
});
JPanel cPanel1= new JPanel();
cPanel1.add(cardnumLabel);
cPanel1.add(txt);
GUI1.add(cPanel1, BorderLayout.CENTER);
JPanel wPanel1=new JPanel(new GridLayout(4,0));
wPanel1.add(bt01);
wPanel1.add(bt02);
wPanel1.add(bt03);
wPanel1.add(bt04);
JPanel ePanel1=new JPanel(new GridLayout(4,0));
ePanel1.add(bt05);
ePanel1.add(bt06);
ePanel1.add(bt07);
ePanel1.add(bt08);
GUI1.add(wPanel1,BorderLayout.WEST);
GUI1.add(ePanel1,BorderLayout.EAST);
//................................
// .
// GUI2 .
//................................
JFrame frame2 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
frame2.setPreferredSize (new Dimension(500, 200));
header2= new JLabel("JLCB Automated Banking Machine");
JPanel nPanel2= new JPanel();
nPanel2.add(header1);
GUI2.add(nPanel2,BorderLayout.NORTH);
JLabel pinLabel =new JLabel("Enter your pin");
JTextField txt2= new JTextField(15);
txt2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cl){
pin=txt2.getText();
txt2.setText(null);
}
});
JPanel cPanel2= new JPanel();
cPanel2.add(pinLabel);
cPanel2.add(txt2);
GUI2.add(cPanel2, BorderLayout.CENTER);
JPanel wPanel2=new JPanel(new GridLayout(4,0));
wPanel2.add(bt21);
wPanel2.add(bt22);
wPanel2.add(bt23);
wPanel2.add(bt24);
JPanel ePanel2=new JPanel(new GridLayout(4,0));
ePanel2.add(bt25);
ePanel2.add(bt26);
ePanel2.add(bt27);
ePanel2.add(bt28);
GUI2.add(wPanel2,BorderLayout.WEST);
GUI2.add(ePanel2,BorderLayout.EAST);
//................................
// .
// GUI3 .
//................................
JFrame frame3 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
frame3.setPreferredSize (new Dimension(500, 200));
header3= new JLabel("JLCB Automated Banking Machine");
JPanel nPanel3= new JPanel();
nPanel3.add(header1);
GUI3.add(nPanel3,BorderLayout.NORTH);
JLabel txt3 =new JLabel("Please select from the buttons on the right");
JPanel cPanel3= new JPanel();
cPanel3.add(txt3);
GUI3.add(cPanel3, BorderLayout.CENTER);
JPanel wPanel3=new JPanel(new GridLayout(4,0));
wPanel3.add(bt31);
wPanel3.add(bt32);
wPanel3.add(bt33);
wPanel3.add(bt34);
JPanel ePanel3=new JPanel(new GridLayout(4,0));
ePanel3.add(bt35);
ePanel3.add(bt36);
ePanel3.add(bt37);
ePanel3.add(bt38);
GUI3.add(wPanel3,BorderLayout.WEST);
GUI3.add(ePanel3,BorderLayout.EAST);
//..................................
// .
// GUI4 .
//..................................
JFrame frame4 = new JFrame ("JLCB Automated Banking Machine v 1.0 ");
frame4.setPreferredSize (new Dimension(500, 200));
header4= new JLabel("JLCB Automated Banking Machine");
JPanel nPanel4= new JPanel();
nPanel4.add(header4);
GUI4.add(nPanel4,BorderLayout.NORTH);
JPanel cPanel4= new JPanel();
GUI4.add(cPanel4, BorderLayout.CENTER);
JPanel wPanel4=new JPanel(new GridLayout(4,0));
wPanel4.add(bt41);
wPanel4.add(bt42);
wPanel4.add(bt43);
wPanel4.add(bt44);
JPanel ePanel4=new JPanel(new GridLayout(4,0));
ePanel4.add(bt45);
ePanel4.add(bt46);
ePanel4.add(bt47);
ePanel4.add(bt48);
GUI4.add(wPanel4,BorderLayout.WEST);
GUI4.add(ePanel4,BorderLayout.EAST);
//---------------------------------------------------
panelCont.setLayout(cl);
panelCont.add(GUI1, "1");
panelCont.add(GUI2, "2");
panelCont.add(GUI3, "3");
cl.show(panelCont,"1");
String a ="100001 1111 15984789.74";
bt08.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cl.show(panelCont,"2");
}
});
bt28.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent r){
if (cardNum.equals(a.substring(0, a.indexOf(" "))) && pin.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" ")))){
cl.show(panelCont,"3");
String acc="a";
}
else{
JOptionPane.showMessageDialog(null,"Invalid accoun number or pin");
cl.show(panelCont,"3");
}
}
});
bt35.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cl.show(panelCont,"3");
}
});
bt36.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cl.show(panelCont,"3");
}
});
bt37.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cl.show(panelCont,"3");
}
});
frame1.add(panelCont);
//(JFrame.DO_NOTHING_ON_CLOSE)
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);
}
}
The issue is that your values for cardNum and pin are never assigned in the actionListeners for txt and txt2. You should pull the values in the event that the user clicks the continue button. See my code below.
bt28.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent r) {
cardNum = txt.getText();
pin = txt2.getText();
if (cardNum.equals(a.substring(0, a.indexOf(" "))) && pin.equals(a.substring(a.indexOf(" ") + 1, a.lastIndexOf(" ")))) {
cl.show(panelCont, "3");
String acc = "a";
} else {
JOptionPane.showMessageDialog(null, "Invalid accoun number or pin");
cl.show(panelCont, "3");
}
}
});

access input that get from user in UI in java and show them in console

i want save input that i get from user and save them in element .
i want to access elements that user write in my UI.
and if i want save the elements in array list which kind of array list i should build.
in my UI i have text field name and text field middle name and combo box city has got 3 city name and and a radio box that it depend sex.
in final show them in console what should i do ?
this all of my code:
package ui;
import java.awt.*;
import javax.swing.*;
public class UI extends JFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(500, 600);
BorderLayout blayout = new BorderLayout();
JButton center = new JButton();
JButton north = new JButton();
JButton south = new JButton();
JComboBox combo = new JComboBox();
combo.addItem("-");
combo.addItem("Tehran");
combo.addItem("Tabriz");
combo.addItem("Shiraz");
JRadioButton rb1 = new JRadioButton("man");
JRadioButton rb2 = new JRadioButton("weman");
frame.setLayout(blayout);
FlowLayout fLoyout = new FlowLayout(FlowLayout.CENTER);
center.setLayout(fLoyout);
south.setLayout(fLoyout);
JLabel jb1 = new JLabel("Name :");
JTextField name = new JTextField(20);
center.add(jb1);
center.add(name);
JLabel jb2 = new JLabel("Family :");
JTextField family = new JTextField(20);
center.add(jb2);
center.add(family);
JLabel jb4 = new JLabel("City :");
center.add(jb4);
center.add(combo);
JLabel jb5 = new JLabel("Sex :");
center.add(jb5);
center.add(rb1);
center.add(rb2);
JLabel jb6 = new JLabel("Comment :");
JTextField comment = new JTextField(50);
JLabel jb7 = new JLabel("Save");
south.add(jb7);
JPanel cpanel = new JPanel();
cpanel.add(center);
JPanel spanel = new JPanel();
spanel.add(south);
cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.Y_AXIS));
cpanel.add(jb6);
cpanel.add(comment);
frame.add(cpanel,BorderLayout.CENTER);
frame.add(spanel,BorderLayout.SOUTH);
}
}
You need to use listeners to create code that runs when ui component is pressed. I added listener to every component. try it:
public class UI extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout blayout = new BorderLayout();
JButton center = new JButton();
JButton north = new JButton();
JButton south = new JButton();
south.addActionListener(e->{System.out.println("Save button is pressed");});
JComboBox combo = new JComboBox();
combo.addItem("-");
combo.addItem("Tehran");
combo.addItem("Tabriz");
combo.addItem("Shiraz");
combo.addActionListener(e -> {
System.out.println(((JComboBox<String>) e.getSource()).getSelectedItem());
});
JRadioButton rb1 = new JRadioButton("man");
rb1.addActionListener(e -> {
System.out.println("man: " + ((JRadioButton) e.getSource()).isSelected());
});
JRadioButton rb2 = new JRadioButton("weman");
rb2.addActionListener(e -> {
System.out.println("weman: " + ((JRadioButton) e.getSource()).isSelected());
});
frame.setLayout(blayout);
FlowLayout fLoyout = new FlowLayout(FlowLayout.CENTER);
center.setLayout(fLoyout);
south.setLayout(fLoyout);
JLabel jb1 = new JLabel("Name :");
JTextField name = new JTextField(20);
center.add(jb1);
center.add(name);
name.addActionListener(e -> {
System.out.println("name: " + ((JTextField) e.getSource()).getText());
});
JLabel jb2 = new JLabel("Family :");
JTextField family = new JTextField(20);
center.add(jb2);
center.add(family);
family.addActionListener(e -> {
System.out.println("family: " + ((JTextField) e.getSource()).getText());
});
JLabel jb4 = new JLabel("City :");
center.add(jb4);
center.add(combo);
JLabel jb5 = new JLabel("Sex :");
center.add(jb5);
center.add(rb1);
center.add(rb2);
JLabel jb6 = new JLabel("Comment :");
JTextField comment = new JTextField(50);
comment.addActionListener(e -> {
System.out.println("comment: " + ((JTextField) e.getSource()).getText());
});
JLabel jb7 = new JLabel("Save");
south.add(jb7);
JPanel cpanel = new JPanel();
cpanel.add(center);
JPanel spanel = new JPanel();
spanel.add(south);
cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.Y_AXIS));
cpanel.add(jb6);
cpanel.add(comment);
frame.add(cpanel, BorderLayout.CENTER);
frame.add(spanel, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
You will have to put JRadioButton into RadioGroup to select one of them.
At first, you need to check your GUI, because from skimming it works not good. Later you can get data from the different component using an appropriate listener for each component or you can use any General button to reading data from all components and print it in a console or anywhere else.
To get data from combo, for example, will be here:
String data = combo.getSelectedIndex();
More information you can get here:
How can I get the user input in Java?

Trying to layout JPanels. Java program layout changes every time I run

First time posting so go easy on me.
I am new to Java and am trying to get 3 JPanels to line up on top of each other. The first image is how I want it to look and it does sometimes when I run the program but as you can see by the other images it doesn't line up every time I run it. Sometimes not even showing some of the images/components.
So how can I get three JPanels to line up one after the other vertically?
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class FrameMain {
static final int MY_MINIMUM = 0;
static final int MY_MAXIMUM = 100;
public static void main(String[] args) {
JFrame frame1 = new JFrame("Harvest Frame Test");
frame1.setVisible(true);
frame1.setSize(800,700);
frame1.setResizable(false);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Container Panel
JPanel container = new JPanel();
container.setSize(800,700);
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
frame1.add(container);
//First Panel
JPanel panel1 = new JPanel();
panel1.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel1);
JButton button1 = new JButton("Add Water");
panel1.add(button1);
JButton button2 = new JButton("Add Food");
panel1.add(button2);
JButton button3 = new JButton("Add Medicine");
panel1.add(button3);
ImageIcon image = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel = new JLabel(image);
panel1.add(imagelabel);
JProgressBar pbar = new JProgressBar();
pbar.setMinimum(MY_MINIMUM);
pbar.setMaximum(MY_MAXIMUM);
// add to JPanel
panel1.add(pbar);
// Second Panel
JPanel panel2 = new JPanel();
panel2.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel2);
JButton button4 = new JButton("Add Water");
panel2.add(button4);
JButton button5 = new JButton("Add Food");
panel2.add(button5);
JButton button6 = new JButton("Add Medicine");
panel2.add(button6);
ImageIcon image1 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel1 = new JLabel(image1);
panel2.add(imagelabel1);
JProgressBar pbar1 = new JProgressBar();
pbar1.setMinimum(MY_MINIMUM);
pbar1.setMaximum(MY_MAXIMUM);
// add to JPanel
panel2.add(pbar1);
// Third Panel
JPanel panel3 = new JPanel();
panel3.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel3);
JButton button7 = new JButton("Add Water");
panel3.add(button7);
JButton button8 = new JButton("Add Food");
panel3.add(button8);
JButton button9 = new JButton("Add Medicine");
panel3.add(button9);
ImageIcon image2 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel2 = new JLabel(image2);
panel3.add(imagelabel2);
JProgressBar pbar2 = new JProgressBar();
pbar2.setMinimum(MY_MINIMUM);
pbar2.setMaximum(MY_MAXIMUM);
// add to JPanel
panel3.add(pbar2);
}
//static class Action implements ActionListener {
//public void actionPerformed (ActionEvent e){
//}
//}
}
Move the frame1.setVisible(true); all the way to the bottom. Changing Components on a frame that is already visible can cause issues.

GUI Layout for User Interface

I am trying to get a userinterface page to display correctly on my page. It has to display a certain way on the screen but I cant get the correct Lay out to show up. Any suggestions or Help?! I've tried many things but Java can get a bit confusing. ATTACHED IS A LINK FOR IMAGE OF HOW ITS SUPPOSED TO LOOK
https://courses.eas.asu.edu/cse205/current/assignments/assignment6/assignment6.html
public CreatePanel(Vector accountList, TransferPanel tPanel)
{
this.accountList = accountList;
this.transferPanel = tPanel;
JLabel label1 =new JLabel("Account ID: ");
JLabel label2 = new JLabel("Amount: ");
JTextField field1 = new JTextField();
field1.setPreferredSize(new Dimension(250,70));
JTextField field2 = new JTextField();
field2.setPreferredSize(new Dimension(250,70));
button1 = new JButton("Create an Account");
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(500, 600));
textArea.append("No account");
textArea.setEditable(true);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3,3));
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
panel1.add(field2);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(button1, BorderLayout.SOUTH);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add(textArea, BorderLayout.WEST);
add(panel1);
add(panel3);
add(panel2);
//ActionListener listener = new ButtonListener();
//button1.addActionListener(listener);
}

Content in JPanel won't appear

There is no errors but when I Run it the content that I added into the JPanel won't appear, only the one not inside the JPanel appear.
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame
{
public static void main(String arg[])
{
SimpleGUI f = new SimpleGUI("GUI components");
f.setSize(600,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
SimpleGUI(String s)
{
setTitle(s);
setLayout(new GridLayout(3,2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel ("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel ("Enter age:");
JTextField tf2= new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold",true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5,20);
JList list = new JList(new Object[] {"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[] {"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play",ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
}
}
I've updated your code. Have a look at this version:
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame {
public static void main(String arg[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
SimpleGUI f = new SimpleGUI("GUI components");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
public SimpleGUI(String s) {
setTitle(s);
setLayout(new GridLayout(3, 2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel("Enter age:");
JTextField tf2 = new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold", true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5, 20);
JList list = new JList(new Object[]{"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[]{"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play", ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
/**
* Need to add the following lines
*/
this.add(p1);
this.add(p2);
this.add(p3);
this.pack();
this.setVisible(true);
}
}
A couple of pointers:
You need to add your components to your JFrame for them to actually show up.
Any updates to the user interface must happen on the event dispatch thread. Consequently you would notice that I've added a SwingUtilites.invokeLater() to the main. Have a look at this article to understand "Threading with Swing"
Where you you add your panels to the frame? Also, forgotten my java "rules and regulations": do you need to call "super()"?

Categories