A lot of buttons

B

Bart

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
 
S

Skippy

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;
}
 
B

Bart

Thanks for you reaction.

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

Thanks, Bart
 
R

Roedy Green

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.

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;
}
 
A

Andrew

-----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-----
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top