• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trying to get this button to work

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I'm trying to get this programme to run smoothly without errors;


package untitled3;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;

public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();

/**Construct the frame*/
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
jButton1.setFont(new java.awt.Font("Serif", 2, 22));
jButton1.setForeground(new Color(255, 92, 0));
jButton1.setText("Press Me...");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("First Frame");
jLabel1.setFont(new java.awt.Font("Dialog", 3, 18));
jLabel1.setForeground(Color.red);
jButton2.setText("Again");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton3.setText("jButton3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
contentPane.add(jButton1, new XYConstraints(79, 74, 222, 125));
contentPane.add(jLabel1, new XYConstraints(100, 218, 184, 50));
contentPane.add(jTextField1, new XYConstraints(146, 27, 93, 37));
contentPane.add(jButton2, new XYConstraints(32, 21, -1, 44));
contentPane.add(jButton3, new XYConstraints(294, 25, 76, 42));
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void jButton1_actionPerformed(ActionEvent e) {
jLabel1.setText(jTextField1.getText());
}

void jButton2_actionPerformed(ActionEvent e) {
jTextField1.setText("");
jLabel1.setText("");
jTextField1.requestFocus();
}

void jButton3_actionPerformed(ActionEvent e) {
System.exit(0);
Double.parsedouble();
jLabel1.setText((Double.parseDouble(jTextField1.getText())*0.166));
}
}


I'm having trouble getting the last few lines to work ,i.e;
These Lines: Double.parsedouble();
jLabel1.setText((Double.parseDouble(jTextField1.getText())*0.166));
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael,
In your Code Double.parsedouble();



Should be like Double.parseDouble(String s)
This method returns a new double initialized to the value represented by the specified String.
 
Mike Meakin
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I'll give it a try!

Well I tried that and it did't work, Thanks anyway...
[ February 24, 2005: Message edited by: Michael Munro ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you provide some more details? What do you mean "it didn't work"? Did it compile? If not, copy-and-paste the compiler errors here and we can explain what it means. If it compiled, did it run? Did you get any runtime errors or exceptions? If so, post the stack trace here. If you got output, how did it differ from what you expected?

The more details you can provide the more likely we will be able to help fix the problem. It is difficult to even start guessing how to help you with a statement as vague as "it didn't work."

Layne
 
Mike Meakin
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here the errors:
"Frame1.java": Error #: 300 : method parseDouble() not found in class java.lang.Double at line 82, column 8
"Frame1.java": Error #: 300 : method setText(double) not found in class javax.swing.JLabel at line 83, column 9

-Its just those 2 lines
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michel,
Can you please read my previous post again.


java.lang.Double has a method called parseDouble(String s) , it takes a String as an argument. Check your code at line 82, where you haven't passed the String argument.

In the Same way javax.swing.JLabel has a method called setText(String text) which even takes a string as an argument. See in your code at line 83 you have passed a double instead of String.


Note : i'm not sure whether the line number mentioned by me are correct, i just got it from the error message you have posted. Also in the beginner stage ( even after becomming a Java pro ) it's advisable to have the java API & solve errors like "method not found" which in turn helps people to get into the Java classes & methods strongly.


[ February 24, 2005: Message edited by: Srinivasa Raghavan ]
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remove the line that Srinivasa pointed out:You are using parseDouble() correctly in the line after it, so remove the commented line. The method requires a String.This means the compiler cannot find a parseDouble method that takes no parameters.Similarly, JLable doesn't have a setText method that takes a primitive double. It does have one that takes a String, and the Double class has a method to convert a double to a String. There are other ways to do it too:If you don't like the format it uses, you can create your own with java.text.NumberFormat. Check the JavaDocs for more information on these classes and methods.
 
Mike Meakin
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I know I've got the code right now, and as silly as it sounds the main class won't work, any ideas what class I should run this as?

package untitled14;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;

public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
XYLayout xYLayout2 = new XYLayout();

/**Construct the frame*/
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}

/**Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");*/
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
jButton1.setFont(new java.awt.Font("Serif", 2, 22));
jButton1.setForeground(new Color(255, 92, 0));
jButton1.setText("Press Me...");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.setLayout(xYLayout2);
this.setSize(new Dimension(400, 300));
this.setTitle("First Frame");
jLabel1.setFont(new java.awt.Font("Dialog", 3, 18));
jLabel1.setForeground(Color.red);
jButton2.setText("Again");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton3.setText("jButton3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
contentPane.add(jLabel1, new XYConstraints(30, 35, 126, 42));
contentPane.add(jButton1, new XYConstraints(180, 38, -1, -1));
contentPane.add(jButton2, new XYConstraints(230, 154, -1, -1));
contentPane.add(jButton3, new XYConstraints(50, 163, -1, -1));
contentPane.add(jTextField1, new XYConstraints(26, 118, 144, -1));
}


/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton1_actionPerformed(ActionEvent e) {
jLabel1.setText(jTextField1.getText());
}

void jButton2_actionPerformed(ActionEvent e) {
jTextField1.setText("");
jLabel1.setText("");
jTextField1.requestFocus();
}

void jButton3_actionPerformed(ActionEvent e) {
System.exit(0);
jLabel1.setText("" + (Double.parseDouble(jTextField1.getText())*0.166));
}
}
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic