Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help merging program code with GUI code

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help merging program code with GUI code

    I made a quadratic formula solver, and built the GUI using WindowsBuilder Pro in Eclipse, I want to know how do i merge my code with the gui so that when i press buttons they do what i want.

    ** I also dont know how to add a display widget, in which there is a screen to display user input (check comments)

    Thanks Guys


    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.SWT;
    import java.util.Scanner;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
     
    class Quad {
    	public static void main(String args[]) {
    		Scanner key = new Scanner(System.in);
     
    		//need to make three screens to display user input
     
    		System.out.print("Enter a: ");double a = key.nextDouble();
    		System.out.print("Enter b: ");double b = key.nextDouble();
    		System.out.print("Enter c: ");double c = key.nextDouble();
     
    		System.out.println("");
     
    		double x = 0; x =(-b + Math.sqrt(b*b - (4 * a * c)))/ (2 * a);
    		double y = 0; y = (-b - Math.sqrt(b*b - (4 * a * c)))/ (2 * a);
     
    		//need to make two screens to display both outputs
     
    		System.out.println("x1= " + x);
    		System.out.println("x2= " + y);
    	}
    }
    public class QuadraticWindow {
     
    	/**
    	 * Launch the application.
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Display display = Display.getDefault();
    		Shell shlQuadraticApp = new Shell();
    		shlQuadraticApp.setSize(450, 300);
    		shlQuadraticApp.setText("Quadratic App");
     
    		Button btnCalculate = new Button(shlQuadraticApp, SWT.NONE);
    		btnCalculate.addSelectionListener(new SelectionAdapter() {
    			@Override
    			public void widgetSelected(SelectionEvent e) {
    			}
    		});
    		btnCalculate.setBounds(183, 126, 68, 23);
    		btnCalculate.setText("Calculate");
     
    		Button button = new Button(shlQuadraticApp, SWT.NONE);
    		button.addSelectionListener(new SelectionAdapter() {
     
    		});
    		button.setBounds(10, 89, 29, 23);
    		button.setText("1");
     
    		Button button_1 = new Button(shlQuadraticApp, SWT.NONE);
    		button_1.setText("4");
    		button_1.setBounds(10, 118, 29, 23);
     
    		Button button_2 = new Button(shlQuadraticApp, SWT.NONE);
    		button_2.setText("7");
    		button_2.setBounds(10, 147, 29, 23);
     
    		Button button_3 = new Button(shlQuadraticApp, SWT.NONE);
    		button_3.setText("8");
    		button_3.setBounds(45, 147, 29, 23);
     
    		Button button_4 = new Button(shlQuadraticApp, SWT.NONE);
    		button_4.setText("5");
    		button_4.setBounds(45, 118, 29, 23);
     
    		Button button_5 = new Button(shlQuadraticApp, SWT.NONE);
    		button_5.setText("2");
    		button_5.setBounds(45, 89, 29, 23);
     
    		Button button_6 = new Button(shlQuadraticApp, SWT.NONE);
    		button_6.setText("3");
    		button_6.setBounds(80, 89, 29, 23);
     
    		Button button_7 = new Button(shlQuadraticApp, SWT.NONE);
    		button_7.setText("6");
    		button_7.setBounds(80, 118, 29, 23);
     
    		Button button_8 = new Button(shlQuadraticApp, SWT.NONE);
    		button_8.setText("9");
    		button_8.setBounds(80, 147, 29, 23);
     
    		shlQuadraticApp.open();
    		shlQuadraticApp.layout();
    		while (!shlQuadraticApp.isDisposed()) {
    			if (!display.readAndDispatch()) {
    				display.sleep();
    			}
    		}
    	}
    }


  2. #2
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Help merging program code with GUI code

    First, separate your files into two classes. Secondly, using the other class - not the main method, define a Handler class and link it to your members in the same class. Making the inner class private is advisable. If you have not used inner classes, check out some tutorials. Within that private inner class, define your method to handle whatever you want and remember to add stuff to the frame. Read more! good luck

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help merging program code with GUI code

    Thanks ill look it up

Similar Threads

  1. Replies: 7
    Last Post: November 26th, 2011, 07:29 AM
  2. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  3. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM
  4. Improving the code to produce the same program
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 13
    Last Post: April 8th, 2011, 09:41 PM
  5. HELP with Java Paint Program Code
    By CheekySpoon in forum AWT / Java Swing
    Replies: 1
    Last Post: April 5th, 2010, 12:03 PM