Results 1 to 4 of 4
  1. #1
    sambo731 is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default JFrame - Output and Input in Frame - menu choice and simple Calculator

    Okay so i have 2 Java files
    Java Main Class - "Main.java"
    and
    Java Class - "frame.java"
    these java files are in the same folder and project called "menuchoice"

    Basically what i want to happen is have a JFrame window and have all the input and output code that comes with the calculator and menuchoice happen in the JFrame and not in the IDE NetBeans Run Window

    Inside frame.java

    // this is the frame set up java file

    Java Code:
    package menuchoice;
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class frame extends JFrame{
    
        private JLabel item1;
    
        public frame(){
            super("The Title Bar");
            setLayout(new FlowLayout());
    
            item1 = new JLabel("This Is The Label");
            item1.setToolTipText("This Is Show On Hover");
            add(item1);
        }
    }

    Inside Main.java

    // this is the file that contains the menu + calculator code.

    Java Code:
    package menuchoice;
    
    import javax.swing.JFrame;
    import java.util.Scanner;
    public class Main {
    
      public static void main(String[] args) {
    
            frame oneframe = new frame();
            oneframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            oneframe.setSize(275,180);
            oneframe.setVisible(true);
    
            Scanner choice = new Scanner(System.in);{
                double menuselect;
                System.out.println("Please Enter the Number\n1. Addition\n2. Subtraction");
                menuselect = choice.nextDouble();
                if (menuselect == 1){
                    Scanner addidtion = new Scanner(System.in);
                    double fnum, snum, answer;
                    System.out.println("Enter first number: ");
                    fnum = addidtion.nextDouble();
                    System.out.println("Enter second number: ");
                    snum = addidtion.nextDouble();
                    answer = fnum + snum;
                    System.out.println(answer);
            }
                if (menuselect == 2){
                    Scanner subtraction = new Scanner(System.in);
                    double fnum, snum, answer;
                    System.out.println("Enter first number: ");
                    fnum = subtraction.nextDouble();
                    System.out.println("Enter second number ");
                    snum = subtraction.nextDouble();
                    answer = fnum - snum;
                    System.out.println(answer);}
    
                else{
                    System.out.println("Error: Enter a valid menu choice");}
            }
            }
    
    }
    Picture of what currently is happening and what i would like to happen.


    Thanks a lot guys any help/tips would be much appreciated, ive tried a few things but can figure it out.
    Last edited by Fubarable; 08-07-2010 at 05:21 AM. Reason: Moderator Edit: Bold tags changed to Code tags for readability

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Moderator edit: bold tags exchanged for code tags to aid readability. To the OP, to see how to use code tags, please click on the related link in my signature.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You really can't just move a console app to a GUI like you're trying to do. Well, you can, sort of, but it's ugly and kludgy. Part of the issue is that GUI applications don't just look different from console code, they are very different to the core since gui programs are event-driven while console programs are usually much more linear. What would be better is to read up on and learn how to create Swing apps from scratch, and you are interested, you should start here: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    Much luck and welcome!
    Last edited by Fubarable; 08-07-2010 at 05:28 AM.

  4. #4
    sambo731 is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. creating a simple calculator
    By hobo in forum New To Java
    Replies: 4
    Last Post: 11-09-2009, 03:09 AM
  2. Simple Calculator Display Problem :(
    By jimbob in forum Java Applets
    Replies: 4
    Last Post: 07-18-2009, 04:13 AM
  3. [SOLVED] Simple Conversion Calculator
    By dbashby in forum New To Java
    Replies: 6
    Last Post: 03-20-2009, 01:06 AM
  4. Sending output to a Frame
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-04-2008, 09:28 AM

Tags for this Thread

Posting Permissions

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