Author Topic: [java GUI] cannot find buttonPanel? :S  (Read 7108 times)

0 Members and 1 Guest are viewing this topic.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
[java GUI] cannot find buttonPanel? :S
« on: April 28, 2011, 07:45:09 pm »
I'm pretty sure buttonPanel and BorderLayout are part of the swing package.. yet it cant be found?  ???

code for kgtolb.java:

Code: [Select]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class kgtolb extends JFrame{
private converter w = new converter();
private JLabel kgLabel = new JLabel("Kilograms");
private JLabel lbLabel = new JLabel("Pounds");
private JTextField kgField = new JTextField("0.0");
private JTextField lbField = new JTextField("0.0");
private JButton kgButton = new JButton("Convert >>>>>");
private JButton lbButton = new JButton("Convert <<<<<");

public kgtolb() {
JPanel dataPanel = new JPanel(new GridLayout(2,2,12,6));
dataPanel.add(kgLabel);
dataPanel.add(lbLabel);
dataPanel.add(kgField);
dataPanel.add(lbField);
JPanel buttonPane1 = new JPanel();
buttonPanel.add(kgButton);
buttonPanel.add(lbButton);
Container container = getContentPane();
container.add(dataPanel, borderLayout.CENTER);
kgButton.addActionListener(new kgListener());
lbButton.addActionListener(new lbListener());
}
private class kgListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String input = kgField.getText();
w.setkg(Double.parseDouble(input));
double kg = w.getkg();
kgField.setText(" ",kg);
}
}
private class lbListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String input = lbField.getText();
w.setlb(Double.parseDouble(input));
double lb = w.getlb();
lbField.setText(" ",lb);
}
}
public static void main(String[]args){
kgtolb GUI = new kgtolb();
GUI.setTitle("Pounds to Kilograms converter");
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.pack();
GUI.setVisible(true);
}
}

here is the converter.java file:

Code: [Select]
public class converter {

private double weightLB;
private double weightKG;

public converter() {
}
public void setkg(double lb)
{
double weightKG = (double)lb/2.2;
}
public void setlb(double kg)
{
double weightLB = (double)kg*2.2;
}
public double getkg(){
return weightKG;
}
public double getlb(){
return weightLB;
}
}

/*lbs / 2.2 = kilograms
 *kg x  2.2 = pounds
 */

here is the build output:

Code: [Select]

--------------------Configuration: <Default>--------------------
E:\adv. comp sci\kgtolb.java:21: cannot find symbol
symbol  : variable buttonPanel
location: class kgtolb
buttonPanel.add(kgButton);
^
E:\adv. comp sci\kgtolb.java:22: cannot find symbol
symbol  : variable buttonPanel
location: class kgtolb
buttonPanel.add(lbButton);
^
E:\adv. comp sci\kgtolb.java:24: cannot find symbol
symbol  : variable borderLayout
location: class kgtolb
container.add(dataPanel, borderLayout.CENTER);
                         ^
E:\adv. comp sci\kgtolb.java:33: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.String,double)
kgField.setText(" ",kg);
       ^
E:\adv. comp sci\kgtolb.java:41: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.String,double)
lbField.setText(" ",lb);
       ^
5 errors

Process completed.

Although I know it may not convert right, im just looking for it to work so I can make it convert properly once I see it work.
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #1 on: April 28, 2011, 07:49:05 pm »
BorderLayout.CENTER
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #2 on: April 28, 2011, 07:49:47 pm »
ok, but what about the buttonPanel one?

edit: ..and the setText one also
« Last Edit: April 28, 2011, 07:51:05 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #3 on: April 28, 2011, 07:54:14 pm »
Well, the setText() one is that setText() only accepts one argument, and it has to be a String. I think you want that to say setText(" "+kg) and setText(" "+lb)

As for the buttonPanel one, I'm not sure yet...it looks like it should work just fine, but for some reason it's not.
« Last Edit: April 28, 2011, 07:54:38 pm by ZippyDee »
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #4 on: April 28, 2011, 07:57:07 pm »
hmm it says setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.String,double)
(minus the bold)
so that means it accepts 2 arguments. Also it worked on the code my teacher gave me when it was used in that format :s

edit: man this is the 2nd time recently ive been getting an overly weird error.. the last time my teacher actually had to analyze it on her own computer during class! :o
« Last Edit: April 28, 2011, 07:59:28 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #5 on: April 28, 2011, 07:58:55 pm »
No, http://download.oracle.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setText(java.lang.String)

That error means that you are supplying it with java.lang.String,double and that those arguments are not valid. It doesn't mean that it accepts two arguments.


