2014-01-18 114 views
0
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是否是我想要的第一個。Java GUI沒有顯示

+3

你的main() – TwilightSun

回答

6

您錯過了您的程序中的「入口」。在java中,這是我添加到您提供的代碼中的main()方法。你只需要實際創建你寫的類的一個對象。另外,如果您打算進入秋千開發階段,您可能需要查看Swing的EDT(Event Dispatching Thread)。找到它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

哪裏是初始線程 – MadProgrammer

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); 
    } 
} 
3
  1. 你從來沒有在main實例Synthetic_Division

    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 
         } 
        }); 
    } 
    
-1
// you need to make an object of Synthetic_Division. 
Synthetic_Division SD = new Synthetic_Division(); 

我希望它能正常工作。