Java GUI没有显示

时间:2014-01-18 03:20:38

标签: java swing jframe jpanel jtextfield

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");
    }

}
}

出于某种原因,当我运行此代码时,没有GUI显示,但有一个程序开始运行,我可以在任何时候停止。正如你可以从代码中看到的那样,我对Java非常陌生。我刚刚添加了我的其余代码以帮助进行故障排除,尽管我还没有将GUI实现到主类中,因为我想看看GUI是否是我想要的第一个。

4 个答案:

答案 0 :(得分:6)

您错过了程序中的“入口”。在java中,这是我添加到您提供的代码中的main()方法。您只需要实际创建您编写的类的对象。此外,如果您要进入摇摆开发阶段,您可能想查看Swing的EDT(事件调度线程)。找到它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();
            }
        });
    }
}

修改

看到你的编辑后,我可以看到主要问题是你从未创建过类的实例。你遗漏的主要内容是new Synthetic_Division();。其他一些提示:尽量不要将命令行(System.out.println())和GUI代码组合到一个程序中。对于您和用户而言,这可能会很快造成混淆。此外,尝试符合标准Java命名约定。类以大写字母开头,后面的每个单词也是大写SyntheticDivision,而变量以小写字母开头,后面的每个单词都是大写myVariableToWorkWith

答案 1 :(得分:3)

我没有在任何地方看到main课程,这是Java在发布时所寻求的。看看下面的代码块:

import javax.swing.*;
@SuppressWarnings("serial")
public class Synthetic_Division extends JFrame
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        JTextField JTF = new JTextField();
        JLabel label = new JLabel();
        JPanel panel = new JPanel();
        panel.add(label);
        panel.add(JTF);
        frame.add(panel);
        frame.setTitle("Synthetic Division");
        frame.setSize(600, 400);
        frame.setVisible(true);
    }
}

答案 2 :(得分:3)

  1. 您永远不会在Synthetic_Division中实例化main

    new Synthetic_Division();
    
  2. 试图将控制台与GUI混合的设计很差

  3. 您需要像这样指定文本字段的大小

    new JTextField(20);
    
  4. 从EDT运行你的程序

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new Synthetic_Division();
                // everything else here
            }
        });
    }
    

答案 3 :(得分:-1)

// you need to make an object of Synthetic_Division.
Synthetic_Division SD = new Synthetic_Division();

我希望它能正常运作。