Radio Button does not appear in JFrame - java

My JRadioButton does not appear in the JFrame.
It should appear below the Method of payment. If the user picks the creditRadioButton, the creditCardLabel and the creditTextField appears.
Code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class FinishTransaction extends JFrame implements ActionListener{
JLabel totalLabel;
JLabel nameLabel;
JLabel addressLabel;
JLabel contactLabel;
JLabel custPaymentLabel;
JLabel changeLabel;
JLabel methodLabel;
JLabel creditCardLabel;
JTextField totalTextField;
JTextField nameTextField;
JTextField addressTextField;
JTextField contactTextField;
JTextField custPaymentTextField;
JTextField changeTextField;
JTextField creditCardTextField;
final ButtonGroup bGroup = new ButtonGroup();
final JRadioButton[] buttons = new JRadioButton[2];
JButton checkoutButton;
static FinishTransaction fin = new FinishTransaction();
public FinishTransaction(){
super("Finish Transaction-Print Receipt");
setLayout(null);
totalLabel = new JLabel("Total Payment: ");
nameLabel = new JLabel("Name: ");
addressLabel = new JLabel("Address: ");
contactLabel = new JLabel("Contact: ");
custPaymentLabel = new JLabel("Payment: ");
changeLabel = new JLabel("Change: ");
totalTextField = new JTextField("");
nameTextField = new JTextField("");
addressTextField = new JTextField("");
contactTextField = new JTextField("");
custPaymentTextField = new JTextField("");
changeTextField = new JTextField("");
methodLabel = new JLabel("Method of payment: ");
creditCardLabel = new JLabel("Credit Card number: ");
creditCardTextField = new JTextField("");
checkoutButton = new JButton("Checkout");
buttons[0] = new JRadioButton("Cash");
buttons[1] = new JRadioButton("Credit Card");
totalTextField.setEditable(false);
changeTextField.setEditable(false);
creditCardLabel.setVisible(false);
creditCardTextField.setVisible(false);
nameLabel.setBounds(50,60,100,20); nameTextField.setBounds(130,60,200,20);
addressLabel.setBounds(50,80,100,20); addressTextField.setBounds(130,80,200,20);
contactLabel.setBounds(50,100,100,20); contactTextField.setBounds(130,100,200,20);
methodLabel.setBounds(50,130,200,20);
buttons[0].setBounds(50,150,100,20);
creditCardLabel.setBounds(90,170,150,20); creditCardTextField.setBounds(210,170,200,20);
buttons[1].setBounds(50,190,100,20);
totalLabel.setBounds(50,230,100,20); totalTextField.setBounds(140,230,200,20);
custPaymentLabel.setBounds(50,250,100,20); custPaymentTextField.setBounds(130,250,200,20);
changeLabel.setBounds(50,270,100,20); changeTextField.setBounds(130,270,200,20);
checkoutButton.setBounds(130,310,200,20);
add(nameLabel);
add(addressLabel);
add(contactLabel);
add(methodLabel);
bGroup.add(buttons[0]);
bGroup.add(buttons[1]);
add(creditCardLabel);
add(totalLabel);
add(custPaymentLabel);
add(changeLabel);
add(nameTextField);
add(addressTextField);
add(contactTextField);
add(creditCardTextField);
add(totalTextField);
add(custPaymentTextField);
add(changeTextField);
add(checkoutButton);
buttons[0].setSelected(true);
buttons[0].addActionListener(this);
buttons[1].addActionListener(this);
checkoutButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(checkoutButton.getName().equals(((Component)e.getSource()).getName())){
}
if(buttons[0].isSelected()){
creditCardLabel.setVisible(true);
creditCardTextField.setVisible(true);
}
}
public static void main(String args[]){
fin.setVisible(true);
fin.setResizable(false);
fin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fin.setSize(500,400);
}
}
The output:

You did not add buttons[0] and buttons[1] to the panel. Try to add them after this line add(methodLabel); and check the result.
add(buttons[0]);
add(buttons[1]);

Related

