Results 1 to 3 of 3

Thread: GUI help?

  1. #1
    seegee is offline Member
    Join Date
    Jul 2011
    Rep Power

    Default GUI help?

    Hey guys! So i just started reading this book 3 days ago. Its called Beginner Java Programming For Dummies. Now, before I started reading this book, I had never even seen a peace of code. In these 3 days I have learned alot. So far I have made this program.

    import java.util.Scanner;
    import java.text.DecimalFormat;
    public class IDKKKKK

    public static void main(String args[]) {
    Scanner myScanner= new Scanner (;
    DecimalFormat formatter = new DecimalFormat(".00");
    double PayPerHour;
    double HoursADay;
    double DaysAWeek;
    double PerDay;
    double PerWeek;
    double PerYear;
    double itemcost;
    double time;
    char reply;
    double generalexpences;
    double PerWeekWithExpenses;

    System.out.print("How much are you paid an hour?");
    PayPerHour= myScanner.nextDouble();
    System.out.print("How many hours do you work a day?");
    HoursADay= myScanner.nextDouble();
    System.out.print("How many days a week do you work?");
    DaysAWeek= myScanner.nextDouble();
    PerDay= PayPerHour*HoursADay;
    PerWeek= PerDay*DaysAWeek;
    PerYear= PerWeek*52;

    System.out.print("You make ");
    System.out.print( PerDay);
    System.out.println(" dollars per day.");
    System.out.print(" You make ");
    System.out.println(" dollars per week.");
    System.out.print(" You Make ");
    System.out.println(" dollars per year.");
    System.out.println("Is there anything you want to save up for? (Y/N)");

    if (reply== 'Y' || reply=='y'){
    System.out.print ("How much does your item cost?");
    itemcost= myScanner.nextDouble();
    System.out.print ("How much do you pay a week in expenses?");
    generalexpences= myScanner.nextDouble();
    PerWeekWithExpenses= PerWeek-generalexpences;
    time= itemcost/PerWeekWithExpenses;
    System.out.print("It will take you ");
    System.out.print(" weeks of work to get enough money to purchase your item");

    if (reply== 'N' || reply== 'n'){System.exit(0);}

    if (reply !='N' || reply != 'n' || reply != 'Y'|| reply !='y')

    System.out.print ("Huh?");

    Now, the one thing I don't like about this book is that it only shows you how to code text based programs. I want to learn how to code gui. It briefly explains the concept in a few pages but I dont really understand it :( I have done a little more research on it and most things say you need to manually code the gui. I am using NetBeans to code right now and that has a function built in where you can create a new JFrame form and then drag Objects into it. So far, the farthest that I have gotten is making a program, with the NetBeans jframe thing, that prints Letters into the Output area when you press a button (by output area I mean where text based programs are ran.) Here is what that code looked like:

    * To change this template, choose Tools | Templates
    * and open the template in the editor.

    * Created on Jul 17, 2011, 9:45:52 PM
    package gui.pkg2;

    * @author seegee1237
    public class gfhjk extends javax.swing.JFrame {

    /** Creates new form gfhjk */
    public gfhjk() {

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(130, 130, 130)
    .addContainerGap(173, Short.MAX_VALUE))
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
    .addContainerGap(142, Short.MAX_VALUE)
    .add(129, 129, 129))

    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    System.out.print ("Test");

    * @param args the command line arguments
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new gfhjk().setVisible(true);
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration

    Now the only part of that code that I wrote was the "System.out.print ("test")

    NetBeans did the rest for me. I just dont understand how to do this at all, can I still use all the same classes like Scanner and stuff? I really cant figure it out. Can someone help?


  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power


    You can use whatever class you want, but you would rarely use the Scanner class in a GUI. The point of using a GUI is to display frames (forms) with components that the user enters data into. You wouldn't use a Scanner to read data from the command line.

    So, I suggest you forget about the IDE for building a GUI and take the time to read Trail: Creating a GUI With JFC/Swing: Table of Contents (The Java™ Tutorials). You can download the examples from the tutorial and play with them.

    Also, in the future, when you post code in a forum, don't forget to select the code and click the "Code" button (#) so the code will retain its formatting.

  3. #3
    seegee is offline Member
    Join Date
    Jul 2011
    Rep Power

Posting Permissions

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