Thread: GUI help. Don t understand

1. Member
Join Date
Sep 2010
Posts
42
Rep Power
0

GUI help. Don t understand

I'm trying to make a GUI. I want it to solve dimensional analysis. I have made a program that does this, but with no GUI:
Java Code:
```/*
* @(#)DifAnalysis.java
*
* @https://sites.google.com/site/s0m3b0dysstuff/
* @Ryan Fabela
* @version 1.00 2010/9/24
*/

import java.util.*;
import java.io.*;
import java.lang.Math;
import java.lang.Long;

public class DifAnalysis {

public static void main(String[] Arguments){

Scanner oScan = new Scanner(System.in);

double dSciNoteP1,dSciNoteP2,dTarget,dBase,dBaseminusTarget,dAnswer,dTen = 10;/* P1 Means out of 3.125 x 10^5 the 3.125 part. P2 means the 10^5 part. target is the target exponent, for example when trying to get to kilo- you would put 10^3. base is the metric prefixes exponetial value. answer should be self explanatory.*/

String sMetricPrefixes = "tera-, T, 10^12\ngiga-, G, 10^9\nmega-, M, 10^6\nkilo-, k, 10^3\ndeci-, d, 10^-1\ncenti-, c, 10^-2\nmilli-, m, 10^-3\nmicro-, µ, 10^-6\nnano-, n, 10^-9\npico-, p, 10^-12\nFor base units use 10^0";
String oErrorMes;
System.out.println(sMetricPrefixes);

System.out.println();

System.out.println("Instruction:\nWhen it asks you for the digit term use the first set of numbers, for example in 3.125 x 10^5 you would put 3.125.\nWhen it asks for the exponential term put the last set of numbers, for  exapmle you would put 5.\nWhen it asks for the base exponential value you would put the metric scientific notaion of the metric prefix, for example, when going from micrograms to kilograms you would put -6(refer to above chart)\nWhen it asks for the target exponential value you would use one of the above metric scientific notations, for example when trying to get to kilo- you would put 3.");

System.out.println();

System.out.println("Please put enter the digit term:");
dSciNoteP1 = oScan.nextDouble();

System.out.println("Please enter the exponential term:");
dSciNoteP2 = oScan.nextDouble();

System.out.println("Please enter the base exponential value:");
dBase = oScan.nextDouble();

System.out.println("Please enter the target exponential value:");
dTarget = oScan.nextDouble();

dBaseminusTarget = Math.abs(dBase - dTarget);
dBaseminusTarget = (Math.pow(dTen, dBaseminusTarget));

if(dBase < dTarget){
dAnswer = dSciNoteP1 * dBaseminusTarget;
System.out.println("The answer is: "+dAnswer);
}
if(dBase > dTarget){
dAnswer = dSciNoteP1 / dBaseminusTarget;
System.out.println("The answer is: "+dAnswer);
}

System.out.println("To restart press, close the program and launch again.");
System.out.println("This was made by Ryan Fabela. If you have any problems, send me an email at s0m3b0dystuff@gmail.com.");

}

}```
I have tried to do this but don't understand how to do it. I want to make 3 textfields, one for the first part another for the exponent part and another for the target value.
Ex. 3.125 x 10^5 kilograms to grams. 3.125 is the first part , the 5 is the exponent part, and 0 is the target value.

So far I have this
Java Code:
```/*
* @(#)DifAnalysis.java
*
* @https://sites.google.com/site/s0m3b0dysstuff/
* @Ryan Fabela
* @version 1.00 2010/9/24
*/

import java.util.*;
import java.io.*;
import java.lang.Math;
import java.lang.Long;

public class DifAnalysis {

public static void main(String[] Arguments){

Scanner oScan = new Scanner(System.in);

double dSciNoteP1,dSciNoteP2,dTarget,dBase,dBaseminusTarget,dAnswer,dTen = 10;/* P1 Means out of 3.125 x 10^5 the 3.125 part. P2 means the 10^5 part. target is the target exponent, for example when trying to get to kilo- you would put 10^3. base is the metric prefixes exponetial value. answer should be self explanatory.*/

String sMetricPrefixes = "tera-, T, 10^12\ngiga-, G, 10^9\nmega-, M, 10^6\nkilo-, k, 10^3\ndeci-, d, 10^-1\ncenti-, c, 10^-2\nmilli-, m, 10^-3\nmicro-, µ, 10^-6\nnano-, n, 10^-9\npico-, p, 10^-12\nFor base units use 10^0";
String oErrorMes;
System.out.println(sMetricPrefixes);

System.out.println();

System.out.println("Instruction:\nWhen it asks you for the digit term use the first set of numbers, for example in 3.125 x 10^5 you would put 3.125.\nWhen it asks for the exponential term put the last set of numbers, for  exapmle you would put 5.\nWhen it asks for the base exponential value you would put the metric scientific notaion of the metric prefix, for example, when going from micrograms to kilograms you would put -6(refer to above chart)\nWhen it asks for the target exponential value you would use one of the above metric scientific notations, for example when trying to get to kilo- you would put 3.");

System.out.println();

System.out.println("Please put enter the digit term:");
dSciNoteP1 = oScan.nextDouble();

System.out.println("Please enter the exponential term:");
dSciNoteP2 = oScan.nextDouble();

System.out.println("Please enter the base exponential value:");
dBase = oScan.nextDouble();

System.out.println("Please enter the target exponential value:");
dTarget = oScan.nextDouble();

dBaseminusTarget = Math.abs(dBase - dTarget);
dBaseminusTarget = (Math.pow(dTen, dBaseminusTarget));

if(dBase < dTarget){
dAnswer = dSciNoteP1 * dBaseminusTarget;
System.out.println("The answer is: "+dAnswer);
}
if(dBase > dTarget){
dAnswer = dSciNoteP1 / dBaseminusTarget;
System.out.println("The answer is: "+dAnswer);
}

System.out.println("To restart press, close the program and launch again.");
System.out.println("This was made by Ryan Fabela. If you have any problems, send me an email at s0m3b0dystuff@gmail.com.");

}

}```
but I'm not sure how to do it..... Thanks