If you read the error message it says setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.String,double)
« Last Edit: April 28, 2011, 08:00:35 pm by ZippyDee »
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #6 on: April 28, 2011, 08:02:31 pm »
ohhhhhhh your right, it does have the + on my paper. I'll edit this to let you know if that worked

edit: that did it  ;D

but..  ??? that buttonPanel..  :-X

edit: what class is buttonPanel found in?
« Last Edit: April 28, 2011, 08:05:16 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #7 on: April 28, 2011, 08:06:59 pm »
buttonPanel isn't a class. You defined it in your code

Code: [Select]
JPanel buttonPanel = new JPanel();

It's an instance of the JPanel class.
« Last Edit: April 28, 2011, 08:07:15 pm by ZippyDee »
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #8 on: April 28, 2011, 08:09:07 pm »
OOOOOOOOhhhhhhh! Now I got it. Heh.. turns out I put JPanel buttonPane1 instead of buttonPanel. :P now it works. Thanks!
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #9 on: April 28, 2011, 08:10:12 pm »
Ah, well that would certainly do it! Damn this hard-to-distinguish font! Well, I'm glad it got figured out!
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #10 on: April 28, 2011, 08:43:46 pm »
Ok this time it just won't convert period. I tried 5 in the kilograms field to convert to pounds but it didn't work and just put '0.0' in the pounds text field :(

here is the updated code and updated converter.java file

Code: [Select]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class kgtolb extends JFrame{
private converter w = new converter();
private JLabel kgLabel = new JLabel("Kilograms");
private JLabel lbLabel = new JLabel("Pounds");
private JTextField kgField = new JTextField("");
private JTextField lbField = new JTextField("");
private JButton kgButton = new JButton("Convert >>>>>");
private JButton lbButton = new JButton("Convert <<<<<");

public kgtolb() {
JPanel dataPanel = new JPanel(new GridLayout(2,2,12,6));
dataPanel.add(kgLabel);
dataPanel.add(lbLabel);
dataPanel.add(kgField);
dataPanel.add(lbField);
JPanel buttonPanel = new JPanel();
buttonPanel.add(kgButton);
buttonPanel.add(lbButton);
Container container = getContentPane();
container.add(dataPanel, BorderLayout.CENTER);
container.add(buttonPanel, BorderLayout.SOUTH);
kgButton.addActionListener(new kgListener());
lbButton.addActionListener(new lbListener());
}
private class kgListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String input = kgField.getText();
double kginput = Double.parseDouble(input);
w.setlb(kginput);
double lb = w.getlb();
lbField.setText(""+ lb);
}
}
private class lbListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String input = lbField.getText();
double lbinput = Double.parseDouble(input);
w.setkg(lbinput);
double lb = w.getlb();
kgField.setText(""+ lb);
}
}
public static void main(String[]args){
kgtolb GUI = new kgtolb();
GUI.setTitle("Pounds to Kilograms converter");
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.pack();
GUI.setVisible(true);
}
}

converter.java
Code: [Select]
public class converter {

private double weightLB;
private double weightKG;

public converter() {
}
public void setkg(double kg)
{
double weightKG = (double)kg/2.2;
}
public void setlb(double lb)
{
double weightLB = (double)lb*2.2;
}
public double getlb(){
return weightLB;
}
public double getkg(){
return weightKG;
}
}

/*lbs / 2.2 = kilograms
 *kg x  2.2 = pounds
 */
« Last Edit: April 28, 2011, 08:44:18 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #11 on: April 28, 2011, 08:45:31 pm »
Code: [Select]
public class converter {

private double weightLB;
private double weightKG;

public converter() {
}
public void setkg(double kg)
{
weightKG = (double)kg/2.2;
}
public void setlb(double lb)
{
weightLB = (double)lb*2.2;
}
public double getlb(){
return weightLB;
}
public double getkg(){
return weightKG;
}
}

/*lbs / 2.2 = kilograms
 *kg x  2.2 = pounds
 */

Try that
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #12 on: April 28, 2011, 08:54:11 pm »
:s it still outputs '0.0' when you enter a number in the lb input field and convert it to kg
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #13 on: April 28, 2011, 08:56:50 pm »
Code: [Select]
w.setkg(lbinput);
double lb = w.getlb();
Should that be w.getkg()?
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java GUI] cannot find buttonPanel? :S
« Reply #14 on: April 28, 2011, 08:59:14 pm »
yeah, I suppose so. Thanks for helping me! :D

..now to work on my moon phases assignment :-X
Loved this place, still the best producers of power metal, and sparked my dreams of coding.