I got "error:cannot find symbol" for the JButton inside a frame outside of a JTabbedPane [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 4 years ago.
So I have a frame, inside the frame there are tabs. I tried to put Jbutton outside of the tab but it wouldn't recognize my Jbutton. I keep on getting
error: Cant find symbol.
I don't understand why that happened. Would someone explain?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.border.*;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class menu extends JFrame implements ActionListener{
JFrame f;
public menu()
{ f=new JFrame();
JPanel p0 = new JPanel(); p0.setBackground(Color.WHITE);
JPanel p01 = new JPanel(); p01.setBackground(Color.WHITE);
JPanel p01_0 = new JPanel(); p01_0.setBackground(Color.WHITE);
JPanel p01_2 = new JPanel(); p01_2.setBackground(Color.WHITE);
JPanel p01_3 =new JPanel(); p01_3.setBackground(Color.WHITE);
JPanel p01_4 =new JPanel(); p01_4.setBackground(Color.WHITE);
JPanel p01_5 =new JPanel(); p01_5.setBackground(Color.WHITE);
JPanel p01A = new JPanel(); p01A.setBackground(Color.WHITE);
JPanel p02 = new JPanel(); p02.setBackground(Color.WHITE);
JPanel p03 = new JPanel(); p03.setBackground(Color.WHITE);
JPanel p1=new JPanel();
p1.setBackground(Color.WHITE);
JPanel p1A=new JPanel();
p1A.setBackground(Color.WHITE);
JPanel p1A1=new JPanel(); p1A1.setBackground(Color.WHITE); JPanel
p1A2=new JPanel(); p1A2.setBackground(Color.WHITE);
JPanel p1A3=new JPanel(); p1A3.setBackground(Color.WHITE); JPanel
p1A4=new JPanel(); p1A4.setBackground(Color.WHITE);
JPanel p1A5=new JPanel(); p1A5.setBackground(Color.WHITE); JPanel
p1A6=new JPanel(); p1A6.setBackground(Color.WHITE);
JPanel p1A7=new JPanel(); p1A7.setBackground(Color.WHITE); JPanel
p1A8=new JPanel(); p1A8.setBackground(Color.WHITE);
JPanel p1A9=new JPanel(); p1A9.setBackground(Color.WHITE); JPanel
p1A10=new JPanel(); p1A10.setBackground(Color.WHITE);
JPanel p2=new JPanel();
p2.setBackground(Color.WHITE);
JPanel p2A=new JPanel();
p2A.setBackground(Color.WHITE);
JPanel p2A0=new JPanel(); p2A0.setBackground(Color.WHITE); JPanel
p2A1=new JPanel(); p2A1.setBackground(Color.WHITE);
JPanel p2A2=new JPanel(); p2A2.setBackground(Color.WHITE); JPanel
p2A3=new JPanel(); p2A3.setBackground(Color.WHITE);
JPanel p2A4=new JPanel(); p2A4.setBackground(Color.WHITE); JPanel
p2A5=new JPanel(); p2A5.setBackground(Color.WHITE);
JPanel p2A6=new JPanel(); p2A6.setBackground(Color.WHITE); JPanel
p2A7=new JPanel(); p2A7.setBackground(Color.WHITE);
JPanel p2A8=new JPanel(); JPanel p2A9=new JPanel(); JPanel p2A10=new
JPanel(); JPanel p2A11=new JPanel();
JPanel p3=new JPanel();
p3.setBackground(Color.WHITE);
JPanel p3A=new JPanel();
p3A.setBackground(Color.WHITE);
JPanel p3A0=new JPanel(); p3A0.setBackground(Color.WHITE); JPanel
p3A1=new JPanel(); p3A1.setBackground(Color.WHITE);
JPanel p3A2=new JPanel(); p3A2.setBackground(Color.WHITE); JPanel
p3A3=new JPanel(); p3A3.setBackground(Color.WHITE);
JPanel p3A4=new JPanel(); p3A4.setBackground(Color.WHITE);JPanel
p3A5=new JPanel(); p3A5.setBackground(Color.WHITE);
JPanel p3A6=new JPanel(); p3A6.setBackground(Color.WHITE);JPanel
p3A7=new JPanel(); p3A7.setBackground(Color.WHITE);
JPanel p4=new JPanel();
p4.setBackground(Color.WHITE);
JPanel p4A=new JPanel();
p4A.setBackground(Color.WHITE);
JPanel p4A0=new JPanel(); p4A0.setBackground(Color.WHITE); JPanel
p4A1=new JPanel(); p4A1.setBackground(Color.WHITE); JPanel p4A11=new
JPanel(); p4A11.setBackground(Color.WHITE);
JPanel p4A2=new JPanel(); p4A2.setBackground(Color.WHITE); JPanel
p4A3=new JPanel(); p4A3.setBackground(Color.WHITE);
JPanel p4A4=new JPanel(); p4A4.setBackground(Color.WHITE); JPanel
p4A5=new JPanel(); p4A5.setBackground(Color.WHITE);
JPanel p4A6=new JPanel(); p4A6.setBackground(Color.WHITE); JPanel
p4A7=new JPanel(); p4A7.setBackground(Color.WHITE);
JPanel p4B=new JPanel();
p4B.setBackground(Color.WHITE);
JPanel p4B11=new JPanel(); p4B11.setBackground(Color.WHITE); JPanel
p4B1=new JPanel(); p4B1.setBackground(Color.WHITE); JPanel p4B2=new
JPanel(); p4B2.setBackground(Color.WHITE);
JPanel p4B3=new JPanel(); p4B3.setBackground(Color.WHITE); JPanel
p4B4=new JPanel(); p4B4.setBackground(Color.WHITE);
JPanel p4B5=new JPanel(); p4B5.setBackground(Color.WHITE);
JPanel p4C=new JPanel();
p4C.setBackground(Color.WHITE);
JPanel p4D=new JPanel();
p4D.setBackground(Color.WHITE);
JPanel p4D11=new JPanel(); p4D11.setBackground(Color.WHITE); JPanel
p4D1=new JPanel(); p4D1.setBackground(Color.WHITE);
JPanel p4D2=new JPanel(); p4D2.setBackground(Color.WHITE);
JPanel p4F=new JPanel(); p4F.setBackground(Color.WHITE);
Font myFontResta = new Font ( "AR DARLING",Font.BOLD, 60);
Font detailfont = new Font ( "Century Gothic", Font.BOLD, 25);
Font mealfont = new Font ( "Century Gothic", Font.BOLD, 20);
Font drinksfont = new Font ( "Century Gothic", Font.BOLD, 20);
Font drinksfont2 = new Font ( "Century Gothic", Font.BOLD, 25);
LineBorder lineBorder1=new LineBorder(Color.BLACK);
LineBorder lineBorder2=new LineBorder(Color.BLACK);
LineBorder lineBorder3=new LineBorder(Color.BLACK);
JTabbedPane tp = new JTabbedPane();
tp.setFont( new Font( "Century Gothic", Font.BOLD, 24 ) );
tp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
JLabel logo1 = new JLabel(new ImageIcon("wotakoi.png"));
JLabel RName1 = new JLabel("Restaurant");
JLabel table = new JLabel("Table's Number :");
JTextField number = new JTextField(" " ,10);
JLabel Detail = new JLabel("Order Detail");
JLabel food = new JLabel("Food(s) :");
JTextField FoodList = new JTextField(" ",20);
JLabel drnk = new JLabel("Drink(s) :");
JTextField DrinkList = new JTextField(" ",20);
JLabel Price = new JLabel("Total Price :");
JTextField total = new JTextField(" ",20);
JLabel space1 = new JLabel(" "); JLabel space2 = new JLabel(" "); JLabel
space3 = new JLabel(" "); JLabel space4 = new JLabel(" ");
JLabel space5 = new JLabel(" "); JLabel space6 = new JLabel(" "); JLabel
space7 = new JLabel(" "); JLabel space8 = new JLabel(" ");
JLabel space9 = new JLabel(" "); JLabel space10 = new JLabel(" ");
JButton confirm = new JButton("Confirm Order");
confirm.setBackground(Color.GREEN);
confirm.setContentAreaFilled(false);
confirm.setOpaque(true);
JButton reset = new JButton("Reset");
reset.setBackground(Color.RED);
reset.setContentAreaFilled(false);
reset.setOpaque(true);
JButton home = new JButton("Select Table");
//WESTERN
ImageIcon bburger = new ImageIcon("wotakoi.png");
JLabel BBURGER = new JLabel(bburger);
JButton BBURGERp1 = new JButton("+");
BBURGERp1.setBackground(Color.WHITE);
BBURGERp1.setContentAreaFilled(false);
BBURGERp1.setOpaque(true);
JTextField tf22 = new JTextField(" ",2);
JButton BBURGERm1 = new JButton("-");
BBURGERm1.setBackground(Color.WHITE);
BBURGERm1.setContentAreaFilled(false);
BBURGERm1.setOpaque(true);
ImageIcon cburger = new ImageIcon("wotakoi.png");
JLabel CBURGER = new JLabel(cburger);
JButton CBURGERp1 = new JButton("+");
CBURGERp1.setBackground(Color.WHITE);
CBURGERp1.setContentAreaFilled(false);
CBURGERp1.setOpaque(true);
JTextField tf23 = new JTextField(" ",2);
JButton CBURGERm1 = new JButton("-");
CBURGERm1.setBackground(Color.WHITE);
CBURGERm1.setContentAreaFilled(false);
CBURGERm1.setOpaque(true);
ImageIcon bchop = new ImageIcon("wotakoi.png");
JLabel BCHOP = new JLabel(bchop);
JButton BCHOPp1 = new JButton("+");
BCHOPp1.setBackground(Color.WHITE);
BCHOPp1.setContentAreaFilled(false);
BCHOPp1.setOpaque(true);
JTextField tf24 = new JTextField(" ",2);
JButton BCHOPm1 = new JButton("-");
BCHOPm1.setBackground(Color.WHITE);
BCHOPm1.setContentAreaFilled(false);
BCHOPm1.setOpaque(true);
ImageIcon cchop = new ImageIcon("wotakoi.png");
JLabel CCHOP = new JLabel(cchop);
JButton CCHOPp1 = new JButton("+");
CCHOPp1.setBackground(Color.WHITE);
CCHOPp1.setContentAreaFilled(false);
CCHOPp1.setOpaque(true);
JTextField tf25 = new JTextField(" ",2);
JButton CCHOPm1 = new JButton("-");
CCHOPm1.setBackground(Color.WHITE);
CCHOPm1.setContentAreaFilled(false);
CCHOPm1.setOpaque(true);
ImageIcon lchop = new ImageIcon("wotakoi.png");
JLabel LCHOP = new JLabel(lchop);
JButton LCHOPp1 = new JButton("+");
LCHOPp1.setBackground(Color.WHITE);
LCHOPp1.setContentAreaFilled(false);
LCHOPp1.setOpaque(true);
JTextField tf26 = new JTextField(" ",2);
JButton LCHOPm1 = new JButton("-");
LCHOPm1.setBackground(Color.WHITE);
LCHOPm1.setContentAreaFilled(false);
LCHOPm1.setOpaque(true);
ImageIcon spbolognese = new ImageIcon("wotakoi.png");
JLabel SPBOLOGNESE = new JLabel(spbolognese);
JButton SPBOLOGNESEp1 = new JButton("+");
SPBOLOGNESEp1.setBackground(Color.WHITE);
SPBOLOGNESEp1.setContentAreaFilled(false);
SPBOLOGNESEp1.setOpaque(true);
JTextField tf27 = new JTextField(" ",2);
JButton SPBOLOGNESEm1 = new JButton("-");
SPBOLOGNESEm1.setBackground(Color.WHITE);
SPBOLOGNESEm1.setContentAreaFilled(false);
SPBOLOGNESEm1.setOpaque(true);
ImageIcon spcarbonara = new ImageIcon("wotakoi.png");
JLabel SPCARBONARA = new JLabel(spcarbonara);
JButton SPCARBONARAp1 = new JButton("+");
SPCARBONARAp1.setBackground(Color.WHITE);
SPCARBONARAp1.setContentAreaFilled(false);
SPCARBONARAp1.setOpaque(true);
JTextField tf28 = new JTextField(" ",2);
JButton SPCARBONARAm1 = new JButton("-");
SPCARBONARAm1.setBackground(Color.WHITE);
SPCARBONARAm1.setContentAreaFilled(false);
SPCARBONARAm1.setOpaque(true);
//DRINKS
JLabel drinkspace1=new JLabel(" ");
JLabel drinkspace2=new JLabel(" ");
JLabel drinkspace3=new JLabel(" ");
JLabel hotOrcold = new JLabel("Choose Hot or Cold");
JLabel tea = new JLabel("Tea ");
JLabel spacetea = new JLabel(" ");
JLabel spacetea2 = new JLabel(" ");
//Tea RadioButton
JRadioButton hottea = new JRadioButton("Hot");
hottea.setBackground(Color.WHITE);
hottea.setContentAreaFilled(false);
hottea.setOpaque(true);
JRadioButton coldtea = new JRadioButton("Cold");
coldtea.setBackground(Color.WHITE);
coldtea.setContentAreaFilled(false);
coldtea.setOpaque(true);
//Tea PLUS MINUS Button
JButton teap1 = new JButton("+");
teap1.setBackground(Color.WHITE);
teap1.setContentAreaFilled(false);
teap1.setOpaque(true);
JTextField tf29 = new JTextField(" ",2);
JButton team1 = new JButton("-");
team1.setBackground(Color.WHITE);
team1.setContentAreaFilled(false);
team1.setOpaque(true);
//Tea Group
ButtonGroup teagroup = new ButtonGroup();
teagroup.add(hottea);
teagroup.add(coldtea);
JLabel coffee = new JLabel("Coffee ");
JLabel spacecoffee = new JLabel(" ");
JLabel spacecoffee2 = new JLabel(" ");
//Coffee RadioButton
JRadioButton hotcoffee = new JRadioButton("Hot");
hotcoffee.setBackground(Color.WHITE);
hotcoffee.setContentAreaFilled(false);
hotcoffee.setOpaque(true);
JRadioButton coldcoffee = new JRadioButton("Cold");
coldcoffee.setBackground(Color.WHITE);
coldcoffee.setContentAreaFilled(false);
coldcoffee.setOpaque(true);
//Coffee PLUS MINUS Button
JButton coffeep1 = new JButton("+");
coffeep1.setBackground(Color.WHITE);
coffeep1.setContentAreaFilled(false);
coffeep1.setOpaque(true);
JTextField tf30 = new JTextField(" ",2);
JButton coffeem1 = new JButton("-");
coffeem1.setBackground(Color.WHITE);
coffeem1.setContentAreaFilled(false);
coffeem1.setOpaque(true);
//Coffee Group
ButtonGroup coffeegroup = new ButtonGroup();
coffeegroup.add(hotcoffee);
coffeegroup.add(coldcoffee);
JLabel cocoa = new JLabel("Cocoa ");
JLabel spacecocoa = new JLabel(" ");
JLabel spacecocoa2 = new JLabel(" ");
//Cocoa RadioButton
JRadioButton hotcocoa = new JRadioButton("Hot");
hotcocoa.setBackground(Color.WHITE);
hotcocoa.setContentAreaFilled(false);
hotcocoa.setOpaque(true);
JRadioButton coldcocoa = new JRadioButton("Cold");
coldcocoa.setBackground(Color.WHITE);
coldcocoa.setContentAreaFilled(false);
coldcocoa.setOpaque(true);
//Cocoa PLUS MINUS Button
JButton cocoap1 = new JButton("+");
cocoap1.setBackground(Color.WHITE);
cocoap1.setContentAreaFilled(false);
cocoap1.setOpaque(true);
JTextField tf31 = new JTextField(" ",2);
JButton cocoam1 = new JButton("-");
cocoam1.setBackground(Color.WHITE);
cocoam1.setContentAreaFilled(false);
cocoam1.setOpaque(true);
//Cocoa Group
ButtonGroup cocoagroup = new ButtonGroup();
cocoagroup.add(hotcocoa);
cocoagroup.add(coldcocoa);
JLabel nescafe = new JLabel("Nescafe ");
JLabel spacenescafe = new JLabel(" ");
JLabel spacenescafe2 = new JLabel(" ");
//Nescafe RadioButton
JRadioButton hotnescafe = new JRadioButton("Hot");
hotnescafe.setBackground(Color.WHITE);
hotnescafe.setContentAreaFilled(false);
hotnescafe.setOpaque(true);
JRadioButton coldnescafe = new JRadioButton("Cold");
coldnescafe.setBackground(Color.WHITE);
coldnescafe.setContentAreaFilled(false);
coldnescafe.setOpaque(true);
//Nescafe PLUS MINUS Button
JButton nescafep1 = new JButton("+");
nescafep1.setBackground(Color.WHITE);
nescafep1.setContentAreaFilled(false);
nescafep1.setOpaque(true);
JTextField tf32 = new JTextField(" ",2);
JButton nescafem1 = new JButton("-");
nescafem1.setBackground(Color.WHITE);
nescafem1.setContentAreaFilled(false);
nescafem1.setOpaque(true);
//Nescafe Group
ButtonGroup nescafegroup = new ButtonGroup();
nescafegroup.add(hotnescafe);
nescafegroup.add(coldnescafe);
JLabel milo = new JLabel("Milo ");
JLabel spacemilo = new JLabel(" ");
JLabel spacemilo2 = new JLabel(" ");
//Milo RadioButton
JRadioButton hotmilo = new JRadioButton("Hot");
hotmilo.setBackground(Color.WHITE);
hotmilo.setContentAreaFilled(false);
hotmilo.setOpaque(true);
JRadioButton coldmilo = new JRadioButton("Cold");
coldmilo.setBackground(Color.WHITE);
coldmilo.setContentAreaFilled(false);
coldmilo.setOpaque(true);
//Milo PLUS MINUS Button
JButton milop1 = new JButton("+");
milop1.setBackground(Color.WHITE);
milop1.setContentAreaFilled(false);
milop1.setOpaque(true);
JTextField tf33 = new JTextField(" ",2);
JButton milom1 = new JButton("-");
milom1.setBackground(Color.WHITE);
milom1.setContentAreaFilled(false);
milom1.setOpaque(true);
//Milo Group
ButtonGroup milogroup = new ButtonGroup();
milogroup.add(hotmilo);
milogroup.add(coldmilo);
JLabel greentea = new JLabel("Green Tea");
JLabel spacegreentea = new JLabel(" ");
JLabel spacegreentea2 = new JLabel(" ");
//Greentea RadioButton
JRadioButton hotgreentea = new JRadioButton("Hot");
hotgreentea.setBackground(Color.WHITE);
hotgreentea.setContentAreaFilled(false);
hotgreentea.setOpaque(true);
JRadioButton coldgreentea = new JRadioButton("Cold");
coldgreentea.setBackground(Color.WHITE);
coldgreentea.setContentAreaFilled(false);
coldgreentea.setOpaque(true);
//Greentea PLUS MINUS Button
JButton greenteap1 = new JButton("+");
greenteap1.setBackground(Color.WHITE);
greenteap1.setContentAreaFilled(false);
greenteap1.setOpaque(true);
JTextField tf34 = new JTextField(" ",2);
JButton greenteam1 = new JButton("-");
greenteam1.setBackground(Color.WHITE);
greenteam1.setContentAreaFilled(false);
greenteam1.setOpaque(true);
//Greentea Group
ButtonGroup greenteagroup = new ButtonGroup();
greenteagroup.add(hotgreentea);
greenteagroup.add(coldgreentea);
//Juice
JLabel juice = new JLabel("Juice");
JLabel spacejuice = new JLabel(" ");
JLabel orangejuice = new JLabel("Orang Juice ");
JLabel spaceorangejuice = new JLabel(" ");
JLabel spaceorangejuice2 = new JLabel(" ");
//Orange Juice PLUS MINUS Button
JButton orangejuicep1 = new JButton("+");
orangejuicep1.setBackground(Color.WHITE);
orangejuicep1.setContentAreaFilled(false);
orangejuicep1.setOpaque(true);
JTextField tf35 = new JTextField(" ",2);
JButton orangejuicem1 = new JButton("-");
orangejuicem1.setBackground(Color.WHITE);
orangejuicem1.setContentAreaFilled(false);
orangejuicem1.setOpaque(true);
JLabel lemonade = new JLabel("Lamonade ");
JLabel spacelemonade = new JLabel(" ");
JLabel spacelemonade2 = new JLabel(" ");
//Lemonade PLUS MINUS Button
JButton lemonadep1 = new JButton("+");
lemonadep1.setBackground(Color.WHITE);
lemonadep1.setContentAreaFilled(false);
lemonadep1.setOpaque(true);
JTextField tf36 = new JTextField(" ",2);
JButton lemonadem1 = new JButton("-");
lemonadem1.setBackground(Color.WHITE);
lemonadem1.setContentAreaFilled(false);
lemonadem1.setOpaque(true);
JLabel applejuice = new JLabel("Apple Juice ");
JLabel spaceapplejuice = new JLabel(" ");
JLabel spaceapplejuice2 = new JLabel(" ");
//Apple Juice PLUS MINUS Button
JButton applejuicep1 = new JButton("+");
applejuicep1.setBackground(Color.WHITE);
applejuicep1.setContentAreaFilled(false);
applejuicep1.setOpaque(true);
JTextField tf37 = new JTextField(" ",2);
JButton applejuicem1 = new JButton("-");
applejuicem1.setBackground(Color.WHITE);
applejuicem1.setContentAreaFilled(false);
applejuicem1.setOpaque(true);
JLabel bananajuice = new JLabel("Banana Juice ");
JLabel spacebananajuice = new JLabel(" ");
JLabel spacebananajuice2 = new JLabel(" ");
//Banana Juice PLUS MINUS Button
JButton bananajuicep1 = new JButton("+");
bananajuicep1.setBackground(Color.WHITE);
bananajuicep1.setContentAreaFilled(false);
bananajuicep1.setOpaque(true);
JTextField tf38 = new JTextField(" ",2);
JButton bananajuicem1 = new JButton("-");
bananajuicem1.setBackground(Color.WHITE);
bananajuicem1.setContentAreaFilled(false);
bananajuicem1.setOpaque(true);
//Ice Shave
JLabel iceshave = new JLabel("Ice Shave");
JLabel spaceiceshave = new JLabel(" ");
JLabel cendolfloat = new JLabel("Ice Shave Cendol Float ");
JLabel spacecendolfloat = new JLabel(" ");
JLabel spacecendolfloat2 = new JLabel(" ");
//Cendol Float PLUS MINUS Button
JButton cendolfloatp1 = new JButton("+");
cendolfloatp1.setBackground(Color.WHITE);
cendolfloatp1.setContentAreaFilled(false);
cendolfloatp1.setOpaque(true);
JTextField tf39 = new JTextField(" ",2);
JButton cendolfloatm1 = new JButton("-");
cendolfloatm1.setBackground(Color.WHITE);
cendolfloatm1.setContentAreaFilled(false);
cendolfloatm1.setOpaque(true);
f.setLayout(new BorderLayout());
p0.add(logo1); p0.add(RName1);
RName1.setFont(myFontResta);
RName1.setForeground(Color.BLUE);
p02.setLayout(new BorderLayout());
p01_0.add(Detail);
Detail.setFont(detailfont);
Detail.setForeground(Color.BLACK);
p01_2.setLayout(new GridLayout(11,2));
p01_2.add(table);
table.setFont(mealfont);
table.setForeground(Color.BLACK);
p01_2.add(number);
p01_2.add(space1);p01_2.add(space2);
p01_2.add(food);
food.setFont(mealfont);
food.setForeground(Color.BLACK);
p01_2.add(FoodList);
p01_2.add(space3);p01_2.add(space4);
p01_2.add(drnk);
drnk.setFont(mealfont);
drnk.setForeground(Color.BLACK);
p01_2.add(DrinkList);
p01_2.add(space5);p01_2.add(space6);
p01_2.add(Price);
Price.setFont(mealfont);
Price.setForeground(Color.BLACK);
p01_2.add(total);
p01_2.add(space7); p01_2.add(space8);
p01_2.add(space9); p01_2.add(space10);
p03.add(p01_2);
p02.add(p01_0, BorderLayout.NORTH);
p02.add(p03, BorderLayout.CENTER);
p01_4.add(confirm);
p01_4.add(reset);
p01_2.add(p01_4);
//TAB 2 = Noodles
tp.add(new JScrollPane(p2),"Noodles");
p2A.setLayout(new GridLayout(11,1));
p2A1.add(MB); p2A1.add(MBp1); p2A1.add(tf11); p2A1.add(MBm1);
p2A2.add(MGB); p2A2.add(MGBp1); p2A2.add(tf12); p2A2.add(MGBm1);
p2A3.add(MH); p2A3.add(MHp1); p2A3.add(tf13); p2A3.add(MHm1);
p2A4.add(MK); p2A4.add(MKp1); p2A4.add(tf14); p2A4.add(MKm1);
p2A5.add(MR); p2A5.add(MRp1); p2A5.add(tf15); p2A5.add(MRm1);
p2A6.add(MS); p2A6.add(MSp1); p2A6.add(tf16); p2A6.add(MSm1);
p2A7.add(MTY); p2A7.add(MTYp1); p2A7.add(tf17); p2A7.add(MTYm1);
p2A8.add(BG); p2A8.add(BGp1); p2A8.add(tf18); p2A8.add(BGm1);
p2A9.add(BGS); p2A9.add(BGSp1); p2A9.add(tf19); p2A9.add(BGSm1);
p2A10.add(BS); p2A10.add(BSp1); p2A10.add(tf20); p2A10.add(BSm1);
p2A11.add(BTY); p2A11.add(BTYp1); p2A11.add(tf21); p2A11.add(BTYm1);
p2A.add(p2A1); p2A.add(p2A2); p2A.add(p2A3); p2A.add(p2A4); p2A.add(p2A5); p2A.add(p2A6);
p2A.add(p2A7); p2A.add(p2A8); p2A.add(p2A9); p2A.add(p2A10); p2A.add(p2A11);
p2.setLayout(new BorderLayout());
p2.add(p2A, BorderLayout.CENTER);
//TAB 3 = Western
tp.add(new JScrollPane(p3),"Western");
p3A.setLayout(new GridLayout(7,1));
p3A1.add(BBURGER); p3A1.add(BBURGERp1); p3A1.add(tf22);
p3A1.add(BBURGERm1);
p3A2.add(CBURGER); p3A2.add(CBURGERp1); p3A2.add(tf23);
p3A2.add(CBURGERm1);
p3A3.add(BCHOP); p3A3.add(BCHOPp1); p3A3.add(tf24); p3A3.add(BCHOPm1);
p3A4.add(CCHOP); p3A4.add(CCHOPp1); p3A4.add(tf25); p3A4.add(CCHOPm1);
p3A5.add(LCHOP); p3A5.add(LCHOPp1); p3A5.add(tf26); p3A5.add(LCHOPm1);
p3A6.add(SPBOLOGNESE); p3A6.add(SPBOLOGNESEp1); p3A6.add(tf27);
p3A6.add(SPBOLOGNESEm1);
p3A7.add(SPCARBONARA); p3A7.add(SPCARBONARAp1); p3A7.add(tf28);
p3A7.add(SPCARBONARAm1);
p3A.add(p3A1); p3A.add(p3A2); p3A.add(p3A3); p3A.add(p3A4);
p3A.add(p3A5); p3A.add(p3A6);
p3A.add(p3A7);
p3.setLayout(new BorderLayout());
p3.add(p3A, BorderLayout.CENTER);
//TAB 4 = Drinks
tp.add(new JScrollPane(p4),"Drinks");
p4.setLayout(new BorderLayout());
p4A.setLayout(new GridLayout(8,1));
p4A11.add(hotOrcold);
hotOrcold.setFont(drinksfont2);
p4A1.add(tea); p4A1.add(spacetea); p4A1.add(hottea); p4A1.add(coldtea);
p4A1.add(spacetea2); p4A1.add(teap1); p4A1.add(tf29); p4A1.add(team1);
tea.setFont(drinksfont);
p4A2.add(coffee); p4A2.add(spacecoffee); p4A2.add(hotcoffee);
p4A2.add(coldcoffee); p4A2.add(spacecoffee2); p4A2.add(coffeep1);
p4A2.add(tf30); p4A2.add(coffeem1);
coffee.setFont(drinksfont);
p4A3.add(cocoa); p4A3.add(spacecocoa); p4A3.add(hotcocoa);
p4A3.add(coldcocoa); p4A3.add(spacecocoa2); p4A3.add(cocoap1);
p4A3.add(tf31); p4A3.add(cocoam1);
cocoa.setFont(drinksfont);
p4A4.add(nescafe); p4A4.add(spacenescafe); p4A4.add(hotnescafe);
p4A4.add(coldnescafe); p4A4.add(spacenescafe2); p4A4.add(nescafep1);
p4A4.add(tf32); p4A4.add(nescafem1);
nescafe.setFont(drinksfont);
p4A5.add(milo); p4A5.add(spacemilo); p4A5.add(hotmilo);
p4A5.add(coldmilo); p4A5.add(spacemilo2); p4A5.add(milop1); p4A5.add(tf33);
p4A5.add(milom1);
milo.setFont(drinksfont);
p4A6.add(greentea); p4A6.add(spacegreentea); p4A6.add(hotgreentea);
p4A6.add(coldgreentea); p4A6.add(spacegreentea2); p4A6.add(greenteap1);
p4A6.add(tf34); p4A6.add(greenteam1);
greentea.setFont(drinksfont);
p4A7.add(drinkspace1);
p4A.add(p4A11); p4A.add(p4A1); p4A.add(p4A2); p4A.add(p4A3);
p4A.add(p4A4); p4A.add(p4A5); p4A.add(p4A6); p4A.add(p4A7);
p4A11.setBorder(lineBorder1); p4A1.setBorder(lineBorder1);
p4A2.setBorder(lineBorder1); p4A3.setBorder(lineBorder1);
p4A4.setBorder(lineBorder1); p4A5.setBorder(lineBorder1);
p4A6.setBorder(lineBorder1);
//JUICE
p4C.setLayout(new BorderLayout());
p4B.setLayout(new BorderLayout());
p4D.setLayout(new BorderLayout());
p4F.setLayout(new BorderLayout());
p4B.setLayout(new GridLayout(6,1));
p4B11.add(juice); juice.setFont(drinksfont2);
p4B1.add(orangejuice); orangejuice.setFont(drinksfont);
p4B1.add(spaceorangejuice); spaceorangejuice.setFont(drinksfont);
p4B1.add(spaceorangejuice2); spaceorangejuice2.setFont(drinksfont);
p4B1.add(orangejuicep1); p4B1.add(tf35); p4B1.add(orangejuicem1);
p4B2.add(lemonade); lemonade.setFont(drinksfont);
p4B2.add(spacelemonade); spacelemonade.setFont(drinksfont);
p4B2.add(spacelemonade2); spacelemonade2.setFont(drinksfont);
p4B2.add(lemonadep1); p4B2.add(tf36); p4B2.add(lemonadem1);
p4B3.add(applejuice); applejuice.setFont(drinksfont);
p4B3.add(spaceapplejuice); spaceapplejuice.setFont(drinksfont);
p4B3.add(spaceapplejuice2); spaceapplejuice2.setFont(drinksfont);
p4B3.add(applejuicep1); p4B3.add(tf37); p4B3.add(applejuicem1);
p4B4.add(bananajuice); bananajuice.setFont(drinksfont);
p4B4.add(spacebananajuice); spacebananajuice.setFont(drinksfont);
p4B4.add(spacebananajuice2); spacebananajuice2.setFont(drinksfont);
p4B4.add(bananajuicep1); p4B4.add(tf38); p4B4.add(bananajuicem1);
p4B5.add(drinkspace2);
p4B.add(p4B11); p4B.add(p4B1); p4B.add(p4B2); p4B.add(p4B3);
p4B.add(p4B4); p4B.add(p4B5);
p4B11.setBorder(lineBorder1); p4B1.setBorder(lineBorder1);
p4B2.setBorder(lineBorder1); p4B3.setBorder(lineBorder1);
p4B4.setBorder(lineBorder1);
//Ice Shave
p4D.setLayout(new GridLayout(3,1));
p4D11.add(iceshave); iceshave.setFont(drinksfont2);
p4D1.add(cendolfloat); cendolfloat.setFont(drinksfont);
p4D1.add(spacecendolfloat);
p4D1.add(cendolfloatp1); p4D1.add(tf39); p4D1.add(cendolfloatm1);
p4D2.add(drinkspace3);
p4D.add(p4D11); p4D.add(p4D1); p4D.add(p4D2);
p4D11.setBorder(lineBorder1); p4D1.setBorder(lineBorder1);
p4F.add(p4D);
p4.add(p4A, BorderLayout.WEST);
p4.add(p4C, BorderLayout.SOUTH);
p4C.add(p4B, BorderLayout.WEST);
p4C.add(p4F, BorderLayout.SOUTH);
p4F.add(p4D, BorderLayout.WEST);
f.add(p0, BorderLayout.NORTH);
f.add(p02, BorderLayout.CENTER);
f.add(tp, BorderLayout.WEST);
f.getContentPane().setBackground(Color.WHITE);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(980,800);
f.setVisible(true);
confirm.addActionListener(this);
BBURGERp1.addActionListener(this);
BBURGERm1.addActionListener(this);
CBURGERp1.addActionListener(this);
CBURGERm1.addActionListener(this);
BCHOPp1.addActionListener(this); BCHOPm1.addActionListener(this);
CCHOPp1.addActionListener(this); CCHOPm1.addActionListener(this);
LCHOPp1.addActionListener(this); LCHOPm1.addActionListener(this);
SPBOLOGNESEp1.addActionListener(this);
SPBOLOGNESEm1.addActionListener(this);
SPCARBONARAp1.addActionListener(this);
SPCARBONARAm1.addActionListener(this);
teap1.addActionListener(this); team1.addActionListener(this);
coffeep1.addActionListener(this); team1.addActionListener(this);
cocoap1.addActionListener(this); cocoam1.addActionListener(this);
nescafep1.addActionListener(this);
nescafem1.addActionListener(this);
milop1.addActionListener(this); milom1.addActionListener(this);
greenteap1.addActionListener(this);
greenteam1.addActionListener(this);
orangejuicep1.addActionListener(this);
orangejuicem1.addActionListener(this);
lemonadep1.addActionListener(this);
lemonadem1.addActionListener(this);
applejuicep1.addActionListener(this);
applejuicem1.addActionListener(this);
bananajuicep1.addActionListener(this);
bananajuicem1.addActionListener(this);
cendolfloatp1.addActionListener(this);
cendolfloatm1.addActionListener(this);
}
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();
if(source == confirm)
{
seat frame = new seat();
frame.setTitle("Restaurant Ordering System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,600);
frame.setVisible(true);
frame.setLocation(100,100);
}
else
{
}
}
public static void main(String[] args) {
new menu();
}
First of all, your code is a mess. Unreadable. And you are violating a whole lot of style rules, which is going to discourage anyone from trying to read your code.
(And that kind of makes asking people to help you problematic ..... doesn't it!)
Your "Cannot find symbol" errors seem to have a common explanation. Here's the first one:
menu.java:544: error: cannot find symbol
p2A1.add(MB); p2A1.add(MBp1); p2A1.add(tf11); p2A1.add(MBm1);
^
symbol: variable MB
location: class menu
When I trawl through the code, I cannot find any declaration for a variable called MB. Or mb. Or anything resembling this.
The other errors are similar.
Basically, you have either dumped in a bunch of statements that refer to things that are not declared ... or you have somehow managed to lose the declarations.
I don't think there is much we can do you help you with your code. It is ... incomprehensible.
However, if you need to understand the various things that can cause the Java compiler to produce that error message, read this:
What does a "Cannot find symbol" compilation error mean?

Finding Area within an action event

I am writing a program for class, and I have about all of it done but one part that is really confusing me.
I have to create a system for flooring that a person can choose the floor type, enter their length and width of their floor and it calculates and produces a order summary.
Now, I am able to get all of this but the calculations correct (and I haven't even started on the database connection because of it.)
My professor is extremely vague in her directions and even the answers to her questions, so as a last ditch effort I thought I would try here.
I cannot for the life of me figure it out so ANY help is greatly appreciated.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class FloorMart
{
private static JFrame frame = null;
private static Integer cost;
private static String floorSize;
private static String floorType;
private double floorLength;
private double floorWidth;
public static void main(String[] args)
{
frame = new JFrame("FloorMart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(320, 300);
JTabbedPane tPane = new JTabbedPane();
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.setPreferredSize(new Dimension(200, 200));
JLabel jLabel = new JLabel();
jLabel.setText("Welcome to the FloorMart ordering system! ");
panel1.add(jLabel);
JLabel jLabel1 = new JLabel();
jLabel1.setText("Enter your Name: ");
panel1.add(jLabel1);
JTextField text1 = new JTextField(10);
panel1.add(text1);
JLabel jLabel2 = new JLabel();
jLabel2.setText("Enter your Pnone Number: ");
panel1.add(jLabel2);
JTextField text2 = new JTextField(10);
panel1.add(text2);
tPane.addTab("Customer", panel1);
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.setPreferredSize(new Dimension(200, 200));
jLabel = new JLabel();
jLabel.setText("Floor Type? ");
panel2.add(jLabel);
ButtonGroup group = new ButtonGroup();
JRadioButton RadioButton = new JRadioButton("Carpet - $10 per sq ft", true);
panel2.add(RadioButton);
group.add(RadioButton);
JRadioButton RadioButton1 = new JRadioButton("Hardwood - $20 per sq ft");
panel2.add(RadioButton1);
group.add(RadioButton1);
tPane.addTab("Floor Type", panel2);
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.setPreferredSize(new Dimension(200, 200));
jLabel = new JLabel();
jLabel.setText("Enter the length and width of your floor! ");
panel3.add(jLabel);
JLabel jLabel3 = new JLabel();
jLabel3.setText("Enter the floor length: ");
panel3.add(jLabel3);
JTextField length = new JTextField(10);
panel3.add(length);
//length.setText(cost.toString()); //
JLabel jLabel4 = new JLabel();
jLabel4.setText("Enter the floor width: ");
panel3.add(jLabel4);
JTextField width = new JTextField(10);
panel3.add(width);
//width.setText(cost.toString()); //
tPane.addTab("Floor Size", panel3);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel4.setPreferredSize(new Dimension(200, 200));
jLabel = new JLabel();
jLabel.setText("Total Cost: ");
panel4.add(jLabel);
JTextField text3 = new JTextField(10);
panel4.add(text3);
JButton button = new JButton("Order Summary");
button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
JPanel panelNew = new JPanel();
panelNew.setLayout(new BoxLayout(panelNew, BoxLayout.Y_AXIS));
JLabel jLabeln = new JLabel("Order Summary");
JLabel jLabeln1 = new JLabel("Customer Name: " + text1.getText());
JLabel jLabeln2 = new JLabel("Phone Number: " + text2.getText());
JLabel jLabeln3 = new JLabel("Floor Type: " + floorType);
JLabel jLabeln4 = new JLabel("Floor Area: " + floorSize);
JLabel jLabeln5 = new JLabel("Total: $" + new Double(cost) + "0");
JLabel jLabeln6 = new JLabel("Thank you for shopping at FloorMart!");
panelNew.add(jLabeln);
panelNew.add(jLabeln1);
panelNew.add(jLabeln2);
panelNew.add(jLabeln3);
panelNew.add(jLabeln4);
panelNew.add(jLabeln5);
panelNew.add(jLabeln6);
frame.invalidate();
frame.remove(panel1);frame.remove(panel2);frame.remove(panel3);
frame.remove(panel4);
frame.remove(tPane);
frame.add(panelNew);
frame.revalidate();
frame.repaint();
}
});
panel4.add(button);
tPane.addTab("Total", panel4);
ChangeListener changeListener= new ChangeListener()
{
public void stateChanged(ChangeEvent changeEvent)
{
JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
int index = sourceTabbedPane.getSelectedIndex();
if(index == 2)
{
double floorLength = Double.parseDouble(length.getText());
double floorWidth = Double.parseDouble(width.getText());
floorSize = floorLength * floorWidth;
if(RadioButton.isSelected())
{
cost = 10; //per sq ft
floorType = RadioButton.getText();
}
else if(RadioButton1.isSelected())
{
cost = 20; //per sq ft
floorType = RadioButton1.getText();
}
text3.setText(cost.toString());
}
}
};
tPane.addChangeListener(changeListener);
frame.add(tPane);
frame.setVisible(true);
}
}
I can see two basic things that needs to be changed:
a. this does not compile (floorSize should be double)
String floorSize;
double floorLength = Double.parseDouble(length.getText());
double floorWidth = Double.parseDouble(width.getText());
floorSize = floorLength * floorWidth;
b. The calculation of floorSize should not be triggered by ChangeListener (which causes the calculation to be done before values are entered).
Use "Order Summary" JButton to trigger the calculation.
Here is a working version of the code. See my comments:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class FloorMart
{
private static JFrame frame = null;
private static Integer cost; //better use int
//////////////////////////////////////
//changed from string to double
private static double floorSize;
////////////////////////////////////
private static String floorType;
private double floorLength;
private double floorWidth;
public static void main(String[] args)
{
frame = new JFrame("FloorMart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(320, 300);
JTabbedPane tPane = new JTabbedPane();
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.setPreferredSize(new Dimension(200, 200));
JLabel jLabel = new JLabel();
jLabel.setText("Welcome to the FloorMart ordering system! ");
panel1.add(jLabel);
JLabel jLabel1 = new JLabel();
jLabel1.setText("Enter your Name: ");
panel1.add(jLabel1);
JTextField text1 = new JTextField(10);
panel1.add(text1);
JLabel jLabel2 = new JLabel();
jLabel2.setText("Enter your Pnone Number: ");
panel1.add(jLabel2);
JTextField text2 = new JTextField(10);
panel1.add(text2);
tPane.addTab("Customer", panel1);
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.setPreferredSize(new Dimension(200, 200));
jLabel = new JLabel();
jLabel.setText("Floor Type? ");
panel2.add(jLabel);
ButtonGroup group = new ButtonGroup();
JRadioButton RadioButton = new JRadioButton("Carpet - $10 per sq ft", true);
panel2.add(RadioButton);
group.add(RadioButton);
JRadioButton RadioButton1 = new JRadioButton("Hardwood - $20 per sq ft");
panel2.add(RadioButton1);
group.add(RadioButton1);
tPane.addTab("Floor Type", panel2);
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.setPreferredSize(new Dimension(200, 200));
jLabel = new JLabel();
jLabel.setText("Enter the length and width of your floor! ");
panel3.add(jLabel);
JLabel jLabel3 = new JLabel();
jLabel3.setText("Enter the floor length: ");
panel3.add(jLabel3);
JTextField length = new JTextField(10);
panel3.add(length);
//length.setText(cost.toString()); //
JLabel jLabel4 = new JLabel();
jLabel4.setText("Enter the floor width: ");
panel3.add(jLabel4);
JTextField width = new JTextField(10);
panel3.add(width);
//width.setText(cost.toString()); //
tPane.addTab("Floor Size", panel3);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel4.setPreferredSize(new Dimension(200, 200));
jLabel = new JLabel();
jLabel.setText("Total Cost: ");
panel4.add(jLabel);
JTextField text3 = new JTextField(10);
panel4.add(text3);
JButton button = new JButton("Order Summary");
button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
//////////////////////////////////////////////////////////////
//calculation moved to here
double floorLength = Double.parseDouble(length.getText());
double floorWidth = Double.parseDouble(width.getText());
floorSize = floorLength * floorWidth;
if(RadioButton.isSelected())
{
cost = 10; //per sq ft
floorType = RadioButton.getText();
}
else if(RadioButton1.isSelected())
{
cost = 20; //per sq ft
floorType = RadioButton1.getText();
}
text3.setText(cost.toString());
//////////////////////////////////////////////////////////////
JPanel panelNew = new JPanel();
panelNew.setLayout(new BoxLayout(panelNew, BoxLayout.Y_AXIS));
JLabel jLabeln = new JLabel("Order Summary");
JLabel jLabeln1 = new JLabel("Customer Name: " + text1.getText());
JLabel jLabeln2 = new JLabel("Phone Number: " + text2.getText());
JLabel jLabeln3 = new JLabel("Floor Type: " + floorType);
JLabel jLabeln4 = new JLabel("Floor Area: " + floorSize);
///////////////////////////////////////////////////////////////////////////
// changed JLabel jLabeln5 = new JLabel("Total: $" + new Double(cost*floorSize) + "0");
//to:
JLabel jLabeln5 = new JLabel("Total: $" + new Double(cost*floorSize) + "0");
///////////////////////////////////////////////////////////////////////////
JLabel jLabeln6 = new JLabel("Thank you for shopping at FloorMart!");
panelNew.add(jLabeln);
panelNew.add(jLabeln1);
panelNew.add(jLabeln2);
panelNew.add(jLabeln3);
panelNew.add(jLabeln4);
panelNew.add(jLabeln5);
panelNew.add(jLabeln6);
frame.invalidate();
frame.remove(panel1);frame.remove(panel2);frame.remove(panel3);
frame.remove(panel4);
frame.remove(tPane);
frame.add(panelNew);
frame.revalidate();
frame.repaint();
}
});
panel4.add(button);
tPane.addTab("Total", panel4);
/////////////////////////////////
//removed ChangeListener
////////////////////////////////
frame.add(tPane);
frame.setVisible(true);
}
}

