0 Members and 1 Guest are viewing this topic.
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); }}
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 */
--------------------Configuration: <Default>--------------------E:\adv. comp sci\kgtolb.java:21: cannot find symbolsymbol : variable buttonPanellocation: class kgtolb buttonPanel.add(kgButton); ^E:\adv. comp sci\kgtolb.java:22: cannot find symbolsymbol : variable buttonPanellocation: class kgtolb buttonPanel.add(lbButton); ^E:\adv. comp sci\kgtolb.java:24: cannot find symbolsymbol : variable borderLayoutlocation: 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 errorsProcess completed.
JPanel buttonPanel = new JPanel();
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); }}
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 */
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 */
w.setkg(lbinput);double lb = w.getlb();