A lot of buttons

Discussion in 'Java' started by Bart, Oct 3, 2003.

  1. Bart

    Bart Guest

    Hi,

    I'm trying to learn java programming for school. Now I have to make a
    calculator. But I have to make a lot of buttons for it. Is there a way to
    make a methode for makeing a button that I only have to type (for example)
    makeButton(one);.
    And that the button will be created.

    Already thanks, Bart
     
    Bart, Oct 3, 2003
    #1
    1. Advertisements

  2. Bart

    Skippy Guest

    I'm trying to learn java programming for school. Now I have to make a
    public Button createButton(String label)
    {
    Button b = new Button();

    b.setLabel(label);
    b.foo();
    b.bar();
    b.addActionListener(...);

    return b;
    }
     
    Skippy, Oct 3, 2003
    #2
    1. Advertisements

  3. Bart

    Bart Guest

    Thanks for you reaction.

    But I do not realy understand the answer. Could you please explain your
    code?

    Thanks, Bart
     
    Bart, Oct 3, 2003
    #3
  4. Bart

    Roedy Green Guest

    If you want a pretty button, you need to get something like Photoshop
    to create gifs, ideally one for pressed, unpressed, selected etc etc.

    If you just want method to create a simple text button try something
    like this untested code:

    static public JButton makeButton( String label )
    {
    Jbutton b = new JButton( label );
    b.setForeground( Color.white );
    b.setBackground( new Color ( 0x228b22 ) );
    b.setFont( new Font( "Dialog", Font.BOLD, 12 ));
    return b;
    }
     
    Roedy Green, Oct 3, 2003
    #4
  5. Bart

    Andrew Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1



    Bart wrote:
    | Thanks for you reaction.
    |
    | But I do not realy understand the answer. Could you please explain your
    | code?
    |
    | Thanks, Bart
    |
    | |
    |>>I'm trying to learn java programming for school. Now I have to make a
    |>>calculator. But I have to make a lot of buttons for it. Is there a way
    |
    | to
    |
    |>>make a methode for makeing a button that I only have to type (for
    |
    | example)
    |
    |>>makeButton(one);.
    |>>And that the button will be created.
    |>
    |>public Button createButton(String label)
    |>{
    |> Button b = new Button();
    |>
    |> b.setLabel(label);
    |> b.foo();
    |> b.bar();
    |> b.addActionListener(...);
    |>
    |> return b;
    |>}
    |>
    |>
    |
    |
    |

    This method call simply creates a button with the label you pass in as
    the parameter, and an actionlistener (which you will have to code and is
    not shown here) and returns a reference to that button so you can put it
    on your form.

    - --
    Andrew.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.3 (GNU/Linux)

    iD8DBQE/fdwdx4nopx2ZsVURApKVAJ9NhvOi9GhjvHCRyDB3fxu8LvHwCgCffpoP
    AQDrtHwuTEocHYwRQzfB8pI=
    =OJwK
    -----END PGP SIGNATURE-----
     
    Andrew, Oct 3, 2003
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.