2. Member
Join Date
Sep 2010
Posts
42
Rep Power
0
Ignore the bottom code. Its wrong. I meant to put

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI
{
//setting up the width and the height values for use with the JPanel
private int Width = 300;
private int Height = 75;
//defining components
private JFrame jFrame;
private JPanel jPanel;
private JLabel jInput, jTentoPower, jTable, jResult;
private JTextField jFirstPart, jSecondPart;

public int TentoThePower;

//'main' part, where all the components are set up and other methods are used, actionListener and display()
public GUI()
{
//setting up JFrame
jFrame = new JFrame("Dimensional Analysis (s0m3b0dy)");//jFrames title
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
//defining jLabels
jTable = new JLabel("tera-, T, 10^12 | giga-, G, 10^9 | mega-, M, 10^6 | kilo-, k, 10^3 | deci-, d, 10^-1 | centi-, c, 10^-2 | milli-, m, 10^-3 | micro-, µ, 10^-6 | nano-, n, 10^-9 | pico-, p, 10^-12 | For base units use 10^0");

jInput = new JLabel("Enter the base value(The value you are starting with):");
jTentoPower = new JLabel("x 10^");
jResult = new JLabel("---");
jTarget = new jLabel("Enter the targets exponetial value. Refer to above. :")

jFirstPart = new JTextField(10);
jFirstPart.addActionListener(new DimListener());
jSecondPart= new JTextField(10);
jSecondPart.addActionListener(new TenToThePower());

jPanel = new JPanel();
jPanel.setPreferredSize(new Dimension(Width, Height));
jPanel.setBackground(Color.green);
jPanel.add(jTable);
jPanel.add(jInput);
jPanel.add(jFirstPart);
jPanel.add(jTentoPower);
jPanel.add(jSecondPart);
jPanel.add(jResult);
jFrame.getContentPane().add(jPanel);

}

public void display()
{
jFrame.pack();
jFrame.show();
}

public class TenToThePower implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
TentoThePower = Integer.parseInt(jTentoPower.getText());
}
}

public class DimListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{

}
}

}

3. Member
Join Date
Oct 2010
Posts
63
Rep Power
0
You need to peruse the java tutorials, but this might help you get started.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI extends JFrame {
//setting up the width and the height values for use with the JPanel

private int Width = 600;
private int Height = 175;
//defining components
private JFrame jFrame;
private JPanel jPanel;
private JLabel jInput, jTentoPower, jTarget;
private JTextArea jTable;
private JTextField jFirstPart, jSecondPart, jResult;
private int TentoThePower;

//'main' part, where all the components are set up and other methods are used, actionListener and display()
public GUI() {
//setting up JFrame
jFrame = new JFrame("Dimensional Analysis (s0m3b0dy)");//jFrames title
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
//defining jLabels
jTable = new JTextArea(
"tera-, T, 10^12 | giga-, G, 10^9 | mega-, M, 10^6 | \n" +
"kilo-, k, 10^3 | deci-, d, 10^-1 | centi-, c, 10^-2 |\n" +
" milli-, m, 10^-3 | micro-, µ, 10^-6 | nano-, n, 10^-9 |\n" +
" pico-, p, 10^-12 |\n\n" +
" For base units use 10^0");
jTable.setBackground(Color.green);
jTable.setMargin(new Insets(10, 10, 10, 10));

jInput = new JLabel("Enter the base value(The value you are starting with):");
jTentoPower = new JLabel("x 10^");
jResult = new JTextField(10);
jTarget = new JLabel("Enter the targets exponetial value. Refer to above. :");

jFirstPart = new JTextField(10);
// jFirstPart.addActionListener(new DimListener());
jSecondPart = new JTextField(10);
jSecondPart.addActionListener(new TenToThePower());

jPanel = new JPanel();
jPanel.setPreferredSize(new Dimension(Width, Height));
jPanel.setBackground(Color.green);

jPanel.add(new JPanel().add(jTable));
jPanel.add(jInput);

JPanel bottomPanel = new JPanel(new FlowLayout());
bottomPanel.add(jFirstPart);
bottomPanel.add(jTentoPower);
bottomPanel.add(jSecondPart);
bottomPanel.add(new JLabel(" = "));
bottomPanel.add(jResult);
jPanel.add(bottomPanel);
jFrame.getContentPane().add(jPanel);

}

public void display() {
jFrame.pack();
jFrame.setVisible(true);
}

public class TenToThePower implements ActionListener {

public void actionPerformed(ActionEvent event) {
Object c = event.getSource();
TentoThePower = Integer.parseInt(((JTextField)c).getText());
Integer base = Integer.parseInt(jFirstPart.getText());
System.out.println("===> " + base);
System.out.println("===> " + TentoThePower);
Double result = base * Math.pow(10, TentoThePower);
jResult.setText(result.toString());
}
}

// public class DimListener implements ActionListener {
//
// public void actionPerformed(ActionEvent event) {
// }
// }

public static void main(String[] args) {
GUI view = new GUI();
view.display();
}
}

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•