-
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 (System.in);
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();
formatter.format(PayPerHour);
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;
formatter.format(PerDay);
PerWeek= PerDay*DaysAWeek;
formatter.format(PerWeek);
itemcost=0;
PerYear= PerWeek*52;
formatter.format(PerYear);
System.out.print("You make ");
System.out.print( PerDay);
System.out.println(" dollars per day.");
System.out.print(" You make ");
System.out.print(PerWeek);
System.out.println(" dollars per week.");
System.out.print(" You Make ");
System.out.print(PerYear);
System.out.println(" dollars per year.");
System.out.println("Is there anything you want to save up for? (Y/N)");
reply= myScanner.next().charAt(0);
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;
formatter.format(time);
System.out.print("It will take you ");
System.out.print(time);
System.out.print(" weeks of work to get enough money to purchase your item");
System.exit(0);
}
if (reply== 'N' || reply== 'n'){System.exit(0);}
if (reply !='N' || reply != 'n' || reply != 'Y'|| reply !='y')
System.out.print ("Huh?");
System.exit(0);
}
}
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.
*/
/*
* gfhjk.java
*
* 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() {
initComponents();
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
.add(layout.createSequentialGroup()
.add(130, 130, 130)
.add(jButton1)
.addContainerGap(173, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap(142, Short.MAX_VALUE)
.add(jButton1)
.add(129, 129, 129))
);
pack();
}// </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() {
@Override
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?
Thanks!
-
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.
-