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");
}
}
});
Related
I am making a vending machine in which there are 6 slots within the vending machine, each with its own soda. We are to have an arraylist of enums for the brands of the sodas. I have created the buttons for each of the canslots but do not know how to assign an arraylist value for one can slot to the appropriate button as well as to have once you click the button for the canslot to decrease by 1 until it is empty.
My code for the actual vending machine is here:
public class VendingFrame extends JFrame {
private ArrayList<CanSlot> CanSlots = new ArrayList<>();
public VendingFrame() {
CanSlots.add(new CanSlot(Brand.PEPSI));
CanSlots.add(new CanSlot(Brand.COKE));
CanSlots.add(new CanSlot(Brand.SUNKIST));
CanSlots.add(new CanSlot(Brand.DIETPEPSI));
CanSlots.add(new CanSlot(Brand.MTDEW));
CanSlots.add(new CanSlot(Brand.SPRITE));
JPanel panel = new JPanel();
JButton button = new JButton("Pepsi");
button.setPreferredSize(new Dimension(100, 80));
JButton button2 = new JButton("Coke");
button2.setPreferredSize(new Dimension(100, 80));
JButton button3 = new JButton("Diest Pepsi");
button3.setPreferredSize(new Dimension(100, 80));
JButton button4 = new JButton("Sunkist");
button4.setPreferredSize(new Dimension(100, 80));
JButton button5 = new JButton("Mountain Dew");
button5.setPreferredSize(new Dimension(100, 80));
JButton button6 = new JButton("Sprite");
button6.setPreferredSize(new Dimension(100, 80));
JPanel picpanel = new JPanel();
JPanel buttonPanel = new JPanel();
JLabel label = new JLabel();
setLayout(new BorderLayout());
buttonPanel.setLayout(new GridLayout(6, 1));
add(panel);
picpanel.add(label);
label.setIcon(new javax.swing.ImageIcon("C:\\Users\\iacol\\Desktop\\cans.jpg.jpg"));
buttonPanel.add(button);
button.addActionListener(new ClickListener());
buttonPanel.add(button2);
button2.addActionListener(new ClickListener2());
buttonPanel.add(button3);
button3.addActionListener(new ClickListener3());
buttonPanel.add(button4);
button4.addActionListener(new ClickListener4());
buttonPanel.add(button5);
button5.addActionListener(new ClickListener5());
buttonPanel.add(button6);
button6.addActionListener(new ClickListener6());
add(picpanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.EAST);
setSize(700, 700);
setTitle("Vending Machine");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
}
You can make, HashMap<Button, List<Product>>();
Using how to id the Button object and retrieve the button list on the value.
I am a new to Java, I met a problem. Once I click the Button, it never shows me another form, just disappear. You can see, I set the button ActionListener, but it only run the second line (close the current form).
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class login implements ActionListener {
public login(){
JFrame frame = new JFrame("Login");
frame.setLayout(new GridLayout(5,1));
JLabel label1 = new JLabel("User Name:");
JPanel panel1 = new JPanel();
frame.add(new JPanel());
frame.add(panel1);
panel1.add(label1);
JLabel label2 = new JLabel("Password:");
JPanel panel2 = new JPanel();
frame.add(panel2);
panel2.add(label2);
JPanel panel3 = new JPanel();
JButton button1 = new JButton("Register");
//button1.addActionListener(this);
JButton button2 = new JButton("Login");
//button2.addActionListener(this);
JButton button3 = new JButton("Cancel");
//button3.addActionListener(this);
panel3.add(button1);
panel3.add(button2);
panel3.add(button3);
frame.add(panel3);
JTextField JTF = new JTextField(12);
JPasswordField JPF = new JPasswordField(12);
panel1.add(JTF);
panel2.add(JPF);
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(300, 200);
frame.setLocation(300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new register();
frame.dispose();
}
});
}
public static void main(String[] args){
new login();
}
public void actionPerformed(ActionEvent e ){
}
}
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());
}
}
I am trying to display the drawing that I have posted. When my code runs and the user clicks Account, the panel only displays the buttons "OK" and "Cancel" (see screenshot). I have added three JTextAreas with a JLabel for each to the panel accountPanel but they don't display. My code is below.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
public class TestApplication implements ActionListener {
public static void main(String[] args) {
JLabel input = new JLabel();
final JFrame frame = new JFrame();
frame.setSize(1000, 1000);
frame.setTitle("RBA Test Application");
frame.add(input);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JRadioButton apprve = new JRadioButton("Approve");
JRadioButton decline = new JRadioButton("Decline");
JRadioButton ethernet = new JRadioButton("Ethernet");
ethernet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog esettings = new JDialog(frame);
esettings.setTitle("Ethernet Settings");
esettings.setSize(400, 400);
esettings.pack();
esettings.setVisible(true);
}
});
JRadioButton rs = new JRadioButton("RS232");
rs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog rsettings = new JDialog(frame);
rsettings.setTitle("RS232 Settings");
rsettings.setSize(400, 400);
rsettings.pack();
rsettings.setVisible(true);
}
});
JRadioButton usbcdc = new JRadioButton("USB_CDC");
usbcdc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog usbc = new JDialog(frame);
usbc.setTitle("USB_CDC Settings");
usbc.setSize(400, 400);
usbc.pack();
usbc.setVisible(true);
}
});
JRadioButton usbhid = new JRadioButton("USB_HID");
usbhid.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog usbh = new JDialog(frame);
usbh.setTitle("USB_HID Settings");
usbh.setSize(400, 400);
usbh.pack();
usbh.setVisible(true);
}
});
JButton next = new JButton("Next");
JButton aok = new JButton("OK");
JButton bok = new JButton("OK");
JButton cok = new JButton("OK");
JButton acancel = new JButton("Cancel");
JButton bcancel = new JButton("Cancel");
JButton ccancel = new JButton("Cancel");
JButton dcancel = new JButton("Cancel");
JLabel cardLabel = new JLabel("Card Number: ");
JLabel expLabel = new JLabel("Exp. Date: ");
JLabel cvvLabel = new JLabel("CVV: ");
JTextArea card = new JTextArea();
card.add(cardLabel);
JTextArea expDate = new JTextArea();
expDate.add(expLabel);
JTextArea cvv = new JTextArea();
cvv.add(cvvLabel);
final JPanel PortSettings = new JPanel();
PortSettings.add(ethernet);
PortSettings.add(rs);
PortSettings.add(usbcdc);
PortSettings.add(usbhid);
PortSettings.add(next);
PortSettings.add(bcancel);
final JPanel accountPanel = new JPanel();
accountPanel.add(bok);
accountPanel.add(ccancel);
accountPanel.add(card);
accountPanel.add(expDate);
accountPanel.add(cvv);
final JPanel apprvordecl = new JPanel();
apprvordecl.add(apprve);
apprvordecl.add(decline);
apprvordecl.add(aok);
apprvordecl.add(acancel);
final JPanel amountPanel = new JPanel();
amountPanel.add(cok);
amountPanel.add(dcancel);
input.setFont(new java.awt.Font("Tahoma", 3, 18));
input.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
input.setText("Input / Output Log");
JButton initialize = new JButton("Initialize");
JButton connect = new JButton("Connect");
JButton disconnect = new JButton("Disconnect");
JButton shutdown = new JButton("Shut Down");
JButton portsettings = new JButton("Port Settings");
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog port = new JDialog(frame);
port.setTitle("Port Settings");
port.setSize(400, 400);
port.add(PortSettings);
port.pack();
port.setVisible(true);
}
});
JButton online = new JButton("Go Online");
JButton offline = new JButton("Go Offline");
JButton status = new JButton("Status");
JButton reboot = new JButton("Reboot");
JButton account = new JButton("Account");
account.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog accountDialog = new JDialog(frame);
accountDialog.setTitle("Account");
accountDialog.setSize(400, 400);
accountDialog.add(accountPanel);
accountDialog.pack();
accountDialog.setVisible(true);
}
});
JButton amount = new JButton("Amount");
amount.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog amount2 = new JDialog(frame);
amount2.setTitle("Amount");
amount2.setSize(400, 400);
amount2.add(amountPanel);
amount2.pack();
amount2.setVisible(true);
}
});
JButton reset = new JButton("Reset");
JButton approvordecl = new JButton("Approve / Decline");
approvordecl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog apprv = new JDialog(frame);
apprv.setTitle("Approve / Decline");
apprv.setSize(400, 400);
apprv.add(apprvordecl);
apprv.pack();
apprv.setVisible(true);
}
});
JButton test = new JButton("Test Button #1");
JButton testing = new JButton("Test Button #2");
JRadioButton button = new JRadioButton("Radio Button");
JRadioButton button2 = new JRadioButton("Radio Button");
JCheckBox checkbox = new JCheckBox("Check Box");
JCheckBox checkbox2 = new JCheckBox("Check Box");
ButtonGroup group = new ButtonGroup();
group.add(usbhid);
group.add(usbcdc);
group.add(ethernet);
group.add(rs);
ButtonGroup approvegroup = new ButtonGroup();
approvegroup.add(apprve);
approvegroup.add(decline);
JPanel testPanel = new JPanel();
testPanel.add(button);
testPanel.add(button2);
testPanel.add(checkbox2);
JPanel posPanel = new JPanel();
posPanel.add(test);
posPanel.add(testing);
posPanel.add(checkbox);
JPanel llpPanel = new JPanel();
llpPanel.add(online);
llpPanel.add(offline);
llpPanel.add(status);
llpPanel.add(reboot);
llpPanel.add(account);
llpPanel.add(amount);
llpPanel.add(reset);
llpPanel.add(approvordecl);
JPanel buttonPanel = new JPanel();
buttonPanel.add(initialize);
buttonPanel.add(connect);
buttonPanel.add(disconnect);
buttonPanel.add(shutdown);
buttonPanel.add(portsettings);
frame.add(buttonPanel);
frame.add(buttonPanel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
tabbedPane.addTab("Test", null, testPanel, "Test");
JPanel tabsPanel = new JPanel(new BorderLayout());
tabsPanel.add(tabbedPane);
frame.add(tabsPanel, BorderLayout.CENTER);
frame.pack();
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
JPanel uses FlowLayout by default which respects preferred sizes. The default width of the preferred size for the JTextArea's is 0 x 0. You need to give the JTextComponent's a preferred size for them to appear. Use the constructor that specifys rows & columns:
JTextArea card = new JTextArea(5, 10);
Don't forget to make the JTextArea scrollable by enclosing it in a JScrollPane:
accountPanel.add(new JScrollPane(card));
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()"?