Post: [JAVA] GUI Help
01-05-2012, 03:55 PM #1
tylerallmighty
Human After All
(adsbygoogle = window.adsbygoogle || []).push({}); So after I got the GUI figured out, I decided to clean my computer a 'lil, by organization. So I installed the JDK 7, JFK 2.0 SDK, the JRE, and Eclipse on my flash drive. In doing so, I deleted my prior project without backing it up. Starting from scratch I figured "I'll make a new one. Gotta practice anyways." So I did, and now I can't get any of the buttons or text areas on the form. I've added them and the panels they've been added to. I'll list the code below and see if you guys can help.
I have kept it quite organized and neat which will allow me to add quite a bit more in the future if I want to, all thanks to multiple classes.

Main.java (Can't run without a main class can ya?)
    
public class Main {


public static void main(String[] args) {
// Create class objects
Generate genObj = new Generate();
GUI gui = new GUI();


// Initialize the GUI
gui.GUI();
// Print out generated number by calling the "Generate" class
System.out.println(Integer.toString(genObj.genRan()));
}


}




Generate.java (Contains all the stuff having to do with generating the number
    
import java.util.Random;


public class Generate {


public int genRan() {
// Create Random variable "rand"
Random rand = new Random();
// Assign "result" the value of "rand" to the next generated integer
int result = rand.nextInt();


// Generate the number and print it out Smile
for (int i = 0; i <= 0; i++) {
result = rand.nextInt();
System.out.println("Working...\nNumber generated.");
}
return result;
}
}


And last but not least: GUI.java (Contains all the GUI stuff)
    
import javax.swing.*;
import java.awt.*;


public class GUI {


private static final String TITLE = "Randomized Number";
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
private final String INSIDETITLE = "Testing";
private final String genNumLabel = "Generated Number: ";


public void GUI() {
// Class objects
Generate genObj = new Generate();

// Frame options
JFrame frame = new JFrame(TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);


// Initialize the buttons.
JButton btnGen = new JButton("Generate");
JButton btnExit = new JButton("EXIT");
JButton btnCopy = new JButton("Copy");


// Initialize the text areas.
JTextArea generatedNum = new JTextArea(genNumLabel);
JLabel theTitle = new JLabel(INSIDETITLE);


// Initialize panels
JPanel textArea = new JPanel();
JPanel btnsArea = new JPanel();


// Panel options


// Add the components to the panels
textArea.add(generatedNum);
textArea.add(theTitle);


btnsArea.add(btnGen);
btnsArea.add(btnExit);
btnsArea.add(btnCopy);


// Add the panels to the frame
frame.add(textArea);
frame.add(btnsArea);
}
}




PS. I'm getting more proficient in Java so don't hesitate to ask for help from me, even though I'm asking you for help right now.
PPS. I've also decided to stick with Java and learn it all the way through to adulthood. I think it'll be a good career choice for me, considering I LOVE Java and plan to possibly get a job in which I get to use it all day err'day. :P
01-05-2012, 07:34 PM #2
Epic?
Awe-Inspiring
Originally posted by bananaman
So after I got the GUI figured out, I decided to clean my computer a 'lil, by organization. So I installed the JDK 7, JFK 2.0 SDK, the JRE, and Eclipse on my flash drive. In doing so, I deleted my prior project without backing it up. Starting from scratch I figured "I'll make a new one. Gotta practice anyways." So I did, and now I can't get any of the buttons or text areas on the form. I've added them and the panels they've been added to. I'll list the code below and see if you guys can help.
I have kept it quite organized and neat which will allow me to add quite a bit more in the future if I want to, all thanks to multiple classes.

Main.java (Can't run without a main class can ya?)
    
public class Main {


public static void main(String[] args) {
// Create class objects
Generate genObj = new Generate();
GUI gui = new GUI();


// Initialize the GUI
gui.GUI();
// Print out generated number by calling the "Generate" class
System.out.println(Integer.toString(genObj.genRan()));
}


}




Generate.java (Contains all the stuff having to do with generating the number
    
import java.util.Random;


public class Generate {


public int genRan() {
// Create Random variable "rand"
Random rand = new Random();
// Assign "result" the value of "rand" to the next generated integer
int result = rand.nextInt();


// Generate the number and print it out Smile
for (int i = 0; i <= 0; i++) {
result = rand.nextInt();
System.out.println("Working...\nNumber generated.");
}
return result;
}
}


And last but not least: GUI.java (Contains all the GUI stuff)
    
import javax.swing.*;
import java.awt.*;


public class GUI {


private static final String TITLE = "Randomized Number";
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
private final String INSIDETITLE = "Testing";
private final String genNumLabel = "Generated Number: ";


public void GUI() {
// Class objects
Generate genObj = new Generate();

// Frame options
JFrame frame = new JFrame(TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);


// Initialize the buttons.
JButton btnGen = new JButton("Generate");
JButton btnExit = new JButton("EXIT");
JButton btnCopy = new JButton("Copy");


// Initialize the text areas.
JTextArea generatedNum = new JTextArea(genNumLabel);
JLabel theTitle = new JLabel(INSIDETITLE);


// Initialize panels
JPanel textArea = new JPanel();
JPanel btnsArea = new JPanel();


// Panel options


// Add the components to the panels
textArea.add(generatedNum);
textArea.add(theTitle);


btnsArea.add(btnGen);
btnsArea.add(btnExit);
btnsArea.add(btnCopy);


// Add the panels to the frame
frame.add(textArea);
frame.add(btnsArea);
}
}




PS. I'm getting more proficient in Java so don't hesitate to ask for help from me, even though I'm asking you for help right now.
PPS. I've also decided to stick with Java and learn it all the way through to adulthood. I think it'll be a good career choice for me, considering I LOVE Java and plan to possibly get a job in which I get to use it all day err'day. :P


Just add:
    frame.setVisible(true);

At the very bottom of your GUI method for your GUI class.

Oh, and you're still missing some button functionality.

---------- Post added at 11:34 AM ---------- Previous post was at 11:31 AM ----------

Originally posted by reScript
I don't do Java so I may be wrong, do you not have to input dimensions for the object to be drawn?

Oh and off the GUI lines, I don't understand your "generate script", you seem to have a loop that would never even start. :p

All you need is this to have the same functionality & return.
    
public class Generate {
public int genRan() {
//Create Random variable "rand"
Random rand = new Random();
//Assign value
int result = rand.nextInt();
//Return
return result;
}
}


Or he could simply do:
    
public class Generate {
public int genRan() {
return new Random().nextInt();
}
}
01-06-2012, 02:40 AM #3
tylerallmighty
Human After All
Originally posted by Epic
Just add:
    frame.setVisible(true);

At the very bottom of your GUI method for your GUI class.

Oh, and you're still missing some button functionality.

---------- Post added at 11:34 AM ---------- Previous post was at 11:31 AM ----------



Or he could simply do:
    
public class Generate {
public int genRan() {
return new Random().nextInt();
}
}


/facepalm on my part. :P

Originally posted by reScript
Or he could just not have it as a return in the first place, nothing is good enough for you is it. :\


Hey hey hey now. Gotta give the guy credit. He is really good at this and what's wrong with showing me something different? :P

---------- Post added at 10:40 PM ---------- Previous post was at 10:36 PM ----------

Originally posted by Epic
Just add:
    frame.setVisible(true);

At the very bottom of your GUI method for your GUI class.

Oh, and you're still missing some button functionality.

---------- Post added at 11:34 AM ---------- Previous post was at 11:31 AM ----------



Or he could simply do:
    
public class Generate {
public int genRan() {
return new Random().nextInt();
}
}


And I know I'm missing stuff. I just wanted to get the current stuff done before moving on.
01-06-2012, 03:54 AM #4
tylerallmighty
Human After All
Originally posted by reScript
You don't understand what this guy is like, he stalks the programming forums and quotes everything anybody says, for some reason he likes to quote you say in more detail on something you have written, I think he is just arrogant tbh.


I'm friends with the guy (He's only 15) and he means well (By that I mean we're admins together. Dead forum, but still.). I love reading his tutorials and receiving his help. He's really a great programmer who has more knowledge than most (including me, I'm quite stupid). I just think that he takes things a bit further than us. He has a bigger interest in computer science than a lot of other people I've met. I really don't think he's intentionally being arrogant, but there's nothing wrong with flaunting his knowledge. He takes things more seriously too, which can get a wee bit annoying (not British). Just listen to him and accept what has been given to you. Think about how nooby we'd all be if there weren't people like him posting tutorials and correcting you.
01-06-2012, 04:03 AM #5
tylerallmighty
Human After All
Originally posted by reScript
He's not correcting me or showing me anything, he just goes in detail for no reason.


Here's my full and honest opinion. I'm really trying to avoid conflict.

He's just trying to help. I honestly love the detail he gives. Here's an example: If your playing Football (American style, duh), and your only told what you've done wrong and how to fix it, you don't know the reasoning behind it. Coach "Go tackle that guy" Me *runs and tackles too high* Coach "Wrong. Hit 'em low at the legs" Me *runs and tackles legs* Coach "Know why that works? Knocking his legs out form underneath him prevents him from running. Meaning he gets less yards than when you hit 'em too high" What I'm saying is, you learn more behind it so you can understand how it works better. This is the way ADD's like me learn.
01-06-2012, 11:07 PM #6
tylerallmighty
Human After All
Originally posted by reScript
Like I said above, I either already know what he's trying to explain or its completely ir******** to anything I have said.


You make a good point there...

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo