Results 1 to 5 of 5
Thread: Action Listener and repaint
- 04-06-2012, 12:28 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Action Listener and repaint
I'm working on this program for weight conversions for school. I've employed the action listener but my repaint method isn't displaying the new information. Also I think it may not be working at all because I changed the color in repaint but it doesn't change. Any help finding the source of this problem would be helpful.
here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.geom.*;
import java.awt.event.*;
public class StringTest extends JFrame implements ActionListener
{
int x = 0;
int y = 150;
double lbs = 0;
String[] weights = new String[4];
String[] unit = {"pounds","ounces","kilograms","metric tons"};
double[] weight = {lbs*1,lbs*16,lbs/2.204623,lbs/2204.623};
Font comic = new Font("Comic Sans", Font.ITALIC, 20);
JLabel label = new JLabel("Enter your weight in pounds");
JTextField field = new JTextField(10);
JButton button = new JButton("Submit");
public StringTest()
{
super("Weight Conversion");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(label);
add(field);
add(button);
field.addActionListener(this);
button.addActionListener(this);
for(x = 0; x<weights.length; ++x)
{
weights[x] = "your weight in "+unit[x]+" is: "+weight[x];
}
}
public void actionPerformed(ActionEvent e)
{
String answer = field.getText();
lbs = Double.valueOf(answer);
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2D = (Graphics2D)g;
g2D.setFont(comic);
g2D.setColor(Color.RED);
y = 150;
for(x = 0; x<weights.length; ++x)
{
g2D.drawString(weights[x], 50, y);
y += 30;
}
}
public void repaint(Graphics gr)
{
Graphics2D gr2D = (Graphics2D)gr;
gr2D.setFont(comic);
gr2D.setColor(Color.BLUE);
for(x=0;x<weights.length; ++x);
{
gr2D.drawString(weights[x], 50, y);
}
}
public static void main(String[] args)
{
StringTest frame = new StringTest();
frame.setSize(400,400);
frame.setVisible(true);
}
}
p.s. I imported the geom package because I intended to do borders around the conversions but getting the conversions working is more important right now.
- 04-06-2012, 01:38 AM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 10
Re: Action Listener and repaint
Ok, I don't think you're supposed to make a custom repaint(), although you can. I got rid of it and got the text to turn from blue to black when the button it pressed. Also, please use tags when posting, it makes it easier to read.
- 04-06-2012, 01:47 AM #3
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 10
Re: Action Listener and repaint
Also, you are trying to update the contents of the weight array by dividing it by the value of lbs. This doesn't work because when the array is created, lbs is equal to zero. You need to find out how to remedy that
- 04-06-2012, 05:56 AM #4
Re: Action Listener and repaint
What are you trying to achieve by creating a repaint(Graphics) overload that's never called?
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-06-2012, 03:48 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Can anyone help me with an action listener?
By mdCollins1 in forum New To JavaReplies: 5Last Post: 03-21-2012, 05:07 AM -
Action-Listener with multiple Arguments? (Button Listener, specifically)
By Kevinw778 in forum AWT / SwingReplies: 2Last Post: 12-11-2011, 11:44 PM -
Help with Combo box and action listener
By sunilmenhdiratta in forum AWT / SwingReplies: 2Last Post: 06-25-2011, 09:32 PM -
Action Listener
By greatmajestics in forum AWT / SwingReplies: 8Last Post: 03-25-2010, 06:39 PM -
Action Listener? how to use this?
By jeffrey in forum New To JavaReplies: 2Last Post: 10-12-2009, 09:51 AM
Bookmarks