Search code examples
javaswingjframejpaneljtextfield

Java GUI not showing


import java.util.Scanner;

import javax.swing.*;
@SuppressWarnings("serial")
public class Synthetic_Division extends JFrame{
public void init(){
    JTextField JTF = new JTextField();
    JLabel label = new JLabel();
    JPanel panel = new JPanel();
    panel.add(label);
    panel.add(JTF);
    this.add(panel);
}
public Synthetic_Division(){
    super("Synthetic Division");
    this.init();
    this.setSize(600, 400);
    this.setVisible(true);

}
public static void main(String[] args) {
    int sol01 = 0, sol12 = 0, sol23 = 0, sol34 = 0, sol45 = 0, cross12 = 0, cross23 = 0, cross34 = 0, cross45 = 0, cross56 = 0, Remainder = 0, Divisor = 0, Number1 = 0, Number2 = 0, Number3 = 0, Number4 = 0, Number5 = 0, Number6 = 0, NumberOfNumbers = 0;
    System.out.println("Please input the following values accordingly.");
    System.out.println("1. Numbers in equation (: 4-6 :)");
    System.out.println("2. Divisor (: After the sign has been flipped :)");
    System.out.println("3. First Number");
    System.out.println("4. Second Number");
    System.out.println("5. Third Number");
    System.out.println("6. Fourth Number");
    System.out.println("7. Fifth Number (: If Needed :)");
    System.out.println("8. Sixth Number (: If Needed :)");
    Scanner in = new Scanner(System.in);
    NumberOfNumbers = in.nextInt();
    Divisor = in.nextInt();
    Number1 = in.nextInt();
    Number2 = in.nextInt();
    Number3 = in.nextInt();
    Number4 = in.nextInt();
    if (NumberOfNumbers == 5 || NumberOfNumbers == 6) {
        Number5 = in.nextInt();
    }
    if (NumberOfNumbers == 6) {
        Number6 = in.nextInt();
    }
    if (NumberOfNumbers == 4) {
        sol01 = Number1;
        cross12 = sol01 * Divisor;
        sol12 = Number2 + cross12;
        cross23 = sol12 * Divisor;
        sol23 = Number3 + cross23;
        cross34 = sol23 * Divisor;
        Remainder = Number4 + cross34;
        if (Remainder == 0) {
            System.out.println(sol01 + " + " + sol12 + " + " + sol23
                    + " with no remainder! ");
        } else {
            System.out.println(sol01 + " + " + sol12 + " + " + sol23
                    + " with a remainder of: " + Remainder);
        }

    } else if (NumberOfNumbers == 5) {
        sol01 = Number1;
        cross12 = sol01 * Divisor;
        sol12 = Number2 + cross12;
        cross23 = sol12 * Divisor;
        sol23 = Number3 + cross23;
        cross34 = sol23 * Divisor;
        sol34 = Number4 + cross34;
        cross45 = sol34 * Divisor;
        Remainder = Number5 + cross45;
        if (Remainder == 0) {
            System.out.println(sol01 + " + " + sol12 + " + " + sol23 + " + "
                    + sol34 + " with no remainder! ");
        } else {
            System.out.println(sol01 + " + " + sol12 + " + " + sol23
                    + " + " + sol34 + " with a remainder of: " + Remainder);
        }
    } else if (NumberOfNumbers == 6) {
        sol01 = Number1;
        cross12 = sol01 * Divisor;
        sol12 = Number2 + cross12;
        cross23 = sol12 * Divisor;
        sol23 = Number3 + cross23;
        cross34 = sol23 * Divisor;
        sol34 = Number4 + cross34;
        cross45 = sol34 * Divisor;
        sol45 = Number5 + cross45;
        cross56 = sol45 * Divisor;
        Remainder = Number6 + cross56;
        if (Remainder == 0) {
            System.out.println(sol01 + " + " + sol12 + " + " + sol23 + " + "
                    + sol34 + " + " + sol45 + " with no remainder! ");
        } else {
            System.out.println(sol01 + " + " + sol12 + " + " + sol23
                    + " + " + sol34 + " + " + sol45
                    + " with a remainder of: " + Remainder);
        }
    } else {
        System.out.println("Please input correct number of numbers");
    }

}
}

For some reason, when I run this code, no GUI shows, but there is a program that starts running that I can stop at any point. As you can probably tell by the code, I am exceptionally new to Java. I just added the rest of my code to hopefully help with troubleshooting, although I still haven't implemented the GUI into the main class yet as I wanted to see if the GUI would be what I wanted to have it be first.


Solution

  • You are missing the "entrance" into your program. In java this is the main() method which I have added to the code you provided. You just need to actually create an object of the class you wrote. Also, you might want to check out Swing's EDT (Event Dispatching Thread) if you are going to be getting into swing development. Find it here.

    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class Synthetic_Division extends JFrame {
        void init() {
            JTextField JTF = new JTextField();
            JLabel label = new JLabel();
            JPanel panel = new JPanel();
            panel.add(label);
            panel.add(JTF);
            this.add(panel);
        }
    
        public Synthetic_Division() {
            super("Synthetic Division");
            this.init();
            this.setSize(600, 400);
            this.setVisible(true);
        }
    
        //Here is the part you were missing. Everything else is fine.
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    new Synthetic_Division();
                }
            });
        }
    }
    

    EDIT

    After seeing your edit I can see the main problem was that you never created an instance of your class. The main thing you were missing was new Synthetic_Division();. Some other tips: Try not to combine command line (System.out.println()) and GUI code into one program. This can get confusing quickly, for you and for the user. Also, try to conform to standard Java naming convention. Classes start with uppercase letters and each word after is uppercase as well SyntheticDivision while variables start with lowercase letters and each word after is uppercase myVariableToWorkWith.