 |
| JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 22nd, 2003, 11:57 AM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
JButton
I am trying to add a button to my form so that the smile turns to a frown when the button is pressed. What am I doing wrong and how can I fix it? Here is a copy of my code so far.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Smile2 extends JApplet implements ActionListener
{
//Get content pane
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
//Add button to content pane
JButton jb = new JButton("Press ME");
jb.setActionCommand("Press ME");
jb.addActionListener(this);
contentPane.add(jb);
}
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawRoundRect(199, 48, 82, 82, 82, 82);
g.setColor(Color.yellow);
g.fillRoundRect(200, 50, 80, 80, 80, 80);
g.setColor(Color.black);
g.fillRoundRect(220, 70, 10, 10, 10, 10);
g.setColor(Color.black);
g.fillRoundRect(250, 70, 10, 10, 10, 10);
g.drawArc(215,70,50,40,0,-180);
g.setColor(Color.black);
g.drawRoundRect(20, 48, 82, 82, 82, 82);
g.setColor(Color.yellow);
g.fillRoundRect(21, 50, 80, 80, 80, 80);
g.setColor(Color.black);
g.fillRoundRect(40, 70, 10, 10, 10, 10);
g.setColor(Color.black);
g.fillRoundRect(70, 70, 10, 10, 10, 10);
g.drawArc(35,90,50,40,180,-180);
}
public void actionPerformed(ActionEvent ae)
{
}
}
|
|

August 27th, 2003, 10:56 AM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
is that how you want it to be?
try this.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JApplet;
public class Smile2 extends JApplet implements ActionListener
{
//Get content pane
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
//Add button to content pane
JButton jb = new JButton("Press ME");
jb.setActionCommand("Press ME");
jb.addActionListener(this);
contentPane.add(jb);
}
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawRoundRect(199, 48, 82, 82, 82, 82);
g.setColor(Color.yellow);
g.fillRoundRect(200, 50, 80, 80, 80, 80);
g.setColor(Color.black);
g.fillRoundRect(220, 70, 10, 10, 10, 10);
g.setColor(Color.black);
g.fillRoundRect(250, 70, 10, 10, 10, 10);
g.drawArc(215,70,50,40,0,-180);
g.setColor(Color.black);
g.drawRoundRect(20, 48, 82, 82, 82, 82);
g.setColor(Color.yellow);
g.fillRoundRect(21, 50, 80, 80, 80, 80);
g.setColor(Color.black);
g.fillRoundRect(40, 70, 10, 10, 10, 10);
g.setColor(Color.black);
g.fillRoundRect(70, 70, 10, 10, 10, 10);
g.drawArc(35,90,50,40,180,-180);
}
public void actionPerformed(ActionEvent ae)
{
}
}
|
|

November 7th, 2004, 10:34 PM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hello, I have two question to this code.
The button doesn't show on screen until you actually click on it, how can you make sure that the button stays visible from the start?
When the button is clicked you would like to paint some more, using a method other than paint(Graphics g), how is this done ? Could you give example, thanks.
|
|
 |