I am to stimulate a java applet of a menu with checkboxes and checkboxgroups, with calories listed next to each food choice. My problem is I cannot figure out why I keep getting this error message when i compile it:
AnAppletWithCheckboxes.java:188: illegal forward reference
int crepeVal = Integer.parseInt(textField.getText());
I get that for every line that I have is converting the textField into an int, so hence there are 17 errors. Any idea of why will be appreciated!
Here is my Code:: it is very long because there are plenty checkboxes and textFields. I commented Out My place of Error! :
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AnAppletWithCheckboxes extends Applet implements ItemListener {
public void init() {
setLayout(new GridLayout(1, 0));
CheckboxGroup dinnerType = new CheckboxGroup();
standard = new Checkbox("standard", dinnerType, false);
standard.addItemListener(this);
deluxe = new Checkbox("deluxe", dinnerType, true);
deluxe.addItemListener(this);
CheckboxGroup appetizers = new CheckboxGroup();
crepe = new Checkbox("crepe", appetizers, false);
crepe.addItemListener(this);
quiche = new Checkbox("quiche", appetizers, false);
quiche.addItemListener(this);
dumpling = new Checkbox("dumpling", appetizers, false);
dumpling.addItemListener(this);
textField = new TextField("300");
textField.setEditable(false);
textField2 = new TextField("330");
textField2.setEditable(false);
textField3 = new TextField("98");
textField3.setEditable(false);
CheckboxGroup soupOrSalad = new CheckboxGroup();
soup = new Checkbox("Soup", soupOrSalad, false);
soup.addItemListener(this);
CheckboxGroup soups = new CheckboxGroup();
cream = new Checkbox("cream", soups, false);
cream.addItemListener(this);
broth = new Checkbox("broth", soups, false);
broth.addItemListener(this);
gumbo = new Checkbox("gumbo", soups, false);
gumbo.addItemListener(this);
textField4 = new TextField("190");
textField4.setEditable(false);
textField5 = new TextField("20");
textField5.setEditable(false);
textField6 = new TextField("110");
textField6.setEditable(false);
salad = new Checkbox("Salad", soupOrSalad, false);
salad.addItemListener(this);
CheckboxGroup salads = new CheckboxGroup();
tossed = new Checkbox("tossed", salads, false);
tossed.addItemListener(this);
caesar = new Checkbox("caesar", salads, false);
caesar.addItemListener(this);
croutons = new Checkbox("croutons");
croutons.addItemListener(this);
lite = new Checkbox("lite dressing");
lite.addItemListener(this);
textField7 = new TextField("35");
textField7.setEditable(false);
textField8 = new TextField("90");
textField8.setEditable(false);
textField9 = new TextField("60");
textField9.setEditable(false);
textField10 = new TextField("50");
textField10.setEditable(false);
CheckboxGroup entrees = new CheckboxGroup();
chicken = new Checkbox("chicken", entrees, false);
chicken.addItemListener(this);
beef = new Checkbox("beef", entrees, false);
beef.addItemListener(this);
lamb = new Checkbox("lamb", entrees, false);
lamb.addItemListener(this);
fish = new Checkbox("fish", entrees, false);
fish.addItemListener(this);
textField11 = new TextField("200");
textField11.setEditable(false);
textField12 = new TextField("170");
textField12.setEditable(false);
textField13 = new TextField("65");
textField13.setEditable(false);
textField14 = new TextField("150");
textField14.setEditable(false);
CheckboxGroup deserts = new CheckboxGroup();
pie = new Checkbox("pie", deserts, false);
pie.addItemListener(this);
fruit = new Checkbox("fruit", deserts, false);
fruit.addItemListener(this);
sherbet = new Checkbox("sherbet", deserts, false);
sherbet.addItemListener(this);
textField15 = new TextField("80");
textField15.setEditable(false);
textField16 = new TextField("60");
textField16.setEditable(false);
textField17 = new TextField("107");
textField17.setEditable(false);
calories = new Button("Calories");
calTextField = new TextField("0"+total);
calTextField.setEditable(false);
setLayout(new GridLayout(0, 1));
Panel p = new Panel();
add(p);
p.add(standard);
p.add(deluxe);
appetizerPanel = new Panel();
add(appetizerPanel);
label = new Label("Appetizer");
appetizerPanel.add(label);
appetizerPanel.add(crepe);
appetizerPanel.add(textField);
appetizerPanel.add(quiche);
appetizerPanel.add(textField2);
appetizerPanel.add(dumpling);
appetizerPanel.add(textField3);
soupPanel = new Panel();
add(soupPanel);
soupPanel.add(soup);
soupPanel.add(cream);
soupPanel.add(textField4);
soupPanel.add(broth);
soupPanel.add(textField5);
soupPanel.add(gumbo);
soupPanel.add(textField6);
saladPanel = new Panel();
add(saladPanel);
saladPanel.add(salad);
saladPanel.add(tossed);
saladPanel.add(textField7);
saladPanel.add(caesar);
saladPanel.add(textField8);
saladPanel.add(croutons);
saladPanel.add(textField9);
saladPanel.add(lite);
saladPanel.add(textField10);
entreePanel = new Panel();
add(entreePanel);
label2 = new Label("Entree");
entreePanel.add(label2);
entreePanel.add(chicken);
entreePanel.add(textField11);
entreePanel.add(beef);
entreePanel.add(textField12);
entreePanel.add(lamb);
entreePanel.add(textField13);
entreePanel.add(fish);
entreePanel.add(textField14);
desertPanel = new Panel();
add(desertPanel);
label3 = new Label("Desert");
desertPanel.add(label3);
desertPanel.add(pie);
desertPanel.add(textField15);
desertPanel.add(fruit);
desertPanel.add(textField16);
desertPanel.add(sherbet);
desertPanel.add(textField17);
caloriesPanel = new Panel();
add(caloriesPanel);
caloriesPanel.add(calories);
caloriesPanel.add(calTextField);
}
public void LabelChange(Label b) {
if (b ==label3)
b.setForeground(Color.lightGray);
else
label3.setForeground(Color.black);
}
/* This is where i get those errors!! */
int crepeVal = Integer.parseInt(textField.getText());
int quicheVal = Integer.parseInt(textField2.getText());
int dumplingVal = Integer.parseInt(textField3.getText());
int creamVal = Integer.parseInt(textField4.getText());
int brothVal = Integer.parseInt(textField5.getText());
int gumboVal = Integer.parseInt(textField6.getText());
int tossedVal = Integer.parseInt(textField7.getText());
int caesarVal = Integer.parseInt(textField8.getText());
int croutonsVal = Integer.parseInt(textField9.getText());
int liteVal = Integer.parseInt(textField10.getText());
int chickenVal = Integer.parseInt(textField11.getText());
int beefVal = Integer.parseInt(textField12.getText());
int lambVal = Integer.parseInt(textField13.getText());
int fishVal = Integer.parseInt(textField14.getText());
int pieVal = Integer.parseInt(textField15.getText());
int fruitVal = Integer.parseInt(textField16.getText());
int sherbetVal = Integer.parseInt(textField17.getText());
public boolean action(Event evt, Object whatAction){
if(!(evt.target instanceof Button)){
return false;
}
else {
calorieCount();
return true;
}
}
public void calorieCount () {
if (crepe.getState())
crepeVal += total;
else
crepeVal = 0;
if (quiche.getState())
quicheVal += total;
else
quicheVal = 0;
if (dumpling.getState())
dumplingVal += total;
else
dumplingVal= 0;
if (cream.getState())
creamVal += total;
else
creamVal = 0;
if (broth.getState())
brothVal += total;
else
brothVal = 0;
if (gumbo.getState())
gumboVal += total;
else
gumboVal = 0;
if (tossed.getState())
tossedVal += total;
else
tossedVal = 0;
if (caesar.getState())
caesarVal += total;
else
caesarVal = 0;
if (croutons.getState())
croutonsVal += total;
else
croutonsVal = 0;
if (lite.getState())
liteVal += total;
else
liteVal = 0;
if (chicken.getState())
chickenVal += total;
else
chickenVal = 0;
if (beef.getState())
beefVal += total;
else
beefVal = 0;
if (lamb.getState())
lambVal += total;
else
lambVal = 0;
if (fish.getState())
fishVal += total;
else
fishVal = 0;
if (pie.getState())
pieVal += total;
else
pieVal = 0;
if (fruit.getState())
fruitVal += total;
else
fruitVal = 0;
if (sherbet.getState())
sherbetVal += total;
else
sherbetVal = 0;
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == standard || e.getSource() == deluxe)
handleDinnerType((Checkbox)e.getSource());
else if (e.getSource() == soup || e.getSource() == salad)
handleSoupSaladChoice((Checkbox)e.getSource());
}
void handleDinnerType(Checkbox selectedType) {
boolean enabled;
if (selectedType == standard){
enabled = false;
LabelChange(label3);
}
else{
enabled = true;
LabelChange(label);
}
soup.setEnabled(enabled);
salad.setEnabled(enabled);
pie.setEnabled(enabled);
fruit.setEnabled(enabled);
sherbet.setEnabled(enabled);
cream.setEnabled(enabled);
broth.setEnabled(enabled);
gumbo.setEnabled(enabled);
tossed.setEnabled(enabled);
caesar.setEnabled(enabled);
croutons.setEnabled(enabled);
lite.setEnabled(enabled);
}
void handleSoupSaladChoice(Checkbox selectedCourse) {
boolean soupEnabled, saladEnabled;
if (selectedCourse == soup) {
soupEnabled = true;
saladEnabled = false;
soup.setForeground(Color.BLACK);
salad.setForeground(Color.lightGray);
}
else {
soupEnabled = false;
saladEnabled = true;
soup.setForeground(Color.lightGray);
salad.setForeground(Color.BLACK);
}
cream.setEnabled(soupEnabled);
broth.setEnabled(soupEnabled);
gumbo.setEnabled(soupEnabled);
tossed.setEnabled(saladEnabled);
caesar.setEnabled(saladEnabled);
croutons.setEnabled(saladEnabled);
lite.setEnabled(saladEnabled);
}
Label
label, label2, label3;
Panel
appetizerPanel, soupPanel, saladPanel, entreePanel, desertPanel, caloriesPanel;
Checkbox
standard, deluxe,
soup, salad,
crepe, quiche, dumpling,
cream, broth, gumbo,
tossed, caesar,
croutons, lite,
chicken, beef, lamb, fish,
pie, fruit, sherbet;
Button calories;
int total = 0;
TextField
textField, textField2, textField3,
textField4, textField5, textField6,
textField7, textField8, textField9,
textField10, textField11, textField12,
textField13, textField14, textField15,
textField16, textField17, calTextField;
}
In Java, you are not allowed to refer to a field a in an initializer of another field b if b is declared before a in the source code. That is, this is allowed:
int b = 3;
int a = b;
but this is not allowed:
int a = b;
int b = 3;
By the way, if Java had some magic way of figuring out that textField had to be initialized before crepeVal, you would still get a NullPointerException since textField is null after object construction.
Related
So I build a shell that looks like the following
After I click the change path button, I prompt for the path and then set the text. The problem is after doing so new text gets cut off if its too long like so.
I am using a Label to display the path, and the only method I'm using in the listener regarding the Laebl is the setText() method. Is there a way to not have this happen? I am using SWT and prefer to maintain the grid layout so I can have the 2 columns. Any info would be helpful. Thank you.
Here's the code
String unit[] = {"Pixil", "Inch"};
final CustomAttribute realWidth = new CustomAttribute(String.valueOf(obj.getGraph().getWidth(null)));
final CustomAttribute realHeight = new CustomAttribute(String.valueOf(obj.getGraph().getHeight(null)));
double[] sizes = obj.convertToSizes();
if(sizes != null){
realWidth.setValue(sizes[0]);
realHeight.setValue(sizes[1]);
}
final Shell shell = new Shell(d);
shell.setImage(ApplicationPlugin.getImage(ApplicationPlugin.QUATTRO_ICON));
shell.setLayout(new org.eclipse.swt.layout.GridLayout(2,true));
shell.setText(EDIT_IMG_WINDOW_TITLE);
//width info
final Label labelWidth = new Label(shell, SWT.HORIZONTAL);
final Text textWidth = new Text(shell,SWT.SINGLE | SWT.BORDER);
//height info
final Label labelHeight = new Label(shell, SWT.HORIZONTAL);
final Text textHeight = new Text(shell,SWT.SINGLE | SWT.BORDER);
//units info
final Combo unitCombo = new Combo (shell, SWT.READ_ONLY);
final Button ratioBox = new Button (shell, SWT.CHECK);
ratioBox.setText(EDIT_IMG_RADIO);
//path info
final Button pathButton = new Button (shell, SWT.PUSH);
pathButton.setText(EDIT_IMG_PATH);
final Label pathText = new Label(shell, SWT.HORIZONTAL);
pathText.setText(obj.getTextData()[0]);
Button change = new Button (shell, SWT.PUSH);
change.setText(EDIT_IMG_SAVE_BUTTON);
ModifyListener heightListener = new ModifyListener(){
public void modifyText(ModifyEvent e) {
if(realImgListen && textHeight.getText() != ""){
realImgListen = false;
try{
double oldHeight = realHeight.getDouble();
double newHeight = Double.parseDouble(textHeight.getText());
double oldWidth = Double.parseDouble(textWidth.getText());
if(unitCombo.getSelectionIndex() == 1){
newHeight = newHeight * designer.getPixilPerInch();
oldWidth = oldWidth * designer.getPixilPerInch();
}
realHeight.setValue(newHeight);
double[] sizes = obj.convertToSizes();
if(sizes != null)
realWidth.setValue(sizes[0]);
else
realWidth.setValue(oldWidth);
if(realHeight.getDouble() > SheetCanvas.sheetYSize)
realHeight.setValue(SheetCanvas.sheetYSize);
if(realHeight.getDouble() < 1)
realHeight.setValue(1);
if(ratioBox.getSelection() == true){
double scale = Double.parseDouble(realHeight.getValue()) / oldHeight;
realWidth.setValue(String.valueOf(realWidth.getDouble()*scale));
if(unitCombo.getSelectionIndex() == 0)
textWidth.setText(String.valueOf((int)Math.round(Double.parseDouble(realWidth.getValue()))));
else
textWidth.setText(String.valueOf(Double.parseDouble(realWidth.getValue())/designer.getPixilPerInch()));
}
obj.storeSizingInfo(realWidth.getDouble(), realHeight.getDouble(), 0);
realImgListen = true;
}
catch(NumberFormatException e2){
realImgListen = true;
}
}
}
};
ModifyListener widthListener = new ModifyListener(){
public void modifyText(ModifyEvent e) {
if(realImgListen && textHeight.getText() != ""){
realImgListen = false;
try{
double oldWidth = realWidth.getDouble();
double newWidth = Double.parseDouble(textWidth.getText());
double oldHeight = Double.parseDouble(textHeight.getText());
if(unitCombo.getSelectionIndex() == 1){
newWidth = newWidth * designer.getPixilPerInch();
oldHeight = oldHeight * designer.getPixilPerInch();
}
realWidth.setValue(newWidth);
double[] sizes = obj.convertToSizes();
if(sizes != null)
realHeight.setValue(sizes[1]);
else
realHeight.setValue(oldHeight);
if(realWidth.getDouble() > SheetCanvas.sheetYSize)
realWidth.setValue(SheetCanvas.sheetYSize);
if(realWidth.getDouble() < 1)
realWidth.setValue(1);
if(ratioBox.getSelection() == true){
double scale = Double.parseDouble(realWidth.getValue()) / oldWidth;
realHeight.setValue(String.valueOf(realHeight.getDouble()*scale));
if(unitCombo.getSelectionIndex() == 0)
textHeight.setText(String.valueOf((int)Math.round(Double.parseDouble(realHeight.getValue()))));
else
textHeight.setText(String.valueOf(Double.parseDouble(realHeight.getValue())/designer.getPixilPerInch()));
}
obj.storeSizingInfo(realWidth.getDouble(), realHeight.getDouble(), 0);
realImgListen = true;
}
catch(NumberFormatException e2){
realImgListen = true;
}
}
}
};
textHeight.addModifyListener(heightListener);
textWidth.addModifyListener(widthListener);
unitCombo.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
realImgListen = false;
if(unitCombo.getSelectionIndex() == 0){
textWidth.setText(String.valueOf((int)Math.rint(realWidth.getDouble())));
textHeight.setText(String.valueOf((int)Math.rint(realHeight.getDouble())));
}
else{
textWidth.setText(String.valueOf(realWidth.getDouble()/designer.getPixilPerInch()));
textHeight.setText(String.valueOf(realHeight.getDouble()/designer.getPixilPerInch()));
}
realImgListen = true;
}
});
change.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
Double width = realWidth.getDouble();
Double height = realHeight.getDouble();
String line1;
if(obj.getTextData() != null)
line1 = obj.getTextData()[0];
else
line1 = null;
String[] textData = {line1,null};
obj.setTextData(textData);
obj.storeSizingInfo(Math.rint(width), Math.rint(height),0);
designer.updateDataFromSource(settings);
designer.repaint();
updatePanelButtons();
shell.close();
}
});
pathButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//path button
String path = null;
path = getPathFromBrowser(FILE_CHOOSER_IMAGE_TITLE_STR,DEFAULT_PATH,acceptedImgFormats,null,SWT.OPEN);
pathText.setText(path);
if(path != null){
String[] paths = settings.getCustomImagePath();
int pathIndex = 0;
for(int i = 0; i < paths.length; i++){
if(obj.getTextData()[0].equals(paths[i]))
pathIndex = i;
}
paths[pathIndex] = path;
settings.setCustomImagePath(paths);
paths = settings.getCustomImageChoices();
paths[pathIndex] = getFileNameFromPath(path);
settings.setCustomImageChoices(paths);
designer.updateDataFromSource(settings);
String[] textData = obj.getTextData();
textData[0] = path;
if(textData.length > 1)
textData[1] = null;
obj.setTextData(textData);
}
}
});
textWidth.setTextLimit(7);
textHeight.setTextLimit(7);
labelWidth.setText(EDIT_IMG_WIDTH);
labelHeight.setText(EDIT_IMG_HEIGHT);
unitCombo.setItems(unit);
unitCombo.select(0);
ratioBox.setSelection(true);
org.eclipse.swt.graphics.Rectangle clientArea = shell.getClientArea();
unitCombo.setBounds(clientArea.x, clientArea.y, 300, 200);
shell.pack();
shell.open();
Shell shellTemp = SWT_AWT.new_Shell(d, designer);
Monitor primary = d.getPrimaryMonitor();
org.eclipse.swt.graphics.Rectangle bounds = primary.getBounds();
org.eclipse.swt.graphics.Rectangle rect = shell.getBounds();
int x = bounds.x + (bounds.width - rect.width) / 2;
int y = bounds.y + (bounds.height - rect.height) / 2;
shell.setLocation(x, y);
shell.moveAbove(shellTemp);
shellTemp.dispose();
shell.addListener(SWT.Close, new Listener() {
#Override
public void handleEvent(Event event) {
editingImg();
}
});
realImgListen = false;
textWidth.setText(String.valueOf((int)Double.parseDouble(realWidth.getValue())));
textHeight.setText(String.valueOf((int)Double.parseDouble(realHeight.getValue())));
realImgListen = true;
The width of your columns is currently being set to the width of the widest control - probably 'Maintain Image Ratio'. If you set the the path text to anything longer than that it will be truncated.
You could set a width hint on the path control to set its size:
GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
data.widthHint = 100; // You choose the width
pathText.setLayoutData(data);
Note that you have specified that the columns are of equal width so this will add similar space to the first column. You might want to switch to unequal width columns.
Alternatively you can ask the Shell to recalculate the size after you set the new text. Just call:
shell.pack();
It looks like that you are redrawing the change path label components and you are not wrapping the label .
That why at the time of redrawing some part of the text is not visible.
Use warping or you can also increase the screen size
I'm very new to java.My question is,is it possible to use JButton v,v1 ? Inside this actionPerformed() method ? If it is possible,then how ?
Thanks in advance .
PlaceOrder.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class PlaceOrder extends JFrame implements ActionListener
{
JButton confirm,cancel,logOut;
JRadioButton rb1,rb2,rb3,rb4,rb5,rb6,rb7,rb8;
JTextField r1,r2,r3,r4,r5,r6,r7,r8;
JLabel Item,Quantity;
Food f[] = new Food[10];
double total = 0;
public PlaceOrder()
{
f[0] = new Food("Chicken Burger",120);
f[1] = new Food("Chicken BBQ Burger",150);
f[2] = new Food("Chicken BBQ Cheese Burger",170);
f[3] = new Food("Beef Burger",130);
f[4] = new Food("Beef BBQ Burger",160);
f[5] = new Food("Pizza",180);
f[6] = new Food("Coffee",80);
f[7] = new Food("Soft Drinks(Can)",35);
setSize(700,700);
setVisible(true);
setLayout(null);
rb1 = new JRadioButton("Chicken Burger");
rb2 = new JRadioButton("Chicken BBQ Burger");
rb3 = new JRadioButton("Chicken BBQ Cheese Burger");
rb4 = new JRadioButton("Beef Burger");
rb5 = new JRadioButton("Beef BBQ Burger");
rb6 = new JRadioButton("Pizza");
rb7 = new JRadioButton("Coffee");
rb8 = new JRadioButton("Soft Drinks(Can)");
rb1.setBounds(150,100,150,30);
add(rb1);
rb2.setBounds(150,130,150,30);
add(rb2);
rb3.setBounds(150,160,150,30);
add(rb3);
rb4.setBounds(150,190,150,30);
add(rb4);
rb5.setBounds(150,220,150,30);
add(rb5);
rb6.setBounds(150,250,150,30);
add(rb6);
rb7.setBounds(150,280,150,30);
add(rb7);
rb8.setBounds(150,310,150,30);
add(rb8);
r1 = new JTextField();
r2 = new JTextField();
r3 = new JTextField();
r4 = new JTextField();
r5 = new JTextField();
r6 = new JTextField();
r7 = new JTextField();
r8 = new JTextField();
Item = new JLabel("Item");
Quantity = new JLabel("Quantity");
Item.setBounds(160,20,100,50);
add(Item);
Quantity.setBounds(360,20,100,50);
add(Quantity);
r1.setBounds(350,100,150,20);
add(r1);
r2.setBounds(350,130,150,20);
add(r2);
r3.setBounds(350,160,150,20);
add(r3);
r4.setBounds(350,190,150,20);
add(r4);
r5.setBounds(350,220,150,20);
add(r5);
r6.setBounds(350,250,150,20);
add(r6);
r7.setBounds(350,280,150,20);
add(r7);
r8.setBounds(350,310,150,20);
add(r8);
confirm = new JButton("Order Status");
cancel = new JButton("Cancel Order");
logOut = new JButton("LogOut");
confirm.setBounds(150,500,120,30);
add(confirm);
cancel.setBounds(380,500,120,30);
add(cancel);
logOut.setBounds(550,50,80,30);
add(logOut);
confirm.addActionListener(this);
cancel.addActionListener(this);
logOut.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource() == confirm)
{
JFrame YourOrder = new JFrame();
YourOrder.setSize(700,800);
YourOrder.setLayout(null);
YourOrder.setVisible(true);
JLabel confirmation = new JLabel("Your Order");
confirmation.setBounds(250,30,150,30);
YourOrder.add(confirmation);
JLabel s[] = new JLabel[3];
s[0] = new JLabel("Item");
s[0].setBounds(100,100,50,30);
YourOrder.add(s[0]);
s[1] = new JLabel("Quantity");
s[1].setBounds(250,100,50,30);
YourOrder.add(s[1]);
s[2] = new JLabel("Total");
s[2].setBounds(400,100,50,30);
YourOrder.add(s[2]);
JLabel l,l1,l2;
JLabel netTotal = new JLabel("Net Amount = ");
netTotal.setBounds(400,550,100,30);
YourOrder.add(netTotal);
JButton v,v1;
v = new JButton("Change");
v.setBounds(100,600,100,30);
YourOrder.add(v);
v.addActionListener(this);
v1 = new JButton("Confirm Order");
v1.setBounds(400,600,120,30);
YourOrder.add(v1);
v1.addActionListener(this);
if(rb1.isSelected() && r1.getText() != "")
{
total = total + ((Integer.parseInt(r1.getText()))*(f[0].getFoodPrice()));
l = new JLabel(rb1.getText());
l.setBounds(100,150,100,30);
YourOrder.add(l);
l1 = new JLabel(r1.getText());
l1.setBounds(250,150,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r1.getText()))*(f[0].getFoodPrice())));
l2.setBounds(400,150,100,30);
YourOrder.add(l2);
}
if(rb2.isSelected() && r2.getText() != "")
{
total = total + ((Integer.parseInt(r2.getText()))*(f[1].getFoodPrice()));
l = new JLabel(rb2.getText());
l.setBounds(100,200,100,30);
YourOrder.add(l);
l1 = new JLabel(r2.getText());
l1.setBounds(250,200,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r2.getText()))*(f[1].getFoodPrice())));
l2.setBounds(400,200,100,30);
YourOrder.add(l2);
}
if(rb3.isSelected() && r3.getText() != "")
{
total = total + ((Integer.parseInt(r3.getText()))*(f[2].getFoodPrice()));
l = new JLabel(rb3.getText());
l.setBounds(100,250,100,30);
YourOrder.add(l);
l1 = new JLabel(r3.getText());
l1.setBounds(250,250,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r3.getText()))*(f[2].getFoodPrice())));
l2.setBounds(400,250,100,30);
YourOrder.add(l2);
}
if(rb4.isSelected() && r4.getText() != "")
{
total = total + ((Integer.parseInt(r4.getText()))*(f[3].getFoodPrice()));
l = new JLabel(rb4.getText());
l.setBounds(100,300,100,30);
YourOrder.add(l);
l1 = new JLabel(r4.getText());
l1.setBounds(250,300,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r4.getText()))*(f[3].getFoodPrice())));
l2.setBounds(400,300,100,30);
YourOrder.add(l2);
}
if(rb5.isSelected() && r5.getText() != "")
{
total = total + ((Integer.parseInt(r5.getText()))*(f[4].getFoodPrice()));
l = new JLabel(rb5.getText());
l.setBounds(100,350,100,30);
YourOrder.add(l);
l1 = new JLabel(r5.getText());
l1.setBounds(250,350,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r5.getText()))*(f[4].getFoodPrice())));
l2.setBounds(400,350,100,30);
YourOrder.add(l2);
}
if(rb6.isSelected() && r6.getText() != "")
{
total = total + ((Integer.parseInt(r6.getText()))*(f[5].getFoodPrice()));
l = new JLabel(rb6.getText());
l.setBounds(100,400,100,30);
YourOrder.add(l);
l1 = new JLabel(r6.getText());
l1.setBounds(250,400,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r6.getText()))*(f[5].getFoodPrice())));
l2.setBounds(400,400,100,30);
YourOrder.add(l2);
}
if(rb7.isSelected() && r7.getText() != "")
{
total = total + ((Integer.parseInt(r7.getText()))*(f[6].getFoodPrice()));
l = new JLabel(rb7.getText());
l.setBounds(100,450,100,30);
YourOrder.add(l);
l1 = new JLabel(r7.getText());
l1.setBounds(250,450,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r7.getText()))*(f[6].getFoodPrice())));
l2.setBounds(400,450,100,30);
YourOrder.add(l2);
}
if(rb8.isSelected() && r8.getText() != "")
{
total = total + ((Integer.parseInt(r8.getText()))*(f[7].getFoodPrice()));
l = new JLabel(rb8.getText());
l.setBounds(100,500,100,30);
YourOrder.add(l);
l1 = new JLabel(r8.getText());
l1.setBounds(250,500,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r8.getText()))*(f[7].getFoodPrice())));
l2.setBounds(400,500,100,30);
YourOrder.add(l2);
}
JLabel pay = new JLabel(String.valueOf(total));
pay.setBounds(500,550,100,30);
YourOrder.add(pay);
if(a.getSource() == v)
{
YourOrder.setVisible(false);
}
else if(a.getSource() == v1)
{
JOptionPane.showMessageDialog(this,"Printing Invoice ...");
}
}
else if(a.getSource() == cancel)
{
this.setVisible(false);
PlaceOrder o1 = new PlaceOrder();
}
else
{
this.setVisible(false);
}
}
}
Food.java
class Food
{
private String foodName;
private double foodPrice;
public Food(String foodName,double foodPrice)
{
this.foodName = foodName;
this.foodPrice = foodPrice;
}
public String getFoodName()
{
return this.foodName;
}
public double getFoodPrice()
{
return this.foodPrice;
}
}
These two classes ..
My guess: you want to be able to refer to the local variables in your code image above, v and v1, but being local to the method, their scope or "visibility" is also limited to the method. If so, then get that code out of the actionPerformed and instead into its own class, one that creates a JPanel that is set up as you desire. You can then make v and v1 fields of the class, and allow outside code access to necessary properties via getter methods.
If you need more detailed help and less guesses, again please post a viable mcve. If you do this, we can work with and even sometimes help enhance your code.
Other issues: avoid null layouts and setBounds since these lead to rigid and hard to enhance GUI's that look bad on most platforms other than your own.
Also check out The Use of Multiple JFrames, Good/Bad Practice?
Edit
That confirm order window should be a modal JDialog and not a JFrame. You want the program to stop and not go forward until the user has dealt with it, which is what a modal dialog will do. Also, I would use JSpinner and not JTextFields to get the quantity information. Why? You must always assume that the user is an idiot, and using a JSpinner you would prevent the user from entering non-numeric or other incorrect information.
Alright, this one is a bit of a doozy, its about 240 lines long, but on to my point. The point is that it doesn't work. I'm pretty new to coding in general and some help would be really appreciated. I know where the problem is, it's in the last bits of it in the for loops, I went under the false presumption that if you click a JButton, it is then selected. How do I implement the .setSelected method to my program without rewriting the entire code? This code is a make-shift jeopardy of sorts btw.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import sun.audio.*;
import java.io.*;
public class Jeopardy implements ActionListener
{
public static JFrame frame = new JFrame("Jeopardy!");
public static JButton[] row1Buttons = new JButton[6];
public static JButton[] row2Buttons = new JButton[6];
public static JButton[] row3Buttons = new JButton[6];
public static JButton[] row4Buttons = new JButton[6];
public static JButton[] row5Buttons = new JButton[6];
//Creates 5 arrays, each with 6 JButtons in each (otherwise mass confusion
//is going to ensue because I would have to individually add 30 JButtons to the pane,
//all of which I would have to remember what they contain.)
public static JPanel pane = new JPanel();
public static JLabel[] labels = new JLabel[6];
//Creates an array of 6 JLabels
public static void main(String[] args)
{
frame.setSize(800, 600);
frame.setVisible(true);
frame.toFront();
frame.setContentPane(pane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font font = new Font("ComicSans", Font.PLAIN, 20);
Color colour = new Color(0, 0, 240);
for(int a = 0; a < row1Buttons.length; a++)
{
row1Buttons[a] = new JButton("$200");
row2Buttons[a] = new JButton("$400");
row3Buttons[a] = new JButton("$600");
row4Buttons[a] = new JButton("$800");
row5Buttons[a] = new JButton("$1000");
//Gives text to all 30 JButtons
row1Buttons[a].addActionListener(new Jeopardy());
row2Buttons[a].addActionListener(new Jeopardy());
row3Buttons[a].addActionListener(new Jeopardy());
row4Buttons[a].addActionListener(new Jeopardy());
row5Buttons[a].addActionListener(new Jeopardy());
//adds action listener to all 30
row1Buttons[a].setBackground(colour);
row2Buttons[a].setBackground(colour);
row3Buttons[a].setBackground(colour);
row4Buttons[a].setBackground(colour);
row5Buttons[a].setBackground(colour);
//sets background colour
row1Buttons[a].setForeground(Color.yellow);
row2Buttons[a].setForeground(Color.yellow);
row3Buttons[a].setForeground(Color.yellow);
row4Buttons[a].setForeground(Color.yellow);
row5Buttons[a].setForeground(Color.yellow);
}
labels[0] = new JLabel("Mathematics");
labels[1] = new JLabel("Computer Science");
labels[2] = new JLabel("Historical Events");
labels[3] = new JLabel("Chemistry");
labels[4] = new JLabel("TBD");
labels[5] = new JLabel("TBD");
for(int k = 0; k < labels.length; k++)
{
labels[k].setForeground(Color.yellow);
}
pane.setLayout(new GridLayout(6, 6, 6, 6));
for(int b = 0; b < labels.length; b++)
{
pane.add(labels[b]);
pane.add(row1Buttons[b]);
pane.add(row2Buttons[b]);
pane.add(row3Buttons[b]);
pane.add(row4Buttons[b]);
pane.add(row5Buttons[b]);
}
pane.setBackground(Color.blue);
}
public void actionPerformed(ActionEvent event)
{
String[] answerRow1 = new String[6];
String[] answerRow2 = new String[6];
String[] answerRow3 = new String[6];
String[] answerRow4 = new String[6];
String[] answerRow5 = new String[6];
String[] questionsRow1 = new String[6];
//Write $200 questions
questionsRow1[0] = "Question";
questionsRow1[1] = "Question";
questionsRow1[2] = "Question";
questionsRow1[3] = "Question";
questionsRow1[4] = "Question";
questionsRow1[5] = "Question";
String[] questionsRow2 = new String[6];
//Write $400 questions
questionsRow2[0] = "Question";
questionsRow2[1] = "Question";
questionsRow2[2] = "Question";
questionsRow2[3] = "Question";
questionsRow2[4] = "Question";
questionsRow2[5] = "Question";
String[] questionsRow3 = new String[6];
//Write $600 questions
questionsRow3[0] = ("The function y=3(2)^x will have this for a y value when x = 3.");
questionsRow3[1] = "Question";
questionsRow3[2] = "Question";
questionsRow3[3] = "Question";
questionsRow3[4] = "Question";
questionsRow3[5] = "Question";
String[] questionsRow4 = new String[6];
//Write $800 questions
questionsRow4[0] = "Question";
questionsRow4[1] = "Question";
questionsRow4[2] = "Question";
questionsRow4[3] = "Question";
questionsRow4[4] = "Question";
questionsRow4[5] = "Question";
String[] questionsRow5 = new String[6];
//Write $1000 questions
questionsRow5[0] = "Question";
questionsRow5[1] = "Question";
questionsRow5[2] = "Question";
questionsRow5[3] = "Question";
questionsRow5[4] = "Question";
questionsRow5[5] = "Question";
for(int j = 0; j < questionsRow1.length; j++)
{
if(row1Buttons[j].getModel().isPressed())
{
try
{
InputStream in = new FileInputStream("sounds/x.wav");
AudioStream openSound = new AudioStream(in);
AudioPlayer.player.start(openSound);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "You don't have the sound clip for this");
}
answerRow1[j] = JOptionPane.showInputDialog(null, questionsRow1[j]);
}
}
for(int a = 0; a < questionsRow2.length; a++)
{
if(row2Buttons[a].isSelected())
{
try
{
InputStream in = new FileInputStream("sounds/x.wav");
AudioStream openSound = new AudioStream(in);
AudioPlayer.player.start(openSound);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "You don't have the sound clip for this");
}
JOptionPane.showInputDialog(null, questionsRow2[a]);
}
}
for(int r = 0; r < questionsRow3.length; r++)
{
if(row3Buttons[r].isSelected())
{
try
{
InputStream in = new FileInputStream("sounds/x.wav");
AudioStream openSound = new AudioStream(in);
AudioPlayer.player.start(openSound);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "You don't have the sound clip for this");
}
JOptionPane.showInputDialog(null, questionsRow3[r]);
}
}
for(int e = 0; e < questionsRow4.length; e++)
{
if(row4Buttons[e].isSelected())
{
try
{
InputStream in = new FileInputStream("sounds/x.wav");
AudioStream openSound = new AudioStream(in);
AudioPlayer.player.start(openSound);
}
catch(Exception exception)
{
JOptionPane.showMessageDialog(null, "You don't have the sound clip for this");
}
JOptionPane.showInputDialog(null, questionsRow4[e]);
}
}
for(int d = 0; d < questionsRow5.length; d++)
{
if(row5Buttons[d].isSelected())
{
try
{
InputStream in = new FileInputStream("sounds/x.wav");
AudioStream openSound = new AudioStream(in);
AudioPlayer.player.start(openSound);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "You don't have the sound clip for this");
}
JOptionPane.showInputDialog(null, questionsRow5[d]);
}
}
}
}
The problem is that you make the frame visible and then add your components. Instead, call setVisible() at the end of your constructor, after you pack() the frame.
public static void main(String[] args) {
...
pane.setBackground(Color.blue);
frame.pack();
frame.setVisible(true);
}
try to create a new class for buttons
public class Response_Button extends JButton {
public Response_Button(final JPanel container, final String name) {
this.setText(name);
this.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JOptionPane.showMessageDialog(container, "clicked " + name);
}
});
}
like this each button will respond through it's own method
This is what fixes it:
if(event.getSource()== row1Buttons[j])
Yes, that makes it so that it will do all of the category in one fell swoop, but it doesn't matter. Thanks for all who tried and gave positive and helpful answers.
EDIT: nope, it doesn't do it all in one go, I just labeled it wrong.
This is my code for populating JcomboBox.
I try to select a text file from JFileChooser, then read and put into an array list and put it in the combo box. But my problem is, even i put it in the array list and show it(system.out.println(list)), but still can't populate to the combobox. What can i do for that?
public class Read2 extends JFrame implements ActionListener{
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 500;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
private JComboBox cb;
private JButton b;
private JPanel J;
int len = 0;
int room = 1;
int line = 1;
int north = 0;
int east = 0;
int west = 0;
int south = 0;
int up = 0;
int down = 0;
//String s="";
String s2="";
Room newRoom;
HashMap<Integer,Room> Map = new HashMap<Integer,Room>();
public Read2(){
Container contentPane;
//set the frame properties
setTitle ("Creat your own map");
setSize (FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
contentPane = getContentPane( );
contentPane.setLayout(new BorderLayout());
J= new JPanel();
J.setLayout(new BorderLayout());
cb = new JComboBox();
b = new JButton ("insert");
b.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
//String fileName="";//ask hillary for import
JFileChooser fc = new JFileChooser();
int r = fc.showOpenDialog(null);
if (r == JFileChooser.APPROVE_OPTION) {
File filename = fc.getSelectedFile();
//File directory = fc.getCurrentDirectory();
FileInputStream inputfile = null;
try {
inputfile = new FileInputStream(filename);
FileReader in = new FileReader(filename.getAbsoluteFile());
BufferedReader br=new BufferedReader(in);
String s1 ="";
String s= "";
while ((s1=br.readLine())!=null){
switch(line % 8){
case 1:
s = s1;
break;
case 2:
s2 = s1;
break;
case 3:
north = Integer.parseInt(s1);
break;
case 4:
east = Integer.parseInt(s1);
break;
case 5:
south = Integer.parseInt(s1);
break;
case 6:
west = Integer.parseInt(s1);
break;
case 7:
up = Integer.parseInt(s1);
break;
case 0:
down = Integer.parseInt(s1);
newRoom = new Room(s,s2, north, east, south, west, up, down);
Map.put(room, newRoom);
room++;
break;
}
line++;
//System.out.println(s1);
}
List<String> list = new ArrayList<String>();
//System.out.println(list);
if(list !=null){
//list = new ArrayList<String>();
//list = new ArrayList<String>();
for(int i = 1; i <=Map.size(); i++){
String d = Map.get(i).getImg();
list.add(d);
}
cb= new JComboBox(list.toArray());
//find out the problem for this and it will be solved
System.out.println(list);
inputfile.close();
}
}catch( IOException e){}
System.out.println("Could not find file");
}
}});
J.add(b, BorderLayout.NORTH);
J.add(cb,BorderLayout.CENTER);
contentPane.add(J,BorderLayout.CENTER);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Read2 r = new Read2();
r.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
You're creating a new JComboBox with the file results, but the UI still contains the old one. Just remove the old one and add the new one.
I have created a java GUI that works as a 6-question questionnaire. For each of the six question, I have 4 choices , i, ii, iii, iv for the user to choose, and then type out one of the four choices in a text box, then click a button "enter" to go to the next question. After all six questions have been answered, it will jump to a finish page.
I want to write the input value (in the text box) of each question to a text file. So After clicking on the "enter button" to answer all six questions I can see something like "i ii iii i ii iii" in a text file.
Is there a way to do it?
Here is my code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class HTML extends Applet implements ActionListener
{
private TextField question;
private Button enter, start;
int count = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
String text, input;
private Label intro1, intro2;
private Label qone1, qone2, qone3, qone4, qone5, qone6;
private Label qtwo1, qtwo2, qtwo3, qtwo4, qtwo5, qtwo6;
private Label qthree1, qthree2, qthree3, qthree4, qthree5, qthree6;
private Label qfour1, qfour2, qfour3, qfour4, qfour5, qfour6;
private Label qfive1, qfive2, qfive3, qfive4, qfive5, qfive6;
private Label qsix1, qsix2, qsix3, qsix4, qsix5, qsix6;
private Label finish1, finish2, finish3;
public void init()
{
setLayout(null);
start = new Button ("Start");
question = new TextField(10);
enter = new Button ("Enter");
if (count == 0)
{
setBackground( Color.yellow);
intro1 = new Label("Target Advertising", Label.CENTER);
intro1.setFont(new Font("Times-Roman", Font.BOLD, 16));
intro2 = new Label("Welcome to this questionnaire. In order to show the most appropriate advertisement, we would like to know more about your personal preferences.");
add(intro1);
add(intro2);
intro1.setBounds(0,0,800,20);
intro2.setBounds(15,20,800,20);
add(start);
start.setBounds(370,60,70,23);
start.addActionListener(this);
}
if(count == 1)
{
setBackground( Color.yellow );
qone1 = new Label("Question 1", Label.LEFT);
qone1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qone2 = new Label("On average, How many hours do you spend on playing sports every week?");
qone3 = new Label("i.0-2");
qone4 = new Label("ii.3-6");
qone5 = new Label("iii.7-10");
qone6 = new Label("iv.10+");
add(qone1);
add(qone2);
add(qone3);
add(qone4);
add(qone5);
add(qone6);
qone1.setBounds(15,0,800,20);
qone2.setBounds(15,20,800,15);
qone3.setBounds(15,60,800,15);
qone4.setBounds(15,80,800,15);
qone5.setBounds(15,100,800,15);
qone6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if (count == 2)
{
qtwo1 = new Label("Question 2", Label.LEFT);
qtwo1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qtwo2 = new Label("On average, How many hours do you spend on qsixening to music every week?");
qtwo3 = new Label("i. 0-4 ");
qtwo4 = new Label("ii. 5-10");
qtwo5 = new Label("iii. 11-20");
qtwo6 = new Label("iv. 20+");
add(qtwo1);
add(qtwo2);
add(qtwo3);
add(qtwo4);
add(qtwo5);
add(qtwo6);
qtwo1.setBounds(15,20,800,15);
qtwo2.setBounds(15,40,800,15);
qtwo3.setBounds(15,60,800,15);
qtwo4.setBounds(15,80,800,15);
qtwo5.setBounds(15,100,800,15);
qtwo6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 3)
{
qthree1 = new Label("Question 3", Label.LEFT);
qthree1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qthree2 = new Label("On average, How many hours do you spend on using computers every week?");
qthree3 = new Label("i.0-2");
qthree4 = new Label("ii.3-10");
qthree5 = new Label("iii.11-15");
qthree6 = new Label("iv.20+");
add(qthree1);
add(qthree2);
add(qthree3);
add(qthree4);
add(qthree5);
add(qthree6);
qthree1.setBounds(15,20,800,20);
qthree2.setBounds(15,40,800,15);
qthree3.setBounds(15,60,800,15);
qthree4.setBounds(15,80,800,15);
qthree5.setBounds(15,100,800,15);
qthree6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 4)
{
qfour1 = new Label("Question 4", Label.LEFT);
qfour1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qfour2 = new Label("On average, How many hours do you spend on groceries every week?");
qfour3 = new Label("i.0-2");
qfour4 = new Label("ii.3-10");
qfour5 = new Label("iii.11-15");
qfour6 = new Label("iv.20+");
add(qfour1);
add(qfour2);
add(qfour3);
add(qfour4);
add(qfour5);
add(qfour6);
qfour1.setBounds(15,20,800,15);
qfour2.setBounds(15,40,800,15);
qfour3.setBounds(15,60,800,15);
qfour4.setBounds(15,80,800,15);
qfour5.setBounds(15,100,800,15);
qfour6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 5)
{
qfive1 = new Label("Question 5", Label.LEFT);
qfive1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qfive2 = new Label("On average, How many hours do you spend on watching TV every week?");
qfive3 = new Label("i.0-2");
qfive4 = new Label("ii.3-10");
qfive5 = new Label("iii.11-15");
qfive6 = new Label("iv.20+");
add(qfive1);
add(qfive2);
add(qfive3);
add(qfive4);
add(qfive5);
add(qfive6);
qfive1.setBounds(15,20,800,15);
qfive2.setBounds(15,40,800,15);
qfive3.setBounds(15,60,800,15);
qfive4.setBounds(15,80,800,15);
qfive5.setBounds(15,100,800,15);
qfive6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 6)
{
qsix1 = new Label("Question 6", Label.LEFT);
qsix1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qsix2 = new Label("On average, How many times do you spend with family every week?");
qsix3 = new Label("i.0-2");
qsix4 = new Label("ii.3-10");
qsix5 = new Label("iii.11-15");
qsix6 = new Label("iv.20+");
add(qsix1);
add(qsix2);
add(qsix3);
add(qsix4);
add(qsix5);
add(qsix6);
qsix1.setBounds(15,20,800,15);
qsix2.setBounds(15,40,800,15);
qsix3.setBounds(15,60,800,15);
qsix4.setBounds(15,80,800,15);
qsix5.setBounds(15,100,800,15);
qsix6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
finish1 = new Label("Thank You." , Label.CENTER);
finish1.setFont(new Font("Times-Roman", Font.BOLD, 50));
finish2 = new Label("Questionnaire Completed.", Label.CENTER);
finish2.setFont(new Font("Times-Roman", Font.BOLD, 50));
add(finish1);
add(finish2);
finish1.setBounds(0,200,800,60);
finish2.setBounds(0,300,800,60);
}
}
public void actionPerformed(ActionEvent ae)
{
String button = ae.getActionCommand();
text = question.getText();
b = 0;
c = 0;
if (count == 6)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qsix1);
remove(qsix2);
remove(qsix3);
remove(qsix4);
remove(qsix5);
remove(qsix6);
if(input.equals("OL"))
{
b = 1;
count = 7;
init();
}
else
{
b = 2;
count = 7;
init();
}
}
if (count == 5)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qfive1);
remove(qfive2);
remove(qfive3);
remove(qfive4);
remove(qfive5);
remove(qfive6);
if(input.equals("BR"))
{
b = 1;
count = 6;
init();
}
else
{
b = 2;
count = 6;
init();
}
}
if (count == 4)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qfour1);
remove(qfour2);
remove(qfour3);
remove(qfour4);
remove(qfour5);
remove(qfour6);
}
if(input.equals("no"))
{
b = 1;
count = 5;
init();
}
else
{
b = 2;
count = 5;
init();
}
}
if (count == 3)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qthree1);
remove(qthree2);
remove(qthree3);
remove(qthree4);
remove(qthree5);
remove(qthree6);
if(input.equals("black"))
{
b = 1;
count = 4;
init();
}
else
{
b = 2;
count = 4;
init();
}
}
if (count == 2)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qtwo1);
remove(qtwo2);
remove(qtwo3);
remove(qtwo4);
remove(qtwo5);
remove(qtwo6);
if(input.equals("yes"))
{
b = 1;
count = 3;
init();
}
else
{
b = 2;
count = 3;
init();
}
}
if (count == 1)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qone1);
remove(qone2);
remove(qone3);
remove(qone4);
remove(qone5);
remove(qone6);
if(input.equals("i"))
{
b = 1;
count = 2;
init();
}
else
{
b = 1;
count = 1;
init();
}
}
if (count == 0)
{
remove(intro1);
remove(intro2);
remove(start);
count = 1;
init();
}
this.validate();
}
}
Sure, something like
OutputStream out = new FileOutputStream("/path/to/your/file");
out.write(answer.getBytes());
out.close();