Help with buttons in gui
I need to create first, last, next, and previous buttons in my gui. I have them in there (in the code), but only one button shows up (instead of four), and it has "first" overlapped with the "previous" button. The "next" and "last" don't even show up at all.
Can some one guide me please? Can someone tell me what I am doing wrong?? Thank you in advance
Here is my maker.java which houses the navigation panel
package inventorymain;
import javax.swing.JFrame;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.text.NumberFormat;
import javax.swing.border.*;
import java.net.*;
import java.util.StringTokenizer;
class Maker extends Inventory implements ActionListener
{//begins Class Maker
static String manufact[] = new String[10];
static double restockingFee[] = new double[10];
static int i;
static double TotalVal;
static int navItem;
private Container cp = getContentPane();
GridBagConstraints c;
GridBagConstraints cconstraint;
Border titledborder;
JPanel pan;
String labels[] = {"Product Name:", "Manufacturer:", "Product ID Number:", "Units in Stock:",
"Price Per Unit: $", "Restocking Fee: $", "Value of Product in Stock Plus Restocking Fee: $",
"Value of All Merchandise Plus Restocking Fees: $"};
int len1 = labels.length;
JLabel lbl[] = new JLabel[len1];
JTextField txtfield1[] = new JTextField[len1];
String blabels[] = {"First", "Previous", "Next", "Last"};
int blen = blabels.length;
JButton navigate[] = new JButton[blen];
JLabel lblImg;
File file;
public String FileName;
public Maker(int Item_Number, String Item_Name, double Item_Price, int Items_in_Stock, String manufact, double restockingFee)// Constructor for varibles
super(Item_Number, Item_Name, Item_Price, Items_in_Stock);
this.manufact[i] = manufact;
this.restockingFee[i] = restockingFee;
i = i + 1;
public static void setManufact(int k, String value)
manufact[k] = value;
public static double getRestockFee(int val)
return restockingFee[val];
public void ShowInventory()
setLayout(new FlowLayout());
GridBagLayout contlayout = new GridBagLayout();//layout for container
GridBagConstraints cconstraint = new GridBagConstraints();//constraint for container
GridBagLayout gblayout = new GridBagLayout();//layout for panel
GridBagConstraints gbconstraint = new GridBagConstraints();
try
//first panel
pan = new JPanel();
gblayout = new GridBagLayout();
gbconstraint = new GridBagConstraints();
pan.setLayout(gblayout);
for (int i = 0; i < 2; i++)
for (int j = 0; j < len1; j++)
int x = i;
int y = j;
if (x == 0)
lbl[j] = new JLabel(labels[j]);
lbl[j].setHorizontalAlignment(JLabel.LEFT);
lbl[j].setPreferredSize(new Dimension(250, 15));
gbconstraint.insets = new Insets(10, 0, 0, 0);
gbconstraint.gridx = x;
gbconstraint.gridy = y;
pan.add(lbl[j], gbconstraint);
}//end if
else
txtfield1[j] = new JTextField(15);
txtfield1[j].setPreferredSize(new Dimension(300, 15));
txtfield1[j].setHorizontalAlignment(JLabel.LEFT);
txtfield1[j].setEnabled(false);
lbl[j].setLabelFor(txtfield1[j]);
gbconstraint.gridx = x;
gbconstraint.gridy = y;
pan.add(txtfield1[j], gbconstraint);
}//end else
}//end for
}//end for
Border titledborder = BorderFactory.createTitledBorder("Current Inventory Data");
pan.setBorder(titledborder);
//adds panel to container
cconstraint.gridwidth = 1;
cconstraint.gridheight = 1;
cconstraint.gridx = 0;
cconstraint.gridy = 0;
cp.add(pan, cconstraint);
//add icon to display
pan = new JPanel();
gblayout = new GridBagLayout();
gbconstraint = new GridBagConstraints();
pan.setLayout(gblayout);
gbconstraint.gridwidth = 1;
gbconstraint.gridheight = 1;
gbconstraint.gridy = 0;
lblImg = new JLabel((new ImageIcon(getClass().getResource("logo111.jpg"))));
lblImg.setPreferredSize(new Dimension(120, 60));
pan.add(lblImg);
cconstraint.gridwidth = 1;
cconstraint.gridheight = 1;
cconstraint.gridx = 0;
cconstraint.gridy = 1;
cp.add(pan, cconstraint);
//navigation panel
pan = new JPanel();
gblayout = new GridBagLayout();
gbconstraint = new GridBagConstraints();
pan.setLayout(gblayout);
gbconstraint.gridwidth = 1;
gbconstraint.gridheight = 1;
gbconstraint.gridx = 0;
gbconstraint.gridy = 1;
for (int i = 0; i < blen; i++)
navigate[i] = new JButton(blabels);
gbconstraint.gridx = 1;
pan.add(navigate[i], gbconstraint);
navigate[i].addActionListener(this);
}//end for
titledborder = BorderFactory.createTitledBorder("Navigation");
pan.setBorder(titledborder);
//add panel to container
cconstraint.gridwidth = 4;
cconstraint.gridheight = 1;
cconstraint.gridx = 1;
cconstraint.gridy = 1;
cp.add(pan, cconstraint);
ShowInventory(0);
}//end try
catch (Exception e)
e.printStackTrace();
}//end catch
}//end showInventory
public void ShowInventory(int ItemNo)
txtfield1[0].setText(Integer.toString(ItemNo));
txtfield1[0].setText(Inventory.getItemName(ItemNo));
txtfield1[1].setText(manufact[ItemNo]);
txtfield1[2].setText(Integer.toString(Inventory.getItemNum(ItemNo)));
txtfield1[3].setText(Integer.toString(Inventory.getItemUnits(ItemNo)));
txtfield1[4].setText(Double.toString(Inventory.getItemPrice(ItemNo)));
txtfield1[5].setText(String.format("%3.2f",
Products.totalOfRestockFee(Inventory.getItemPrice(ItemNo),
getRestockFee(ItemNo))));
txtfield1[6].setText(String.format("%3.2f",
Products.totalOfInventory(Inventory.getItemPrice(ItemNo),
Inventory.getItemUnits(ItemNo), getRestockFee(ItemNo))));
txtfield1[7].setText(String.format("%3.2f",
GetTotalInvVal()));
}//end ShowInventory(int ItemNo)
public void EnableFields(boolean bflag)
txtfield1[1].setEnabled(bflag);
txtfield1[2].setEnabled(bflag);
txtfield1[3].setEnabled(bflag);
txtfield1[4].setEnabled(bflag);
txtfield1[5].setEnabled(bflag);
}//end EnableFields
public double GetTotalInvVal()
TotalVal = 0;
for(int j = 0; j < Inventory.getCount(); j++)
TotalVal += Products.totalOfInventory(Inventory.getItemPrice(j),
Inventory.getItemUnits(j), getRestockFee(j));
return TotalVal;
}//end GetTotalInvVal
public void actionPerformed(ActionEvent e)//button actions
String btnClicked = ((JButton)e.getSource()).getText();
if (btnClicked.equals("First"))
EnableFields(false);
navItem = 0;
ShowInventory(navItem);
}//end if
if (btnClicked.equals("Next"))
EnableFields(false);
if (navItem == getCount() - 1)
navItem = 0;
}//end if
else
navItem += 1;
}//end else
ShowInventory(navItem);
}//end if
if (btnClicked.equals("Previous"))
EnableFields(false);
if (navItem == 0)
navItem = getCount() - 1;
}//end if
else
navItem = navItem - 1;
}//end else
ShowInventory(navItem);
}//end if
if (btnClicked.equals("Last"))
EnableFields(false);
navItem = getCount() - 1;
ShowInventory(navItem);
}//end if
}//end ActionPerformed
}// end of class Maker
for (int i = 0; i < blen; i++)
navigate[i] = new JButton(blabels);
gbconstraint.gridx = 1; // ---- > You mean for this be = i, No?
// Somehow you need to be resetting the grid x for each button added
pan.add(navigate[i], gbconstraint);
navigate[i].addActionListener(this);
}//end for
Or check out gridx = GridBagConstraints.RELATIVE
Message was edited by:
nantucket
Similar Messages
-
Please Help - Need Help with Buttons for GUI for assignment. URGENT!!
Can someone please help me with the buttons on this program? I cannot figure out how to get them to work.
Thanks!!!
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.JButton;
public class InventoryTAH implements ActionListener
Maker[] proMaker;
JTextField[] fields;
NumberFormat nf;
public void actionPerformed(ActionEvent e)
int index = ((JComboBox)e.getSource()).getSelectedIndex();
populateFields(index);
public static void main(String[] args)
try
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
catch (Exception e)
System.err.println(e.getClass().getName() + ": " + e.getMessage());
InventoryTAH test = new InventoryTAH();
test.initMakers();
test.showGUI();
test.populateFields(0);
private void initMakers() {
proMaker = new Maker[10];
proMaker[0] = new Maker( 1, "Pens",1.59,100,"Bic");
proMaker[1] = new Maker( 2, "Pencils", .65, 100,"Mead");
proMaker[2] = new Maker( 3, "Markers", 1.29, 100,"Sharpie");
proMaker[3] = new Maker( 4, "Paperclips", 1.19, 100,"Staples");
proMaker[4] = new Maker( 5, "Glue", .85, 100,"Elmer's");
proMaker[5] = new Maker( 6, "Tape", .50, 100,"3m");
proMaker[6] = new Maker( 7, "Paper", 1.85, 100,"Mead");
proMaker[7] = new Maker( 8, "Stapler", 2.21, 100,"Swingline");
proMaker[8] = new Maker( 9, "Folders", .50, 100,"Mead");
proMaker[9] = new Maker( 10, "Rulers", .27, 100,"Stanley");
int maxNum = 10;
int currentNum = 0;
int currentInv = 0;
Action firstAction = new AbstractAction("First")
public void actionPerformed(ActionEvent evt)
currentInv = 0;
int populateFields;
JButton firstButton = new JButton(firstAction);
Action previousAction = new AbstractAction("Previous")
public void actionPerformed(ActionEvent evt)
currentInv--;
if (currentInv < 0)
currentInv = maxNum - 1;
int populateFields;
JButton previousButton = new JButton(previousAction);
Action nextAction = new AbstractAction("Next")
public void actionPerformed(ActionEvent evt)
currentInv++;
if (currentInv >= currentNum)
currentInv = 0;
int populateFields;
JButton nextButton = new JButton(nextAction);
Action lastAction = new AbstractAction("Last")
public void actionPerformed(ActionEvent evt)
currentInv = currentNum - 1;
int populateFields;
JButton lastButton = new JButton(lastAction);
JPanel buttonPanel = new JPanel( );
private void showGUI() {
JLabel l;
JButton button1;
JButton button2;
fields = new JTextField[8];
JFrame f = new JFrame("Inventory");
Container cp = f.getContentPane();
cp.setLayout(new GridBagLayout());
cp.setBackground(UIManager.getColor(Color.BLACK));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = GridBagConstraints.RELATIVE;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(2, 2, 2, 2);
c.anchor = GridBagConstraints.EAST;
cp.add(l = new JLabel("Item Number:", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('a');
cp.add(l = new JLabel("Item Name:", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('b');
cp.add(l = new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('c');
cp.add(l = new JLabel("Price per Unit: $", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('d');
cp.add(l = new JLabel("Total cost of Item: $", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('e');
cp.add(l = new JLabel("Total Value of Merchandise in Inventory: $", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('f');
cp.add(l = new JLabel("Manufacturer:", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('g');
cp.add(l = new JLabel("Restocking Fee: $", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('h');
c.gridx = 1;
c.gridy = 0;
c.weightx = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
cp.add(fields[0] = new JTextField(), c);
fields[0].setFocusAccelerator('a');
c.gridx = 1;
c.gridy = GridBagConstraints.RELATIVE;
cp.add(fields[1] = new JTextField(), c);
fields[1].setFocusAccelerator('b');
cp.add(fields[2] = new JTextField(), c);
fields[2].setFocusAccelerator('c');
cp.add(fields[3] = new JTextField(), c);
fields[3].setFocusAccelerator('d');
cp.add(fields[4] = new JTextField(), c);
fields[4].setFocusAccelerator('e');
cp.add(fields[5] = new JTextField(), c);
fields[5].setFocusAccelerator('f');
cp.add(fields[6] = new JTextField(), c);
fields[6].setFocusAccelerator('g');
cp.add(fields[7] = new JTextField(), c);
fields[7].setFocusAccelerator('h');
c.weightx = 0.0;
c.fill = GridBagConstraints.NONE;
cp.add(firstButton);
cp.add(previousButton);
cp.add(nextButton);
cp.add(lastButton);
JComboBox combo = new JComboBox();
for(int j = 0; j < proMaker.length; j++)
combo.addItem(proMaker[j].getName());
combo.addActionListener(this);
cp.add(combo);
cp.add(button1 = new JButton(" "), c);
f.pack();
f.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent evt)
System.exit(0);
f.setVisible(true);
private void populateFields(int index) {
Maker maker = proMaker[index];
fields[0].setText(Long.toString(maker.getNumberCode()));
fields[1].setText(maker.getName());
fields[2].setText(Long.toString(maker.getUnits()));
fields[3].setText(Double.toString(maker.getPrice()));
fields[4].setText(Double.toString(maker.getSum()));
fields[5].setText(Double.toString(maker.totalAllInventory(proMaker)));
fields[6].setText(maker.getManufact());
fields[7].setText(Double.toString(maker.getSum()*.05));
class Maker {
int itemNumber;
String name;
int units;
double price;
String manufacturer;
public Maker(int n, String name, double price, int units, String manufac) {
itemNumber = n;
this.name = name;
this.price = price;
this.units = units;
manufacturer = manufac;
public int getNumberCode() { return itemNumber; }
public String getName() { return name; }
public int getUnits() { return units; }
public double getPrice() { return price; }
public double getSum() { return units*price; }
public String getManufact() { return manufacturer; }
public double totalAllInventory(Maker[] makers) {
double total = 0;
for(int j = 0; j < makers.length; j++)
total += makers[j].getSum();
return total;
}}// I have made some modifications. Please try this.
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.JButton;
public class InventoryTAH implements ActionListener
Maker[] proMaker;
JTextField[] fields;
NumberFormat nf;
int currentInv = 0;
public void actionPerformed(ActionEvent e)
currentInv= ((JComboBox)e.getSource()).getSelectedIndex();
populateFields(currentInv);
public static void main(String[] args)
try
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
catch (Exception e)
System.err.println(e.getClass().getName() + ": " + e.getMessage());
InventoryTAH test = new InventoryTAH();
test.initMakers();
test.showGUI();
test.populateFields(0);
private void initMakers() {
proMaker = new Maker[10];
proMaker[0] = new Maker( 1, "Pens",1.59,100,"Bic");
proMaker[1] = new Maker( 2, "Pencils", .65, 100,"Mead");
proMaker[2] = new Maker( 3, "Markers", 1.29, 100,"Sharpie");
proMaker[3] = new Maker( 4, "Paperclips", 1.19, 100,"Staples");
proMaker[4] = new Maker( 5, "Glue", .85, 100,"Elmer's");
proMaker[5] = new Maker( 6, "Tape", .50, 100,"3m");
proMaker[6] = new Maker( 7, "Paper", 1.85, 100,"Mead");
proMaker[7] = new Maker( 8, "Stapler", 2.21, 100,"Swingline");
proMaker[8] = new Maker( 9, "Folders", .50, 100,"Mead");
proMaker[9] = new Maker( 10, "Rulers", .27, 100,"Stanley");
int maxNum = 10;
int currentNum = 0;
Action firstAction = new AbstractAction("First")
public void actionPerformed(ActionEvent evt)
currentInv = 0;
populateFields(currentInv);
JButton firstButton = new JButton(firstAction);
Action previousAction = new AbstractAction("Previous")
public void actionPerformed(ActionEvent evt)
currentInv--;
if (currentInv < 0)
currentInv = maxNum - 1;
populateFields(currentInv);
JButton previousButton = new JButton(previousAction);
Action nextAction = new AbstractAction("Next")
public void actionPerformed(ActionEvent evt)
currentInv++;
if (currentInv >= maxNum)
currentInv = 0;
populateFields(currentInv);
JButton nextButton = new JButton(nextAction);
Action lastAction = new AbstractAction("Last")
public void actionPerformed(ActionEvent evt)
currentInv = maxNum-1;
populateFields(currentInv);
JButton lastButton = new JButton(lastAction);
JPanel buttonPanel = new JPanel( );
private void showGUI() {
JLabel l;
JButton button1;
JButton button2;
fields = new JTextField[8];
JFrame f = new JFrame("Inventory");
Container cp = f.getContentPane();
cp.setLayout(new GridBagLayout());
cp.setBackground(UIManager.getColor(Color.BLACK));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = GridBagConstraints.RELATIVE;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(2, 2, 2, 2);
c.anchor = GridBagConstraints.EAST;
cp.add(l = new JLabel("Item Number:", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('a');
cp.add(l = new JLabel("Item Name:", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('b');
cp.add(l = new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('c');
cp.add(l = new JLabel("Price per Unit: $", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('d');
cp.add(l = new JLabel("Total cost of Item: $", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('e');
cp.add(l = new JLabel("Total Value of Merchandise in Inventory: $", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('f');
cp.add(l = new JLabel("Manufacturer:", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('g');
cp.add(l = new JLabel("Restocking Fee: $", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('h');
c.gridx = 1;
c.gridy = 0;
c.weightx = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
cp.add(fields[0] = new JTextField(), c);
fields[0].setFocusAccelerator('a');
c.gridx = 1;
c.gridy = GridBagConstraints.RELATIVE;
cp.add(fields[1] = new JTextField(), c);
fields[1].setFocusAccelerator('b');
cp.add(fields[2] = new JTextField(), c);
fields[2].setFocusAccelerator('c');
cp.add(fields[3] = new JTextField(), c);
fields[3].setFocusAccelerator('d');
cp.add(fields[4] = new JTextField(), c);
fields[4].setFocusAccelerator('e');
cp.add(fields[5] = new JTextField(), c);
fields[5].setFocusAccelerator('f');
cp.add(fields[6] = new JTextField(), c);
fields[6].setFocusAccelerator('g');
cp.add(fields[7] = new JTextField(), c);
fields[7].setFocusAccelerator('h');
c.weightx = 0.0;
c.fill = GridBagConstraints.NONE;
cp.add(firstButton);
cp.add(previousButton);
cp.add(nextButton);
cp.add(lastButton);
JComboBox combo = new JComboBox();
for(int j = 0; j < proMaker.length; j++)
combo.addItem(proMaker[j].getName());
combo.addActionListener(this);
cp.add(combo);
cp.add(button1 = new JButton(" "), c);
f.pack();
f.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent evt)
System.exit(0);
f.setVisible(true);
private void populateFields(int index) {
Maker maker = proMaker[index];
fields[0].setText(Long.toString(maker.getNumberCode()));
fields[1].setText(maker.getName());
fields[2].setText(Long.toString(maker.getUnits()));
fields[3].setText(Double.toString(maker.getPrice()));
fields[4].setText(Double.toString(maker.getSum()));
fields[5].setText(Double.toString(maker.totalAllInventory(proMaker)));
fields[6].setText(maker.getManufact());
fields[7].setText(Double.toString(maker.getSum()*.05));
class Maker {
int itemNumber;
String name;
int units;
double price;
String manufacturer;
public Maker(int n, String name, double price, int units, String manufac) {
itemNumber = n;
this.name = name;
this.price = price;
this.units = units;
manufacturer = manufac;
public int getNumberCode() { return itemNumber; }
public String getName() { return name; }
public int getUnits() { return units; }
public double getPrice() { return price; }
public double getSum() { return units*price; }
public String getManufact() { return manufacturer; }
public double totalAllInventory(Maker[] makers) {
double total = 0;
for(int j = 0; j < makers.length; j++)
total += makers[j].getSum();
return total;
}} -
hi all,
i'm new to flash but i have an idea whats going on with webpages. I need some help with buttons! i have a website and its has a stage and 9 different flv's (screens). I've been trying to read online and i can't find out how to navigate to different screens, example below
home | |
about us | stage |
contact | |
you click one one button it updates the stage. also i'm using actionscript 2.0.
thanks for any help ahead of time
Thank you,Is it a wi-fi or cellular connection that isn't working? If its wi-fi and other people/computers/phones can connect check your settings. If its cellular, are other people's phones (on the same carrier) working in the same location? If other people can't connect then it is likely a network problem. If others can you may need to reset/restore you iPhone.
-
Help with Times Table GUI applet
Hello,
I need help with an applet which inputs an integer from the user and displays the appropiate times table up to times 10 eg; user input 5 - display shows
5 time 1 is 5
5 times 2 is 10
5 times 10 is 50.
I have only managedt o get the display to show one statement eg 5 times 1 - I have tried using a for loop to show the whole table - but unlike a print statement each time the loop goes round it overwrites the data in the display box with the new data - any help would be much appreciated.
Note: it is for a programming course year 1 exercise - so I can only use basic constructs or loops to achieve this. Thanks in advance! Heres what I have so far:
import java.applet.* ;
import java.awt.* ;
import java.awt.event.* ;
public class TimesTableApplet extends Applet implements ActionListener
// Declare the GUI components globally
Label titleLabel, whichTableLabel ;
TextField whichTableBox, resultBox ;
Button showTableButton, clearButton ;
// Declare integer variables for holdind the number input by the user,
// the times number, and the result number
int whichTable, times=1, result ;
// Declare variables to hold string versions of the three integer variables
// above, for placing in the TextFields
String whichTableString, timesString, resultString ;
public void init ()
// Create the Labels
titleLabel = new Label ( "Times Table" ) ;
whichTableLabel = new Label ( "Which Table?" ) ;
// Create the TextFields
whichTableBox = new TextField ( 5 ) ;
resultBox = new TextField ( 30 ) ;
// Create the Buttons
showTableButton = new Button ( "Show Table" ) ;
clearButton = new Button ( "Clear" ) ;
// Add the components to the applet window
add ( titleLabel ) ;
add ( whichTableLabel ) ;
add ( whichTableBox ) ;
whichTableBox.addActionListener ( this ) ;
add ( resultBox ) ;
resultBox.setEditable ( false ) ;
add ( showTableButton ) ;
showTableButton.addActionListener ( this ) ;
add ( clearButton ) ;
clearButton.addActionListener ( this ) ;
} // End of init method
public void actionPerformed ( ActionEvent event )
// Find out which button generated the event
String arg = event.getActionCommand () ;
// If the user clicks the clear button, clear the whichTableBox and
// resultBox
if ( arg.equals ( "Clear" ) )
whichTableBox.setText ( "" ) ;
resultBox.setText ( "" ) ;
else
try
// Try extracting a string from the whichTableBox ( the users input )
// and converting it to an integer
whichTableString = whichTableBox.getText () ;
whichTable = Integer.parseInt ( whichTableString ) ;
// Clear status box
showStatus ( "" ) ;
// If the user clicks the showTableButton, display the appropiate
// times table up to times 10
if ( arg.equals ( "Show Table" ) ) ;
result = whichTable * times ;
timesString = Integer.toString ( times ) ;
resultString = Integer.toString ( result ) ;
resultBox.setText ( whichTableString + " times " + timesString + " is " + resultString ) ;
} // End of try block
catch ( NumberFormatException entry )
// Display error message and clear whichTableBox
showStatus ( "Error: Invalid Input - not an integer!" ) ;
whichTableBox.setText ( "" ) ;
} // End of catch block
} // End of else statement
} // End of actionPerformed method
} // End of classuse this code, please arrange your User interface correctly.
* TimesTableApplet.java
* Created on March 12, 2007, 12:08 PM
* @author cc.woon
import java.applet.* ;
import java.awt.* ;
import java.awt.event.* ;
public class TimesTableApplet extends Applet implements ActionListener
// Declare the GUI components globally
Label titleLabel, whichTableLabel ;
TextField whichTableBox ;
TextArea resultBox;
Button showTableButton, clearButton ;
// Declare integer variables for holdind the number input by the user,
// the times number, and the result number
int whichTable, times=1, result ;
// Declare variables to hold string versions of the three integer variables
// above, for placing in the TextFields
String whichTableString, timesString, resultString ;
public void init ()
// Create the Labels
titleLabel = new Label ( "Times Table" ) ;
whichTableLabel = new Label ( "Which Table?" ) ;
// Create the TextFields
whichTableBox = new TextField ( 5 ) ;
resultBox = new TextArea() ;
// Create the Buttons
showTableButton = new Button ( "Show Table" ) ;
clearButton = new Button ( "Clear" ) ;
// Add the components to the applet window
add ( titleLabel ) ;
add ( whichTableLabel ) ;
add ( whichTableBox ) ;
whichTableBox.addActionListener ( this ) ;
add ( resultBox ) ;
resultBox.setEditable ( false ) ;
add ( showTableButton ) ;
showTableButton.addActionListener ( this ) ;
add ( clearButton ) ;
clearButton.addActionListener ( this ) ;
} // End of init method
public void actionPerformed ( ActionEvent event )
// Find out which button generated the event
String arg = event.getActionCommand () ;
// If the user clicks the clear button, clear the whichTableBox and
// resultBox
if ( arg.equals ( "Clear" ) )
whichTableBox.setText ( "" ) ;
resultBox.setText ( "" ) ;
else
try
// Try extracting a string from the whichTableBox ( the users input )
// and converting it to an integer
whichTableString = whichTableBox.getText () ;
whichTable = Integer.parseInt ( whichTableString ) ;
// Clear status box
showStatus ( "" ) ;
// If the user clicks the showTableButton, display the appropiate
// times table up to times 10
if ( arg.equals ( "Show Table" ) ) ;
result = whichTable * times ;
timesString = Integer.toString ( times ) ;
String output = "";
for(int i =1;i<=10;i++){
output += whichTable +" times "+i + ": "+(i*whichTable)+"\n";
resultString = Integer.toString ( result ) ;
//resultBox.setText ( whichTableString + " times " + timesString + " is " + resultString ) ;
resultBox.setText ( output) ;
} // End of try block
catch ( NumberFormatException entry )
// Display error message and clear whichTableBox
showStatus ( "Error: Invalid Input - not an integer!" ) ;
whichTableBox.setText ( "" ) ;
} // End of catch block
} // End of else statement
} // End of actionPerformed method
} // End of class -
Help with button code please?
I am a novice and have just completed my first animated movie. I need help with action script.
The movie is currently looped and I have created and invisible button the size of the frame with the intention that a user can click on the movie image to stop it and on second click continue the movie from where it stopped.
I have used the AS event handler method to stop the movie. I have tested it and it works. But I cannot figure suitable code to restart the movie.
Two questions;
1. am I going about the task the right way? (should I use two buttons, one stop, one start)?
2. if my approach is feasible, can anyone give me the correct AS to re-start the movie and put me out of my misery please?you can use the following after changing "yourbutton" and "yourmovieclip" to use suitable instance names:
yourbutton.onRelease=function(){
if(!this.toggle){
yourmovieclip.stop();
} else {
yourmovieclip.play();
this.toggle=!this.toggle; -
Help with a better GUI??
hi everyone!!
i am thinking for a better GUI i have already made..earlier my GUI was not reflecting any changes but with the help and suggestions by many of you i have tried to change it..but i still find it is not reflecting to be a good interface.i really need some help..i am hereby posting the code ..hope to get a good GUI than i have..
Thanks a lot
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
public class front3 extends JFrame
JRadioButton split,zip,rename,unzip;
ButtonGroup radioGroup;
public front3()
Container c = getContentPane();
c.setLayout(new GridLayout(0,1));
c.setBackground(Color. cyan);
setSize(530,120);
setResizable(false); // to disable the maximize button
ImageIcon ip= new ImageIcon("title.jpg");
JLabel j1=new JLabel("Choose the operation to be done :",ip,JLabel.CENTER);
j1.setBackground(Color.red);
c.add(j1);
ImageIcon ii= new ImageIcon("split.jpg");
split=new JRadioButton("Splitter",ii,false);
split.setBackground(Color.cyan);
ImageIcon ij= new ImageIcon("zip.jpg");
zip=new JRadioButton("Zipper",ij,false);
zip.setBackground(Color.cyan);
ImageIcon ik= new ImageIcon("rename.jpg");
rename=new JRadioButton("Rename all",ik,false);
rename.setBackground(Color.cyan);
ImageIcon il= new ImageIcon("unzip.jpg");
unzip=new JRadioButton("unzip",il,false);
unzip.setBackground(Color.cyan);
c.add (split);
c.add(zip);
c.add(unzip);
c.add(rename);
// create logical relationship between JRadioButtons
radioGroup = new ButtonGroup();
radioGroup.add(split );
radioGroup.add( zip);
radioGroup.add(rename );
radioGroup.add( unzip);
// setPosition(250,250);
setTitle("FILE UTILITY SOFTWARE");
setSize(300,500);
setVisible(true);
split.addItemListener(new ItemListener()
public void itemStateChanged(ItemEvent event)
if(split.isSelected())
Gui file = new Gui();
zip.addItemListener(new ItemListener()
public void itemStateChanged(ItemEvent event)
if(zip.isSelected())
zipping file = new zipping();
unzip.addItemListener(new ItemListener()
public void itemStateChanged(ItemEvent event)
if(unzip.isSelected())
decompress file = new decompress();
rename.addItemListener(new ItemListener()
public void itemStateChanged(ItemEvent event)
if(rename.isSelected())
rename file = new rename();
public static void main(String args[])
front3 f=new front3();
f.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e)
System.exit(0);
}simply run this code to see the GUI-
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
public class front33 extends JFrame
JRadioButton split,zip,rename,unzip;
ButtonGroup radioGroup;
public front33()
Container c = getContentPane();
c.setLayout(new GridLayout(0,1));
c.setBackground(Color. cyan);
setSize(530,120);
setResizable(false); // to disable the maximize button
ImageIcon ip= new ImageIcon("title.jpg");
JLabel j1=new JLabel("Choose the operation to be done :",ip,JLabel.CENTER);
j1.setBackground(Color.red);
c.add(j1);
ImageIcon ii= new ImageIcon("split.jpg");
split=new JRadioButton("Splitter",ii,false);
split.setBackground(Color.cyan);
ImageIcon ij= new ImageIcon("zip.jpg");
zip=new JRadioButton("Zipper",ij,false);
zip.setBackground(Color.cyan);
ImageIcon ik= new ImageIcon("rename.jpg");
rename=new JRadioButton("Rename all",ik,false);
rename.setBackground(Color.cyan);
ImageIcon il= new ImageIcon("unzip.jpg");
unzip=new JRadioButton("unzip",il,false);
unzip.setBackground(Color.cyan);
c.add (split);
c.add(zip);
c.add(unzip);
c.add(rename);
// create logical relationship between JRadioButtons
radioGroup = new ButtonGroup();
radioGroup.add(split );
radioGroup.add( zip);
radioGroup.add(rename );
radioGroup.add( unzip);
// setPosition(250,250);
setTitle("FILE UTILITY SOFTWARE");
setSize(300,500);
setVisible(true);
public static void main(String args[])
front33 f=new front33();
f.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e)
System.exit(0);
} -
I am creating a GUI program, and I would like to know how to recognize a "mouseover" event on a button. Right now I am using "private JButton Button;" -- is it possible to have a mouseover for that?
If not, please tell me the steps I need to take in order to be able to have a mouseover event on a button. It is relatively important to the program =P
Thanks.
Edit -- I am going to be AFK for a while, please respond with as much info as possible. Thanks again ;-)I went to the help section on setRolloverIcon, and I kind of understand it, but what is an "Icon"?
What I want to happen when I move my mouse over the button is this: TextBox.setText("This button does this: bla bla bla");
Then I want the text in TextBox to go away when my mouse leaves the button. I know this is possible in some programs, but is it in Java? -
Help with buttons in photo gallery
Hello all
I am trying to find a more effeicent way to place buttons on each of my pages (Photo Gallery SIte). I have 150 pages and constantly have to link each button to the "next" or "last" button. Is there a more efficent way to for me to ad butons to each page.My site is Blazinginterest.com
Please give me some feedback on my site, possibly add a picture viewer?
Thank you, HDwhat code do you already have? Maybe we can help you modify
it where it's not working.
Also, how do you have it currently laid out? eg. do you have
several images on the screen, but some method to scroll through
other images? Have you done a search here or on director-online.com
(in the forum or articles section) on similar questions? Do you
have something written up to describe what features you need such
as a design document?
Just trying to get a sense of your level of programming in
general, experience with Director and what planning/details you
have worked out so far because there are so many different ways to
program a gallery. -
Need help with buttons in applet
stupid question maybe but i'm a beginner, and hey..there are no stupid questions, right? well, i may prove that wrong...anyway...
I can't relocate a button, or any other component for that matter. When I try to make a simple applet with just one button showing, then it works perfect...but when I make the applet a little bigger with some graphcis and text, the setLocation() and setSize() methods don't work. the wierd part is that I don't get an error message, the compiler just ignores those lines...so it seems anyway.
If anyone can help me I'd be very greatful
thanx a lotDo you know there are 5 layout managers in java. It is probably worth looking through each one and the commands they use.
The layout is determined by two things:
1.position Components are added
2.Layout manager used
Ive never had to use setLocation() and setSize() as the layouts do it for you:
1.Flowlayout: which is the default pane. e.g.
setLayout(new Flowlayout()).
2.GridLayout: postions panels into rows and columns
b.GridBagLayout
3.Borderlayout(NORTH,SOUTH,EAST,WEST and CENTER) postions in applet.
4.CardLayout(a bit like a slideshow)
5.Insets() which is used to determine top,bottom,left and right.
Don't bother about 4 or 5 yet. But definitly have a look at the other 3.
e.g. setLayout(new BorderLayout());
add("North", new Button("ok"));
add("CENTER", new Button("Exit");
etc.. -
Need help with buttons in a phone simulation
Hello
I have built a telephone interface to be used for training in a Call Center.
I am attempting to allow the user to press on the numberpad to enter passwords and such.
It is only a simulation and will only work if the user presses the correct buttons.
I am able to create the simulation when the user presses different buttons such as 1234
but I need them to press 0000 (zero 4 times)
I am not sure how to do this...
I have been playing around with the advanced actions, which has allowed me to accomplish the hide and show actions when the user clicks a button, but wondering if there is a way I can
have them click the 0 on the dialpad so each time it brings up the next zero
once its gets to the 4th one, it will advance to the next slide...
Hope that makes sense
I appreciate any help or advice
thanks
MelissaHi there,
What I did, I took a variable 'counter123' and assign '0' value to this variable.
I then added a button with Advanced action, I am sharing the Screenshot of the advanced action panel.
Screen shot 1 :
Incrementing the Value on click :
The value of the variable should increase on every click, so lets add a true statement as a condition.
Screenshot no 2 :
As normally on buttons/click boxes, the project will continue as soon we click them.
lets compare the value of our variable and then pause the project.
Screen shot 3 :
last if our purpose is solved, here we have clicked the button 4 times, so that has updated the value of our counter to 4, so as soon as the counter exceeds the value, we want the project to continue.
please let me know if this helps you.
This will help you in pressing a button 4 times, and i think you can also add actions to show and hide 'zeros' in your project.
I find such scenarios very interesting and I always find help on Lilybiri's Blog : http://lilybiri.posterous.com/
Thanks -
NEED HELP WITH BUTTONS IN AS3!!
Hello, I am stumped with an action scripting problem. I'm
trying to create a really basic website in flash using buttons to
jump from scene to scene. Let's say I want to do something as
simple as make scene 1 jump to scene two right? I'll place in my
scene 2 button on my first page:
But instead of the scene changing to the next scene after I
press the button, these errors come up!!
1180: Call to a possibly undefined method on.
1120: Access of undefined property release.
1120: Access of undefined property _root.
I've tried asking in so many different forums, I usually do
pretty good with action scripting for buttons. But this is my first
time doing it with cs3. I'm utterly confused, someone please help!!
Thank you.
The code on this page is the code I was using to try to
change scenes...Oh! and whenever I try to put a code in the actual
button, it won't let me. I just get a message that says something
like "Can't have any actions applied to it".foxxpop,
> But instead of the scene changing to the next scene
> after I press the button, these errors come up!!
>
> 1180: Call to a possibly undefined method on.
> 1120: Access of undefined property release.
> 1120: Access of undefined property _root.
You're using three terms that aren't supported in
ActionScript 3.0: the
on() function, the release parameter to that function, and
_root. In
ActionScript 3.0, you'll have to give your button an instance
name and wire
it up like this:
http://www.quip.net/blog/2007/flash/making-buttons-work-in-flash-cs3
In that blog entry, you'll see (with explanation) something
that looks
like this:
myButton.addEventListener(
MouseEvent.CLICK,
function(evt:MouseEvent):void {
trace("I've been clicked!");
Just bear in mind that you can also make that a named
function, like
this:
myButton.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(evt:MouseEvent):void {
trace("I've been clicked!");
> I've tried asking in so many different forums, I usually
do
> pretty good with action scripting for buttons. But this
is
> my first time doing it with cs3.
Your issue isn't Flash CS3, it's that your FLA file is
configured for
ActionScript 3.0. If you change that to AS2, you can go right
on using the
same approach you used to -- and you'll be able to attach
code directly to
objects. ;)
David Stiller
Co-author, The ActionScript 3.0 Quick Reference Guide
http://tinyurl.com/2s28a5
"Luck is the residue of good design." -
Hi Folks,
I'm stuck with a simple flash movie I need to create in which two images fade in and fade out, and each of them must be clickable and hyperlink to a separate URL.
I've got the fading going okay, but I cannot for the life of me get even a single button to work on it.
If it's helpful I can send along what I've got so far. Any assistance would be greatly appreciated.
Regards,
Andrew.Hi Ned,
Thank you for the followup email.
I think I was close - I was aware of the changes between AS2 and AS3, but I
think I was putting the Action in the wrong place.
Next question - if I have 2 buttons (say btn1 and btn2), how do I make btn1
available for say frames 1-60, and btn2 available from frames 61-120?
I am primarily a ColdFusion developer, and a complete newbie to flash, so
sorry if this is a dumb question.
Regards,
Andrew.
2009/5/15 Ned Murphy <[email protected]>
In AS3, to make a button work with code, you need to add an event listener
for it. In the timeline that holds that button, in a separate actions layer
that you create, in a frame numbered the same as where that button exists,
you would add the event listener:
>
btn1.addEventListener(MouseEvent.CLICK, btn1Click);
>
Descriptively, that line of code contains the following:
>
buttonInstanceName . displayObjectMethod (eventClass . eventType,
eventHandlerFunction);
>
The name of the function for processing the clicking of that button was
already defined at the end of the event listener assignment, so now you just
have to write that function out:
>
function btn1Click(evt:MouseEvent):void {
var /String = "http://www.awebsite.com/awebpage.html";
var req:URLRequest = new URLRequest(url);
navigateToURL(req);
> -
Help with Buttons in Actionscript 2
Help!! I am creating a Flash movie with 5 scenes. I have
buttons for each that I have added actionscript to, but they still
do not work. I am not getting any error messages. I am using
actionscript 2 and have gone step by step. here is what i have :
on (release) {
gotoAndStop("main", 1);
on (release) {
gotoAndStop("blackboard", 1);
etc for the rest of the buttons. Any help would save me much
stress as I am trying to finish a project for school!!! thanks
ahTry adding _root. before the gotoAndStop command. If you have
the code on the buttons that is. -
Help with Buttons on MIDlet application
Hi,
I'm writing a game on MIDP using Netbeans' cellular simulator.
How can I make buttons on the screen? those buttons should have the ability for the user click on them & then
the screen would be change (using by ActionListener).I know that on GUI I should write it as many JButtons in the place
where the game board should be at, but what should be written when using MIDP and how to connect to it a "Listener"?Hi,
the philosophy used for application design and development on mobile devices differes from desktop design and development. And using the Java ME has some specifics and limitations. You have basicly 3 options to develop your applications:
1) use high level API screens - List/Form - in this case the application will look differently on every device and usually not too good and you can't control formatting of the output, but the development is very fast and easy
2) use existing graphical libraries - like LWUIT - good choice for many applications (but games), gives you rich API (in relation to the native List/Form) with good results - it's only slightly more complicated and the result is significantly better
3) build your own UI on Canvas - you can display whatever you want... but you must fully implement it
Every next option needs more effort but gives you more freedom in application design and development.
I know that I didin't answer exactly what you asked for, but I'm sure that this answer makes more sense...
Rada -
Help with buttons/actionscript
I've got a movieclip acting as a button. Code on the button itself handles roll-over/-out states, and I define an onRelease function in the main timeline as follows:
backBut.onRelease=function(){
gotoAndPlay(1);
This code takes the movie back to the first frame and works on the third frame. For some reason, though, the same code on the fifth frame has no effect. The button doesn't do anything. I have tried using the same button, I tried inserting a new keyframe with a new button, renaming the button, etc. and for some reason I just can't get it to work on the fifth frame.
If it helps to visualize, I am essentially making a menu. The first frame contains buttons to take you to submenus, located on following slides. This button to go back to the original menu is on each frame with a submenu, but for some reason only works on one of them. Thanks in advance for your help.Try moving that button in frame 5 to it's own layer. If it's the same llibrary symbol, what happens is the name from the preceding instance gets inherited, along with other characteristics. If frame 4 doesn't do anything, instead of a new layer, just try putting a blank keyframe between the buttons in frames 3 and 5. This may not be the problem at all, but it's my first guess at the possible problem.
Maybe you are looking for
-
Can you help me to retrieve my applications. In iTunes I accidentally clicked the Restore button in the Summary screen. It restored the original settings and I lost all my applications and don't know if it is possible to retrieve them.
-
Itunes will not install. PLEASE HELP!
i've tryed to download itunes 10 and quicktime. but once i've pressed the blue 'download now' button the next page appears with a red box that stretches to the bottom of the page and says "This frame was blocked because it contains some insecure cont
-
ABC Analysis report -Inventory
HI Has anyone created this kind of report in B1 . I came to know that this report is kind of standard report in most ERP. Can you please let me know how can i created that kind of report .I need to create that report as soon as possible so i need s
-
My Nikon D810 will not tether with lightroom. I've tried three different USB 3.0 Cords, Two different computers and reinstalling lightroom and have made sure its up to date 5.6 Help?
-
IMessage sending as email why?
Hi there does anyone know why my iMessage is now sending as an email? I've been in my message settings and the numbers there it says it only has 1 address anyone know how I can get my number in there? Thanks