Having prob with my "Display Data Button"

Try running my program. Press Add Subject (fill the data). Then Display. Then Add another subject. Then Display.
On the Display Tab, it shows 2 subjects and 1 empty subject.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
#SuppressWarnings ({"unchecked" , "rawtypes"})
public class StudentGradeCalc extends JFrame implements ActionListener {
//String setName [];
int x = 0;
int count = 0;
int w = 0;
int [] addSize = {50,10,20,30,40,100};
private JPanel panelInfo = new JPanel(new GridLayout(7,0) );
private JLabel sy = new JLabel ("School Year : ");
private JLabel sem = new JLabel ("Sem/Term: ");
private JLabel Name = new JLabel ("Name : ");
private JLabel Course = new JLabel ("Course : ");
private JLabel Year = new JLabel ("Year : ");
private JLabel college = new JLabel ("College : ");
private JLabel id = new JLabel ("I.D Number : ");
private JTextField syear = new JTextField ("");
private JTextField semnum = new JTextField("");
private JTextField name = new JTextField ("");
private JTextField course = new JTextField ("");
private JTextField year = new JTextField ("");
private JTextField scs = new JTextField("");
private JTextField idnum = new JTextField("");
private JPanel fakebuttons = new JPanel (new GridLayout (4,0,10,10));
private JButton addSubject = new JButton("Add Subject..");
private JButton deleteSubject = new JButton("Delete Subject..");
private JButton editSubject = new JButton("Edit Subject..");
private JButton totalLoad = new JButton("Total Load : ");
private JButton GPA = new JButton ("GPA : ");
private JTextField units_textfield = new JTextField(3);
private JTextField gpa_textfield = new JTextField (7);
private JButton displayInfo = new JButton("Display Data");
private JPanel addPanel = new JPanel(new GridLayout(3,2,20,10));
private JLabel subjectName = new JLabel("Subject: ");
private JTextField subject [] ;
private JLabel gradeLabel = new JLabel("Grade : ");
private JLabel unitsLabel = new JLabel ("Units : ");
private JButton addButton = new JButton ("Add");
String grade [] = {"1.00","1.25","1.50","1.75","2.00","2.25","2.50","2.75","3.00","5.0", "DRP", "INC", "WDRW"};
private JComboBox comboGrade [];
String unit [] = {"1","2","3","4","5","6"};
private JComboBox comboUnit[];
private JButton done = new JButton ("Done");
private JButton d_subj,d_unit,d_grade;
// private JTextField displaySubject = new JTextField();
// private JTextField displayUnit = new JTextField ();
//private JTextField displayGrade = new JTextField();
private JTextField displaySubject[];
private JTextField displayUnit[];
private JTextField displayGrade [];
private JLabel t_units = new JLabel ("Total Load : ");
private JTextField totalUnits = new JTextField ();
private JLabel gpa = new JLabel ("GPA : ");
private JTextField gpaTxt = new JTextField ();
private JLabel err = new JLabel ("No Entry Found");
private JLabel display1 = new JLabel ();
private JLabel display2 = new JLabel ();
private JLabel displayID = new JLabel ();
final String getName [] = new String [10];
final int getUnit [] = new int [10];
final String getGrade [] = new String [10];
public StudentGradeCalc()
{
Border border = BorderFactory.createLineBorder(null);
setLayout(null);
panelInfo.setBounds(5,5,280,150);
//panelInfo.setBorder(border);
panelInfo.add(Name);
panelInfo.add(name);
panelInfo.add(Course);
panelInfo.add(course);
panelInfo.add(Year);
panelInfo.add(year);
panelInfo.add(sy);
panelInfo.add(syear);
panelInfo.add(sem);
panelInfo.add(semnum);
panelInfo.add(college);
panelInfo.add(scs);
panelInfo.add(id);
panelInfo.add(idnum);
add(panelInfo);
fakebuttons.setBounds(5,160,280,160);
fakebuttons.add(addSubject);
fakebuttons.add(deleteSubject);
fakebuttons.add(editSubject);
fakebuttons.add(displayInfo);
add(fakebuttons);
//ADD SUBJECT BUTTON
addSubject.addActionListener(new ActionListener (){
public void actionPerformed (ActionEvent cags){
final JFrame addFrame = new JFrame ();
addFrame.setVisible(true);
addFrame.setSize (300,190);
addFrame.setResizable(false);
addFrame.setLocationRelativeTo(null);
addFrame.setTitle("Add Subject");
//addFrame.setLayout(new GridLayout (3,2,20,10));
addFrame.setLayout(null);
subject = new JTextField [] {
new JTextField (),
new JTextField (),
new JTextField (),
new JTextField (),
new JTextField (),
new JTextField (),
new JTextField (),
new JTextField (),
new JTextField (),
};
subjectName.setBounds (10,10,120,20);
subject[x].setBounds (75,10,170,20);
addFrame.add(subjectName);
addFrame.add(subject[x]);
gradeLabel.setBounds (10,40,100,20);
addFrame.add(gradeLabel);
comboGrade = new JComboBox [] {
new JComboBox (grade),
new JComboBox (grade),
new JComboBox (grade),
new JComboBox (grade),
new JComboBox (grade),
new JComboBox (grade),
new JComboBox (grade),
new JComboBox (grade),
new JComboBox (grade),
new JComboBox (grade),
};
comboGrade[x].setBounds(150,40,95,20);
addFrame.add(comboGrade[x]);
unitsLabel.setBounds (10, 70, 95, 20);
addFrame.add(unitsLabel);
comboUnit = new JComboBox [] {
new JComboBox(unit),
new JComboBox(unit),
new JComboBox(unit),
new JComboBox(unit),
new JComboBox(unit),
new JComboBox(unit),
new JComboBox(unit),
};
comboUnit[x].setBounds(150,70,95,20);
addFrame.add(comboUnit[x]);
addButton.setBounds (120,120,60,30);
addFrame.add(addButton);
w++;
addButton.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent k){
//JOptionPane.showMessageDialog(null, "Subject "+subject[x].getText()+ " has been added", "Add Subject", JOptionPane.INFORMATION_MESSAGE);
getName [x] = subject[x].getText();
getGrade[x] = String.valueOf((String)comboGrade[x].getSelectedItem());
getUnit [x] = Integer.valueOf((String)comboUnit[x].getSelectedItem());
x++;
addFrame.dispose();
}
});
}
});
//DISPLAY DATA BUTTON
displayInfo.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent m){
final String name_course_year = "Name : "+name.getText()+ " Course : " +course.getText()+"- "+year.getText()+ " ID : "+idnum.getText() ;
final String ubos = "College : "+scs.getText()+ " SY : "+syear.getText()+ " Term : "+semnum.getText() ;
//String nametanga [] = {name.getText() , };
if (name.getText().equals("")|| course.getText().equals("")||year.getText ().equals("") || idnum.getText().equals("")||syear.getText ().equals("") || scs.getText().equals("")|| semnum.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Complete the Information!", "ERROR", JOptionPane.WARNING_MESSAGE);
}
else
{
final JFrame display = new JFrame ();
display.setVisible(true);
//display.setSize (400,200);
//display.setSize (400,down+130);
display.setResizable(false);
display.setLocationRelativeTo(null);
display.setTitle("Student Info");
display.setLayout(null);
display1.setBounds (5,5,390,20);
display2.setBounds (5,25,390,20);
display1.setText(name_course_year);
display2.setText(ubos);
display.add(display1);
display.add(display2);
d_subj = new JButton ("Subject");
d_unit = new JButton ("Unit");
d_grade = new JButton ("Grade");
d_subj.setBounds(10,50,180,20);
d_unit.setBounds (200,50,90,20);
d_grade.setBounds(300,50,90,20);
display.add(d_subj);
display.add(d_unit);
display.add(d_grade);
displaySubject = new JTextField [] {
new JTextField(),
new JTextField(),
new JTextField(),
new JTextField(),
new JTextField(),
new JTextField(),
new JTextField(),
};
displayUnit = new JTextField [] {
new JTextField(),new JTextField(),new JTextField(),
new JTextField(),new JTextField(),new JTextField(),
new JTextField(),new JTextField(),
};
displayGrade = new JTextField [] {
new JTextField(),new JTextField(),new JTextField(),
new JTextField(),new JTextField(),new JTextField(),
new JTextField(),new JTextField(),
};
int y = 30;
int down = 80;
int z = 0;
for (int i = 0; i < x; i++)
{
displaySubject[i].setBounds(10,down,180,y);
displayUnit[i].setBounds(200,down,90,y);
displayGrade[i].setBounds(300,down,90,y);
displaySubject[i].setText(getName[i]);
displayUnit[i].setText(""+getUnit[i]);
displayGrade[i].setText(getGrade[i]);
display.add(displaySubject[i]);
display.add(displayUnit[i]);
display.add(displayGrade[i]);
displaySubject[i].setEditable(false);
displayUnit[i].setEditable(false);
displayGrade[i].setEditable(false);
down+=y+10;
//i++;
// z++;
}
display.setSize (400,down+130);
t_units.setBounds (100,down,70,25);
totalUnits.setBounds(180,down,50,25);
gpa.setBounds(260,down,50,25);
gpaTxt.setBounds(310,down,80,25);
display.add(t_units);
display.add(totalUnits);
display.add(gpa);
display.add(gpaTxt);
gpaTxt.setEditable(false);
totalUnits.setEditable(false);
done.setBounds (170,down+50,70,30);
display.add(done);
done.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent done){
display.dispose();
}
});
}
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize (300,355);
setResizable(false);
setLocationRelativeTo(null);
setTitle("Student Grade Calculator");
}
public void actionPerformed (ActionEvent vahn)
{
System.exit(1);
}
public static void main(String[] args) {
new StudentGradeCalc();
}
}
I do not know where to find the reason why my program makes this error.
I found your problem it's here addButton.addActionListener(). You always add a new ActionListener to your button instance, because of that, every time you push addSubject button you also add a new Listener to addButton. Don't do that in the future, add your Listeners to Components, in method of component construction.

