Need some java help - Overclock.net - An Overclocking Community
Forum Jump: 

Need some java help

 
Thread Tools
post #1 of 6 (permalink) Old 06-14-2011, 09:48 AM - Thread Starter
New to Overclock.net
 
JCarney's Avatar
 
Join Date: Jul 2010
Posts: 20
Rep: 0
I cannot for the life of me figure out how to put 3 different user inputs into 3 different panels of a primary panel. Any help/guidance is appreciated!
JCarney is offline  
Sponsored Links
Advertisement
 
post #2 of 6 (permalink) Old 06-14-2011, 10:04 AM
New to Overclock.net
 
travesty's Avatar
 
Join Date: Aug 2010
Location: Rochester, NY
Posts: 817
Rep: 144 (Unique: 126)
i think what you need is a structure like this
Code:
JPanel parent = new JPanel(new FlowLayout());
JPanel child1 = new JPanel();
JPanel child2 = new JPanel();
JPanel child3 = new JPanel();

JTextField input1 = new JTextField();
JTextField input2 = new JTextField();
JTextField input3 = new JTextField();

child1.add(input1);
child2.add(input2);
child3.add(input3);

parent.add(child1);
parent.add(child2);
parent.add(child3);


travesty is offline  
post #3 of 6 (permalink) Old 06-14-2011, 10:17 AM - Thread Starter
New to Overclock.net
 
JCarney's Avatar
 
Join Date: Jul 2010
Posts: 20
Rep: 0
Quote:
import java.awt.*;
import javax.swing.*;

public class Triathlon
{
public static void main (String[] args)
{

Triathlete one = new Triathlete ("Smith", "Andrea", 3605, 3600);
Triathlete two = new Triathlete();
Triathlete three = new Triathlete();

JFrame frame = new JFrame ("FiveK Run Display");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JPanel subPanel1 = new JPanel();
subPanel1.setPreferredSize (new Dimension(300, 200));
subPanel1.setBackground(Color.green);
JLabel label1 = new JLabel();
subPanel1.add(label1);

JPanel subPanel2 = new JPanel();
subPanel2.setPreferredSize (new Dimension(300,200));
subPanel2.setBackground(Color.blue);
JLabel label2 = new JLabel();
subPanel1.add(label2);

JPanel subPanel3 = new JPanel();
subPanel3.setPreferredSize (new Dimension(300,200));
subPanel3.setBackground(Color.red);
JLabel label3 = new JLabel ();
subPanel1.add (label3);

JPanel primary = new JPanel();
primary.setBackground(Color.yellow);
primary.add (subPanel1);
primary.add (subPanel2);
primary.add (subPanel3);

frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);


}
}
Quote:
import java.text.DecimalFormat;
import java.util.Scanner;


public class Triathlete
{
private String last;
private String first;
private int seconds;
private int personalRecord;
private double kph;

DecimalFormat dfmt = new DecimalFormat("0.##");
Scanner scan = new Scanner(System.in);



public Triathlete()
{

}

public Triathlete (String last, String first, int seconds, int personalRecord)
{
this.last = last;
this.first = first;
this.seconds = seconds;
this.personalRecord = personalRecord;

kph = 25.75 / (seconds / 3600d);


}

public String getLast()
{
return last;
}

public String getFirst()
{
return first;
}

public int getSeconds()
{
return seconds;
}

public int getPersonalRecord()
{
return personalRecord;
}

public double getKph()
{
return kph;
}

public void setLast(String last)
{
this.last = last;
}

public void setFirst(String first)
{
this.first = first;
}

public void setSeconds(int seconds)
{
this.seconds = seconds;
}

public void setPersonalRecord(int personalRecord)
{
this.personalRecord = personalRecord;
}

public void setKph(double kph)
{
this.kph = kph;
}

public String toString()
{
if (seconds == 0){
System.out.println("Enter last name: " );
String last = scan.next();
System.out.println("Enter first name: ");
String first = scan.next();
System.out.println("Enter seconds: ");
seconds = scan.nextInt();
System.out.println("Enter personal record: ");
personalRecord = scan.nextInt();

return "Name: " + last + ", " + first + "\nTime in seconds: " + seconds + "\nPR Time in seconds: "
+ personalRecord + "\nAverage KM per hour: " + dfmt.format(25.75 / (seconds / 3600d))
+ "\nAverage miles per hour: " + dfmt.format((25.75 / (seconds / 3600d)) * 0.621371192);
}

return "Name: " + last + ", " + first + "\nTime in seconds: " + seconds + "\nPR Time in seconds: "
+ personalRecord + "\nAverage KM per hour: " + dfmt.format(kph) + "\nAverage miles per hour: "
+ dfmt.format((25.75 / (seconds / 3600d)) * 0.621371192);

}




}


I have to get three objects from Triathlete into separate panels. I have the panels set up and as you can see my one object has specific parameters while the others accept input. How can I get each object into it's own subpanel?
JCarney is offline  
Sponsored Links
Advertisement
 
post #4 of 6 (permalink) Old 06-14-2011, 10:49 AM - Thread Starter
New to Overclock.net
 
JCarney's Avatar
 
Join Date: Jul 2010
Posts: 20
Rep: 0
I could seriously use some help!
JCarney is offline  
post #5 of 6 (permalink) Old 06-14-2011, 11:13 AM
New to Overclock.net
 
travesty's Avatar
 
Join Date: Aug 2010
Location: Rochester, NY
Posts: 817
Rep: 144 (Unique: 126)
to gather input for the other 2 triathletes you can put text fields in the subpanels. then get the info from the text fields and pass it onto the constructor for triathlete.

on submit button click:
Triathlete one = new Triathlete (textfield1.getText(), textfield2.getText(), ...);


travesty is offline  
post #6 of 6 (permalink) Old 06-14-2011, 03:39 PM
 
Join Date: May 2011
Location: Champaign, IL
Posts: 274
Quote:
Originally Posted by JCarney View Post
I could seriously use some help!
Why don't you just come out and say you want someone to do it? He gave you more than enough information to go off of. Also, if you want anyone to bother looking at your code, consider placing
Code:
 and
tags around it.
iPoop is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off