Jump to content

JButton Help

- - - - -

  • Please log in to reply
7 replies to this topic

#1
dkwb

dkwb

    Newbie

  • Members
  • Pip
  • 5 posts
Hi, I'm new to java programming, need some help regarding Jbuttons
Below is a picture of my GUI:

Attached File  Untitled.jpg   70.99K   75 downloads

how can I join the 3 buttons I circled?

here's my code btw:

import javax.swing.*;


import java.awt.*;

import java.awt.event.*;


  public class FoodStallApps extends JFrame implements ActionListener{


    private JTextField jtext = new JTextField("");

    private JButton b1 = new JButton("");

    private JButton b2 = new JButton("");

    private JButton b3 = new JButton("1");

    private JButton b4 = new JButton("2");

    private JButton b5 = new JButton("");

    private JButton b6 = new JButton("");

    private JButton b7 = new JButton("");

    private JButton b8 = new JButton("");

    private JButton b9 = new JButton("");

    private JButton b10 = new JButton("");

    private JButton b11 = new JButton("");

    private JButton b12 = new JButton("");

    private JButton b13 = new JButton("");

    private JButton b14 = new JButton("");

    private JButton b15 = new JButton("");

    private JButton b16 = new JButton("3");

    private JButton b17 = new JButton("");

    private JButton b18 = new JButton("");

    private JButton b19 = new JButton("");

    private JButton b20 = new JButton("");

    private JButton b21 = new JButton("");

    private JButton b22 = new JButton("");

    private JButton b23 = new JButton("");

    private JButton b24 = new JButton("");

    private JButton b25 = new JButton("");

    private JButton b26 = new JButton("");

    private JButton b27 = new JButton("");

    private JButton b28 = new JButton("");

    private JButton b29 = new JButton("");

    private JButton b30 = new JButton("");

    private JButton b31 = new JButton("4");

    private JButton b32 = new JButton("");

    private JButton b33 = new JButton("");

    private JButton b34 = new JButton("");

    private JButton b35 = new JButton("");

    private JButton b36 = new JButton("5");

    private JButton b37 = new JButton("5");

    private JButton b38 = new JButton("5");

    private JButton b39 = new JButton("");

    private JButton b40 = new JButton("");

    private JButton b41 = new JButton("");

    private JButton b42 = new JButton("");

    private JButton b43 = new JButton("");

    private JButton b44 = new JButton("");

    private JButton b45 = new JButton("");

    private JButton b46 = new JButton("6");

    private JButton b47 = new JButton("");

    private JButton b48 = new JButton("");

    private JButton b49 = new JButton("");

    private JButton b50 = new JButton("");

    private JButton b51 = new JButton("7");

    private JButton b52 = new JButton("7");

    private JButton b53 = new JButton("7");

    private JButton b54 = new JButton("");

    private JButton b55 = new JButton("");

    private JButton b56 = new JButton("8");

    private JButton b57 = new JButton("");

    private JButton b58 = new JButton("");

    private JButton b59 = new JButton("");

    private JButton b60 = new JButton("");

    private JButton b61 = new JButton("9");

    private JButton b62 = new JButton("");

    private JButton b63 = new JButton("");

    private JButton b64 = new JButton("");

    private JButton b65 = new JButton("");

    private JButton b66 = new JButton("");

    private JButton b67 = new JButton("");

    private JButton b68 = new JButton("");

    private JButton b69 = new JButton("");

    private JButton b70 = new JButton("");

    private JButton b71 = new JButton("");

    private JButton b72 = new JButton("");

    private JButton b73 = new JButton("");

    private JButton b74 = new JButton("");

    private JButton b75 = new JButton("");

    private JButton b76 = new JButton("");

    private JButton b77 = new JButton("10");

    private JButton b78 = new JButton("11");

    private JButton b79 = new JButton("12");

    private JButton b80 = new JButton("13");

    private JButton b81 = new JButton("");

    private JButton b82 = new JButton("");

    private JButton b83 = new JButton("");

    private JButton b84 = new JButton("");

    private JButton b85 = new JButton("14");

    private JButton b86 = new JButton("14");

    private JButton b87 = new JButton("15");

    private JButton b88 = new JButton("16");

    private JButton b89 = new JButton("");

    private JButton b90 = new JButton("17");

    private JButton b91 = new JButton("");

    private JButton b92 = new JButton("");

    private JButton b93 = new JButton("");

    private JButton b94 = new JButton("");

    private JButton b95 = new JButton("");

    private JButton b96 = new JButton("E");

    private JButton b97 = new JButton("X");

    private JButton b98 = new JButton("I");

    private JButton b99 = new JButton("T");

    private JButton b100 = new JButton("");

    private JButton b101 = new JButton("");

    private JButton b102 = new JButton("");

    private JButton b103 = new JButton("");

    private JButton b104 = new JButton("");

    private JButton b105 = new JButton("");

    

    public FoodStallApps(){


        String output = "Welcome to the foodcourt! Please select the stalls you are interested! ";

        JOptionPane.showMessageDialog(null, output, 

          "", JOptionPane.INFORMATION_MESSAGE);

        

        /** creating buttons pad **/

        JPanel p1 = new JPanel();

        p1.setLayout(new GridLayout(7,15,1,1));


        p1.add(b1);

        p1.add(b2);

        p1.add(b3);

        p1.add(b4);

        p1.add(b5);

        p1.add(b6);

        p1.add(b7);

        p1.add(b8);

        p1.add(b9);

        p1.add(b10);

        p1.add(b11);

        p1.add(b12);

        p1.add(b13);

        p1.add(b14);

        p1.add(b15);

        p1.add(b16);

        p1.add(b17);

        p1.add(b18);

        p1.add(b19);

        p1.add(b20);

        p1.add(b21);

        p1.add(b22);

        p1.add(b23);

        p1.add(b24);

        p1.add(b25);

        p1.add(b26);

        p1.add(b27);

        p1.add(b28);

        p1.add(b29);

        p1.add(b30);

        p1.add(b31);

        p1.add(b32);

        p1.add(b33);

        p1.add(b34);

        p1.add(b35);

        p1.add(b36);

        p1.add(b37);

        p1.add(b38);

        p1.add(b39);

        p1.add(b40);

        p1.add(b41);

        p1.add(b42);

        p1.add(b43);

        p1.add(b44);

        p1.add(b45);

        p1.add(b46);

        p1.add(b47);

        p1.add(b48);

        p1.add(b49);

        p1.add(b50);

        p1.add(b51);

        p1.add(b52);

        p1.add(b53);

        p1.add(b54);

        p1.add(b55);

        p1.add(b56);

        p1.add(b57);

        p1.add(b58);

        p1.add(b59);

        p1.add(b60);

        p1.add(b61);

        p1.add(b62);

        p1.add(b63);

        p1.add(b64);

        p1.add(b65);

        p1.add(b66);

        p1.add(b67);

        p1.add(b68);

        p1.add(b69);

        p1.add(b70);

        p1.add(b71);

        p1.add(b72);

        p1.add(b73);

        p1.add(b74);

        p1.add(b75);

        p1.add(b76);

        p1.add(b77);

        p1.add(b78);

        p1.add(b79);

        p1.add(b80);

        p1.add(b81);

        p1.add(b82);

        p1.add(b83);

        p1.add(b84);

        p1.add(b85);

        p1.add(b86);

        p1.add(b87);

        p1.add(b88);

        p1.add(b89);

        p1.add(b90);

        p1.add(b91);

        p1.add(b92);

        p1.add(b93);

        p1.add(b94);

        p1.add(b95);

        p1.add(b96);

        p1.add(b97);

        p1.add(b98);

        p1.add(b99);

        p1.add(b100);

        p1.add(b101);

        p1.add(b102);

        p1.add(b103);

        p1.add(b104);

        p1.add(b105);

        

        b1.addActionListener(this);

        b2.addActionListener(this);

        b3.addActionListener(this);

        b4.addActionListener(this);

        b5.addActionListener(this);

        b6.addActionListener(this);

        b7.addActionListener(this);

        b8.addActionListener(this);

        b9.addActionListener(this);

        b10.addActionListener(this);

        b11.addActionListener(this);

        b12.addActionListener(this);

        b13.addActionListener(this);

        b14.addActionListener(this);

        b15.addActionListener(this);

        b16.addActionListener(this);

        b17.addActionListener(this);

        b18.addActionListener(this);

        b19.addActionListener(this);

        b20.addActionListener(this);

        b21.addActionListener(this);

        b22.addActionListener(this);

        b23.addActionListener(this);

        b24.addActionListener(this);

        b25.addActionListener(this);

        b26.addActionListener(this);

        b27.addActionListener(this);

        b28.addActionListener(this);

        b29.addActionListener(this);

        b30.addActionListener(this);

        b31.addActionListener(this);

        b32.addActionListener(this);

        b33.addActionListener(this);

        b34.addActionListener(this);

        b35.addActionListener(this);

        b36.addActionListener(this);

        b37.addActionListener(this);

        b38.addActionListener(this);

        b39.addActionListener(this);

        b40.addActionListener(this);

        b41.addActionListener(this);

        b42.addActionListener(this);

        b43.addActionListener(this);

        b44.addActionListener(this);

        b45.addActionListener(this);

        b46.addActionListener(this);

        b47.addActionListener(this);

        b48.addActionListener(this);

        b49.addActionListener(this);

        b50.addActionListener(this);

        b51.addActionListener(this);

        b52.addActionListener(this);

        b53.addActionListener(this);

        b54.addActionListener(this);

        b55.addActionListener(this);

        b56.addActionListener(this);

        b57.addActionListener(this);

        b58.addActionListener(this);

        b59.addActionListener(this);

        b60.addActionListener(this);

        b61.addActionListener(this);

        b62.addActionListener(this);

        b63.addActionListener(this);

        b64.addActionListener(this);

        b65.addActionListener(this);

        b66.addActionListener(this);

        b67.addActionListener(this);

        b68.addActionListener(this);

        b69.addActionListener(this);

        b70.addActionListener(this);

        b71.addActionListener(this);

        b72.addActionListener(this);

        b73.addActionListener(this);

        b74.addActionListener(this);

        b75.addActionListener(this);

        b76.addActionListener(this);

        b77.addActionListener(this);

        b78.addActionListener(this);

        b79.addActionListener(this);

        b80.addActionListener(this);

        b81.addActionListener(this);

        b82.addActionListener(this);

        b83.addActionListener(this);

        b84.addActionListener(this);

        b85.addActionListener(this);

        b86.addActionListener(this);

        b87.addActionListener(this);

        b88.addActionListener(this);

        b89.addActionListener(this);

        b90.addActionListener(this);

        b91.addActionListener(this);

        b92.addActionListener(this);

        b93.addActionListener(this);

        b94.addActionListener(this);

        b95.addActionListener(this);

        b96.addActionListener(this);

        b97.addActionListener(this);

        b98.addActionListener(this);

        b99.addActionListener(this);

        b100.addActionListener(this);

        b101.addActionListener(this);

        b102.addActionListener(this);

        b103.addActionListener(this);

        b104.addActionListener(this);

        b105.addActionListener(this);

        

        

        

        

        /*container.add(jtext, BorderLayout.NORTH);

        container.add(p1, BorderLayout.CENTER);

        container.add(p2, BorderLayout.SOUTH);

       *///container object is not needed for new version of JDK

        

        add(jtext, BorderLayout.NORTH);

        add(p1, BorderLayout.CENTER);

     }

    

    public void actionPerformed(ActionEvent e)

    {

    // find out which button was pressed

      String check = jtext.getText();

      if (check.equals("")){

    	  check = ("");

         jtext.setText("");

      }					//this code is for removing first JTextField upon number input

      

      if (e.getSource()==b3)

      {

         jtext.setText("");

         String output = "Stall One sells mixed rice! ";

         JOptionPane.showMessageDialog(null, output, 

           "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b4)

      {

    	  jtext.setText("");

          String output = "Stall Two sells chicken rice! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b16)

      {

    	  jtext.setText("");

          String output = "Stall Three sells Noodles! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b31)

      {

    	  jtext.setText("");

          String output = "Stall Four sells Malay chicken rice! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b36)

      {

    	  jtext.setText("");

          String output = "Stall Five sells fruit juice! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b37)

      {

    	  jtext.setText("");

          String output = "Stall Five sells fruit juice! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b38)

      {

    	  jtext.setText("");

          String output = "Stall Five sells fruit juice! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b46)

      {

    	  jtext.setText("");

          String output = "Stall Six sells Japanese food! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b51)

      {

    	  jtext.setText("");

          String output = "Stall Seven sells drinks! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b52)

      {

    	  jtext.setText("");

          String output = "Stall Seven sells drinks! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b53)

      {

    	  jtext.setText("");

          String output = "Stall Seven sells drinks! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b56)

      {

    	  jtext.setText("");

          String output = "Stall Eight sells Korean dishes! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b61)

      {

    	  jtext.setText("");

          String output = "Stall Nine is currently empty! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b77)

      {

    	  jtext.setText("");

          String output = "Stall Ten sells Malay dishes! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b78)

      {

    	  jtext.setText("");

          String output = "Stall Eleven sells Malay dishes! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b79)

      {

    	  jtext.setText("");

          String output = "Stall Twelve sells Indian dishes! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b80)

      {

    	  jtext.setText("");

          String output = "Stall Thirteen sells Western food! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b85)

      {

    	  jtext.setText("");

          String output = "Stall Fourteen sells Fast Food! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b86)

      {

    	  jtext.setText("");

          String output = "Stall Fourteen sells Fast Food! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b87)

      {

    	  jtext.setText("");

          String output = "Stall Fifteen sells Kebabs! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b88)

      {

    	  jtext.setText("");

          String output = "Stall Sixteen sells Hotplates! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b90)

      {

    	  jtext.setText("");

          String output = "Stall Seventeen sells Hotdogs! ";

          JOptionPane.showMessageDialog(null, output, 

            "", JOptionPane.INFORMATION_MESSAGE);

      }

      else if (e.getSource() == b96)

      {

          JOptionPane.showMessageDialog(null,"Thank you using our application, we hope you manage to find what you want to eat!",

                  "FoodStallApps",JOptionPane.INFORMATION_MESSAGE);

                  System.exit(0);

      }

      else if (e.getSource() == b97)

      {

          JOptionPane.showMessageDialog(null,"Thank you using our application, we hope you manage to find what you want to eat!",

                  "FoodStallApps",JOptionPane.INFORMATION_MESSAGE);

                  System.exit(0);

      }

      else if (e.getSource() == b98)

      {

          JOptionPane.showMessageDialog(null,"Thank you using our application, we hope you manage to find what you want to eat!",

                  "FoodStallApps",JOptionPane.INFORMATION_MESSAGE);

                  System.exit(0);

      }

      else if (e.getSource() == b99)

      {

          JOptionPane.showMessageDialog(null,"Thank you using our application, we hope you manage to find what you want to eat!",

                  "FoodStallApps",JOptionPane.INFORMATION_MESSAGE);

                  System.exit(0);

      }

   }


    public static void main(String[] args) {

        FoodStallApps frame = new FoodStallApps();

        frame.setTitle("FoodStallApps");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//It'll close even though this statement is commented off

        frame.setSize(1000,600);

        frame.setVisible(true);

    }

}

Edited by Roger, 05 August 2011 - 08:02 AM.


#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts

Quote

how can I join the 3 buttons I circled?
Can you define the "join" part? No idea what you meant.

#3
dkwb

dkwb

    Newbie

  • Members
  • Pip
  • 5 posts

Sinipull said:

Can you define the "join" part? No idea what you meant.

Sorry, as in merging the 3 cells together into one?

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I assume you want 1 button with "5" on with the width of 3 buttons there.

You gonna need a gridbagLayout for that.
It's the hardest layout to use, but the most flexible as well.
How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

#5
dkwb

dkwb

    Newbie

  • Members
  • Pip
  • 5 posts

Sinipull said:

Can you define the "join" part? No idea what you meant.

sorry, as in having a button with the width of 3

#6
dkwb

dkwb

    Newbie

  • Members
  • Pip
  • 5 posts
ye... i'll read up on the link, thx!

#7
dkwb

dkwb

    Newbie

  • Members
  • Pip
  • 5 posts
hi again,

am having troubles with the gridbaglayout...
how can i show a message dialogue when i press one of the buttons created through the layout?

skeleton code:

import java.awt.*;

import javax.swing.JButton;

import javax.swing.JFrame;


public class GridBagLayoutDemo {

    final static boolean shouldFill = true;

    final static boolean shouldWeightX = true;

    final static boolean RIGHT_TO_LEFT = false;


    public static void addComponentsToPane(Container pane) {

        if (RIGHT_TO_LEFT) {

            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

        }


        JButton button;

	pane.setLayout(new GridBagLayout());

	GridBagConstraints c = new GridBagConstraints();

	if (shouldFill) {

	//natural height, maximum width

	c.fill = GridBagConstraints.HORIZONTAL;

	}


	button = new JButton("Button 1");

	if (shouldWeightX) {

	c.weightx = 0.5;

	}

	c.fill = GridBagConstraints.HORIZONTAL;

	c.gridx = 0;

	c.gridy = 0;

	pane.add(button, c);


	button = new JButton("Button 2");

	c.fill = GridBagConstraints.HORIZONTAL;

	c.weightx = 0.5;

	c.gridx = 1;

	c.gridy = 0;

	pane.add(button, c);


	button = new JButton("Button 3");

	c.fill = GridBagConstraints.HORIZONTAL;

	c.weightx = 0.5;

	c.gridx = 2;

	c.gridy = 0;

	pane.add(button, c);


	button = new JButton("Long-Named Button 4");

	c.fill = GridBagConstraints.HORIZONTAL;

	c.ipady = 40;      //make this component tall

	c.weightx = 0.0;

	c.gridwidth = 3;

	c.gridx = 0;

	c.gridy = 1;

	pane.add(button, c);


	button = new JButton("5");

	c.fill = GridBagConstraints.HORIZONTAL;

	c.ipady = 0;       //reset to default

	c.weighty = 1.0;   //request any extra vertical space

	c.anchor = GridBagConstraints.PAGE_END; //bottom of space

	c.insets = new Insets(10,0,0,0);  //top padding

	c.gridx = 1;       //aligned with button 2

	c.gridwidth = 2;   //2 columns wide

	c.gridy = 2;       //third row

	pane.add(button, c);

    }


    /**

     * Create the GUI and show it.  For thread safety,

     * this method should be invoked from the

     * event-dispatching thread.

     */

    private static void createAndShowGUI() {

        //Create and set up the window.

        JFrame frame = new JFrame("GridBagLayoutDemo");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //Set up the content pane.

        addComponentsToPane(frame.getContentPane());


        //Display the window.

        frame.pack();

        frame.setVisible(true);

    }


    public static void main(String[] args) {

        //Schedule a job for the event-dispatching thread:

        //creating and showing this application's GUI.

        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                createAndShowGUI();

            }

        });

    }

}

Edited by Roger, 05 August 2011 - 08:03 AM.


#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You need an ActionListener:
        ActionListener buttonListener = new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showConfirmDialog(null, "Button \"" + ((JButton) e.getSource()).getText() + "\" clicked.");
            }
        };
And add it to every button:
button.addActionListener(buttonListener);





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users