JButton exits JPanel

I have multiple cancel, ok, and save buttons. When the user clicks one of these buttons, I need it to exit the JPanel, but not the whole program. I have tried multiple times and everything I do it exits the whole program. My code is below. I know there is a lot of stuff on my code and it can get confusing. Thanks in advance.
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
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.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
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);
// Make all necessary buttons
JButton next = new JButton("Next");
JButton save = new JButton("Save");
JButton save2 = new JButton("Save");
JButton save3 = new JButton("Save");
JButton save4 = new JButton("Save");
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");
JButton ecancel = new JButton("Cancel");
JButton fcancel = new JButton("Cancel");
JButton gcancel = new JButton("Cancel");
JButton hcancel = new JButton("Cancel");
//Make the drop down lists
String[] baudrates = {"57600", "115200", "128000", "256000"};
JComboBox baudlist = new JComboBox(baudrates);
String[] baudrates2 = {"57600", "115200", "128000", "256000"};
JComboBox baudlist2 = new JComboBox(baudrates2);
String[] bytesizes = {"7", "8"};
JComboBox bytelist = new JComboBox(bytesizes);
String[] bytesizes2 = {"7", "8"};
JComboBox bytelist2 = new JComboBox(bytesizes2);
String[] stopbit = {"1", "2"};
JComboBox stoplist = new JComboBox(stopbit);
String[] stopbit2 = {"1", "2"};
JComboBox stoplist2 = new JComboBox(stopbit2);
String[] flows = {"None", "Hardware","Xon", "Xoff"};
JComboBox flowlist = new JComboBox(flows);
String[] flows2 = {"None", "Hardware","Xon", "Xoff"};
JComboBox flowlist2 = new JComboBox(flows2);
String[] paritys = {"None", "Even", "Odd"};
JComboBox paritylist = new JComboBox(paritys);
String[] paritys2 = {"None", "Even", "Odd"};
JComboBox paritylist2 = new JComboBox(paritys2);
//Make all necessary labels
JLabel cardLabel = new JLabel("Card Number: ");
JLabel expLabel = new JLabel("Exp. Date (MM/YY): ");
JLabel cvvLabel = new JLabel("CVV: ");
JLabel ipLabel = new JLabel("IP Address: ");
JLabel connectLabel = new JLabel("Connect Time: ");
JLabel sendLabel = new JLabel("Send Time Out: ");
JLabel receiveLabel = new JLabel("Receive Time Out: ");
JLabel portLabel = new JLabel("Port: ");
JLabel baudrate = new JLabel("Baud Rate: ");
JLabel bytesize = new JLabel("Byte Size: ");
JLabel stopbits = new JLabel("Stop Bits: ");
JLabel flow = new JLabel("Flow Con..: ");
JLabel parity = new JLabel("Parity: ");
JLabel stoLabel = new JLabel("Send Time Out: ");
JLabel rtoLabel = new JLabel("Receive Time Out: ");
JLabel portLabel2 = new JLabel("Port: ");
JLabel baudrate2 = new JLabel("Baud Rate: ");
JLabel bytesize2 = new JLabel("Byte Size: ");
JLabel stopbits2 = new JLabel("Stop Bits: ");
JLabel flow2 = new JLabel("Flow Con..: ");
JLabel parity2 = new JLabel("Parity: ");
JLabel stoLabel2 = new JLabel("Send Time Out: ");
JLabel rtoLabel2 = new JLabel("Receive Time Out: ");
JLabel portLabel3 = new JLabel("Port: ");
JLabel vendor = new JLabel("Vendor ID: ");
JLabel product = new JLabel("Product ID: ");
JLabel stoLabel3 = new JLabel("Send Time Out: ");
JLabel rtoLabel3 = new JLabel("Receive Time Out: ");
JLabel amountLabel = new JLabel("Amount: ");
JLabel textLabel = new JLabel("Display Text: ");
//Make all necessary TextFields
JTextField card = new JTextField(10);
JTextField expDate = new JTextField(10);
JTextField cvv = new JTextField(10);
JTextField ip = new JTextField(10);
JTextField ct = new JTextField(10);
JTextField rto = new JTextField(10);
JTextField sto = new JTextField(10);
JTextField port = new JTextField(10);
JTextField sendto = new JTextField(10);
JTextField reto = new JTextField(10);
JTextField comport = new JTextField(10);
JTextField sendto2 = new JTextField(10);
JTextField reto2 = new JTextField(10);
JTextField comport2 = new JTextField(10);
JTextField vendorid = new JTextField(10);
JTextField productid = new JTextField(10);
JTextField sendtime = new JTextField(10);
JTextField receiveto = new JTextField(10);
JTextField amountbox = new JTextField(10);
JTextField textBox = new JTextField(10);
JTextArea logbox = new JTextArea();
//Add components to the panels
final JPanel ethernetSettings = new JPanel();
ethernetSettings.setLayout(new GridLayout(6, 2));
ethernetSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
ethernetSettings.add(ipLabel);
ethernetSettings.add(ip);
ethernetSettings.add(connectLabel);
ethernetSettings.add(ct);
ethernetSettings.add(receiveLabel);
ethernetSettings.add(rto);
ethernetSettings.add(sendLabel);
ethernetSettings.add(sto);
ethernetSettings.add(portLabel);
ethernetSettings.add(port);
ethernetSettings.add(save);
ethernetSettings.add(ecancel);
final JPanel usbHIDSettings = new JPanel();
usbHIDSettings.setLayout(new GridLayout(5, 2));
usbHIDSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
usbHIDSettings.add(vendor);
usbHIDSettings.add(vendorid);
usbHIDSettings.add(product);
usbHIDSettings.add(productid);
usbHIDSettings.add(stoLabel3);
usbHIDSettings.add(sendtime);
usbHIDSettings.add(rtoLabel3);
usbHIDSettings.add(receiveto);
usbHIDSettings.add(save4);
usbHIDSettings.add(hcancel);
final JPanel usbCDCSettings = new JPanel();
usbCDCSettings.setLayout(new GridLayout(9, 2));
usbCDCSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
usbCDCSettings.add(baudrate2);
usbCDCSettings.add(baudlist);
usbCDCSettings.add(bytesize2);
usbCDCSettings.add(bytelist);
usbCDCSettings.add(stopbits2);
usbCDCSettings.add(stoplist);
usbCDCSettings.add(flow2);
usbCDCSettings.add(flowlist);
usbCDCSettings.add(parity2);
usbCDCSettings.add(paritylist);
usbCDCSettings.add(stoLabel2);
usbCDCSettings.add(sendto2);
usbCDCSettings.add(rtoLabel2);
usbCDCSettings.add(reto2);
usbCDCSettings.add(portLabel3);
usbCDCSettings.add(comport2);
usbCDCSettings.add(save3);
usbCDCSettings.add(gcancel);
final JPanel rsSettings = new JPanel();
rsSettings.setLayout(new GridLayout(9, 2));
rsSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
rsSettings.add(baudrate);
rsSettings.add(baudlist2);
rsSettings.add(bytesize);
rsSettings.add(bytelist2);
rsSettings.add(stopbits);
rsSettings.add(stoplist2);
rsSettings.add(flow);
rsSettings.add(flowlist2);
rsSettings.add(parity);
rsSettings.add(paritylist2);
rsSettings.add(stoLabel);
rsSettings.add(sendto);
rsSettings.add(rtoLabel);
rsSettings.add(reto);
rsSettings.add(portLabel2);
rsSettings.add(comport);
rsSettings.add(save2);
rsSettings.add(fcancel);
JRadioButton ethernet = new JRadioButton("Ethernet");
ethernet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog esettings = new JDialog(frame);
esettings.setTitle("Ethernet Settings");
esettings.add(ethernetSettings);
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.add(rsSettings);
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.add(usbCDCSettings);
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.add(usbHIDSettings);
usbh.setSize(400, 400);
usbh.pack();
usbh.setVisible(true);
}
});
final JPanel PortSettings = new JPanel();
PortSettings.setLayout(new GridLayout(3, 4));
PortSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
PortSettings.add(ethernet);
PortSettings.add(rs);
PortSettings.add(usbcdc);
PortSettings.add(usbhid);
PortSettings.add(next);
PortSettings.add(bcancel);
final JPanel accountPanel = new JPanel();
accountPanel.setLayout(new GridLayout(4, 2));
accountPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
accountPanel.add(cardLabel);
accountPanel.add(card);
accountPanel.add(expLabel);
accountPanel.add(expDate);
accountPanel.add(cvvLabel);
accountPanel.add(cvv);
accountPanel.add(bok);
accountPanel.add(ccancel);
JRadioButton apprve = new JRadioButton("Approve");
JRadioButton decline = new JRadioButton("Decline");
final JPanel apprvordecl = new JPanel();
apprvordecl.setLayout(new GridLayout(3, 2));
apprvordecl.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
apprvordecl.add(apprve);
apprvordecl.add(decline);
apprvordecl.add(textLabel);
apprvordecl.add(textBox);
apprvordecl.add(aok);
apprvordecl.add(acancel);
final JPanel amountPanel = new JPanel();
amountPanel.setLayout(new GridLayout(2, 2));
amountPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
amountPanel.add(amountLabel);
amountPanel.add(amountbox);
amountPanel.add(cok);
amountPanel.add(dcancel);
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);
llpPanel.add(logbox);
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
}
}
I have tried multiple times and everything I do it exits the whole program.
I don't see you code for closing the dialog so I can't guess what you might be doing wrong.
In your ActionListener you would invoke the dispose method on the dialog. The code would be something like:
JButton button = (JButton)event.getSource();
Window dialog = SwingUtilities.windowForComponent( button );
dialog.dispose();
A different approach might be to use a JOptionPane, then you don't need to code your own ActionListeners. You can easily add a panel to an option pane. The code below shows two approaches:
import java.awt.*;
import javax.swing.*;
public class OptionPanePanel
{
private static void createAndShowUI()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo( null );
frame.setVisible( true );
// Build a custom panel
JPanel panel = new JPanel( new GridLayout(2, 2) );
panel.add( new JLabel("First Name") );
JTextField firstName = new JTextField(10);
// firstName.addAncestorListener( new RequestFocusListener(false) );
panel.add( firstName );
panel.add( new JLabel("Last Name") );
JTextField lastName = new JTextField(10);
panel.add( lastName );
int result = JOptionPane.showConfirmDialog(
null, // use your JFrame here
panel,
"Enter Name",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE);
if(result == JOptionPane.YES_OPTION)
{
System.out.println(firstName.getText() + " : " + lastName.getText());
}
else
{
System.out.println("Canceled");
}
// Let Option Pane build the panel for you
Object[] msg = {"First Name:", firstName, "Last Name:", lastName};
result = JOptionPane.showConfirmDialog(
frame,
msg,
"Enter Name...",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.YES_OPTION)
{
System.out.println(firstName.getText() + " : " + lastName.getText());
}
else
{
System.out.println("Canceled");
}
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
One problem with this approach is that focus will be on the OK button instead of the text field. To get around this you can use the RequestFocusListener found in Dialog Focus.

Java Gui writing to output file

My program is showing that it is running successfully, but my dialogue box will not show up. I am new to Java and I am not sure where I am going wrong can someone help? I am trying to write to a file that will store the input from the user
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package database;
/**
*
*
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
`enter code here`import javax.swing.*;
`public class Database extends JFrame implements ActionListener{
FlowLayout flow = new FlowLayout();
JLabel fName = new JLabel("First Name");
JTextField fName1 = new JTextField(15);
JLabel blankSpaces1 = new JLabel(" ");
JLabel lName = new JLabel("Last Name");
JTextField lName1 = new JTextField(15);
JLabel blankSpaces2 = new JLabel(" ");
JLabel age = new JLabel("Age");
JTextField age1 = new JTextField(3);
JLabel blankSpaces3 = new JLabel(" ");
JLabel email = new JLabel("Email Address");
JTextField email1 = new JTextField(30);
JLabel blankSpaces4 = new JLabel(" ");
JLabel phone = new JLabel("Cell Phone Number");
JTextField phone1 = new JTextField(10);
JLabel blankSpaces5 = new JLabel(" ");
JLabel blankSpaces6 = new JLabel(" ");
JButton enter = new JButton("Enter");
JButton reset = new JButton("Reset");
JButton exitButton = new JButton("Exit");
Container con = getContentPane();
public void Database (){
Database Dab = new Database();
con.setLayout(flow);
setTitle("Antwain's Database");
Dab.setSize(800,300);
setVisible(true);
con.add(fName);
con.add(blankSpaces1);
con.add(fName1);
con.add(blankSpaces1);
con.add(lName);
con.add(lName1);
con.add(blankSpaces2);
con.add(age);
con.add(age1);
con.add(blankSpaces3);
con.add(email);
con.add(email1);
con.add(blankSpaces4);
con.add(phone);
con.add(phone1);
con.add(blankSpaces5);
con.add(blankSpaces6);
con.add(blankSpaces2);
con.add(enter);
con.add(exitButton);
con.add(reset);
enter.addActionListener(this);
exitButton.addActionListener(this);
reset.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e1){
Object source = e1.getSource();
if(source == enter){
String fName = fName1.getText();
String lName = lName1.getText();
String age = age1.getText();
String email = email1.getText();
String phone = phone1.getText();
}
else
if(source == reset){
fName1.setText("");
lName1.setText("");
age1.setText("");
email1.setText("");
phone1.setText("");
}
else
{
// if the user clicks on the Exit button (source is Exit button)
System.exit(0);
}
}
public static void main(String[] args) {
Database dab = new Database();
}
}
A lot more you need to go, fixed something you wrongly did in your code. Hope you can identify your mistakes.
public class DataBaseFrame extends JFrame implements ActionListener {
FlowLayout flow = new FlowLayout();
JLabel fName = new JLabel("First Name");
JTextField fName1 = new JTextField(15);
JLabel blankSpaces1 = new JLabel("");
JLabel lName = new JLabel("Last Name");
JTextField lName1 = new JTextField(15);
JLabel blankSpaces2 = new JLabel("");
JLabel age = new JLabel("Age");
JTextField age1 = new JTextField(3);
JLabel blankSpaces3 = new JLabel("");
JLabel email = new JLabel("Email Address");
JTextField email1 = new JTextField(30);
JLabel blankSpaces4 = new JLabel("");
JLabel phone = new JLabel("Cell Phone Number");
JTextField phone1 = new JTextField(10);
JLabel blankSpaces5 = new JLabel("");
JLabel blankSpaces6 = new JLabel("");
JButton enter = new JButton("Enter");
JButton reset = new JButton("Reset");
JButton exitButton = new JButton("Exit");
public DataBaseFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Antwain's Database");
JPanel panel = new JPanel(new FlowLayout());
panel.add(fName);
panel.add(blankSpaces1);
panel.add(fName1);
panel.add(blankSpaces1);
panel.add(lName);
panel.add(lName1);
panel.add(blankSpaces2);
panel.add(age);
panel.add(age1);
panel.add(blankSpaces3);
panel.add(email);
panel.add(email1);
panel.add(blankSpaces4);
panel.add(phone);
panel.add(phone1);
panel.add(blankSpaces5);
panel.add(blankSpaces6);
panel.add(blankSpaces2);
panel.add(enter);
panel.add(exitButton);
panel.add(reset);
getContentPane().add(panel);
enter.addActionListener(this);
exitButton.addActionListener(this);
reset.addActionListener(this);
}
public void actionPerformed(ActionEvent e1) {
Object source = e1.getSource();
if (source == enter) {
String fName = fName1.getText();
String lName = lName1.getText();
String age = age1.getText();
String email = email1.getText();
String phone = phone1.getText();
} else if (source == reset) {
fName1.setText("");
lName1.setText("");
age1.setText("");
email1.setText("");
phone1.setText("");
} else {
// if the user clicks on the Exit button (source is Exit button)
System.exit(0);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
DataBaseFrame dab = new DataBaseFrame();
dab.setSize(800, 300);
dab.setVisible(true);
}
});
}
}
new Database();
calls an empty Constructor, because you dont define none.
Change public void Database() {} to public Database()
Also, you have to eliminate Database Dab = new Database(); from your Constructor and change the Line Dab.setSize(800,300); to setSize(800,300);
Your Code will work when you make this changes but you should really work on your Programming Style and Code Formatting.

Categories