2014-12-13 76 views
1

我在學習Java連同IntelliJ IDEA。我想嘗試在Oracle教程中解釋的攝氏轉換器,所以我做了以下步驟:GUI不會出現在IntelliJ IDEA

  • 創建一個新的GUI窗體。

  • 創建了一個名爲CelsiusConverterGUI的類。

它說的形式自動綁定到CelsiusConverterGUI.java

下面是CelsiusConverterGUI類聲明:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


public class CelsiusConverterGUI extends Frame{ 
    private JTextField celsiusDegree; 
    private JButton convertButton; 
    private JLabel fahrenheitDeg; 
    private JPanel panel; 

    public CelsiusConverterGUI(){ 
    setVisible(true); 
    convertButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
     int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText())); 
     fahrenheitDeg.setText(fahrDegree+"Fahrenheit"); 
     } 
    }); 
    } 

    public static void main(String[] args){ 
    new CelsiusConverterGUI(); 
    } 
} 

當我運行它,一個的Java應用程序窗口彈出hodling只在菜單欄的默認圖標(X-+)並不顯示我在GUI窗體中創建的面板或按鈕。

任何人都可以解釋爲什麼它的行爲如此?

回答

2

問題:

  • 你擴展AWT Frame類不是鞦韆JFrame類
  • 你沒有你的GUI添加。在您的構造函數中,您應該將組件添加到您的JPanel中,並將其添加到您的JFrame ,然後將其設置爲可見。
  • 你需要通過Swing教程,你可以找到here
  • 我在學習一個新庫時的建議 - 避免代碼生成工具,而是學習手工編寫代碼,因爲這將爲理解庫內部提供更好的基礎。
  • 你正在學習一個新的圖書館,當你剛剛開始時,它會很困難,但要堅持下去,避免通過閱讀教程來猜測,它就會到來。
+0

但正如我的理解,我曾經創造了GUI形式,都喜歡按鈕或面板的元素應該是有界的,以我的班,對吧?因爲我認爲,如果我在構造函數中再次添加這些組件,那麼GUI將與我在GUI窗體中創建的GUI不同,因爲這些組件的大小和位置將被重置。 – 2014-12-13 13:58:22

+0

@zhang_rick:我不熟悉IntelliJ的Swing代碼生成工具,但無論如何,我建議您不要使用GUI構建器。作爲指導,使用本教程中較好的部分進行手工編寫。 – 2014-12-13 13:59:57

+0

好的。感謝您的建議 – 2014-12-13 14:02:17

1

@Hovercraft已經解決的主要問題,我想補充一點,你缺少你的GUI組件初始化,一定會導致NullPointerException一旦你得到你的幀視覺和功能:

  • 你對你的celsiusDegree文本字段調用getText()方法:

    int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText())); 
    
  • 雖然你只有聲明實例˚F ield不用初始化:

    private JTextField celsiusDegree; 
    

這裏下了正確版本的類,修復下從@Hovercraft答案主要問題和我的其他注意事項:

包org.wisebrains.thirdparty.gui;

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class CelsiusConverterGUI extends JFrame{ 
    private JTextField celsiusDegree; 
    private JButton convertButton; 
    private JLabel fahrenheitDeg; 
    private JPanel panel; 

    public CelsiusConverterGUI(){ 
    // GUI Components Initialization 
    convertButton = new JButton("Convert"); 
    celsiusDegree = new JTextField(""); 
    panel = new JPanel(new BorderLayout()); 
    fahrenheitDeg = new JLabel(); 
    //... 
    convertButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
     int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText())); 
     fahrenheitDeg.setText(fahrDegree + " Fahrenheit"); 
     } 
    }); 
    // Adding your GUI Components to the main panel then to the frame 
    panel.add(convertButton, BorderLayout.CENTER); 
    panel.add(celsiusDegree, BorderLayout.NORTH); 
    panel.add(fahrenheitDeg, BorderLayout.SOUTH); 
    this.add(panel); 
    this.setVisible(true); 
    this.setSize(300,200); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 


    public static void main(String[] args){ 
    new CelsiusConverterGUI(); 
    } 
} 
+0

謝謝。但這是在沒有GUI窗體的情況下創建GUI的方式。我想使用GUI表單,而不是通過手動編寫代碼來設置所有組件及其屬性。我絕對同意這種方式值得學習。但我真的想知道爲什麼GUI形式的方式不起作用。 – 2014-12-13 14:27:58

+0

(PS:我的代碼中的組件聲明不僅僅是聲明,它們實際上是在我在另一個選項卡中創建GUI窗體後自動生成的.GUI窗體具有由IDE生成的唯一源代碼,用於設置屬性組件。而intelliJ表示GUI表單已經綁定到該類) – 2014-12-13 14:31:49

0

我剛剛查看了IntelliJ IDEA for GUI形式的教程,並通過編輯main()方法找到了解決我的問題的方法。

下面的代碼:

import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 


    public class CelsiusConverterGUI{ 
     private JTextField celsiusDegree; 
     private JButton convertButton; 
     private JLabel fahrenheitDeg; 
     private JPanel panel; 
     private JLabel celsiusLabel; 


     public CelsiusConverterGUI(){ 


      convertButton.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText())*1.8+32); 
        fahrenheitDeg.setText(fahrDegree+" Fahrenheit"); 
       } 
      }); 

     } 


     public static void main(String[] args) { 
      JFrame frame = new JFrame("CelsiusConverterGUI"); 
      frame.setContentPane(new CelsiusConverterGUI().panel); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.pack(); 
      frame.setVisible(true); 
     } 

    } 

相比,我的原代碼,新的一個補充JFrame中架,setContentPane,默認關閉操作。

現在我的理解是,我確實不需要初始化我在GUI窗體中創建的組件。但我確實需要創建JFrame框架以使GUI窗體正常工作。

1

你錯過添加內容面板

下面是CelsiusConverterGUI類聲明:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


public class CelsiusConverterGUI extends Frame{ 
    private JTextField celsiusDegree; 
    private JButton convertButton; 
    private JLabel fahrenheitDeg; 
    private JPanel panel; 

    public CelsiusConverterGUI(){ 
    super("CelsiusConverterGUI");//name of you program 
    setSize(400,500);//size of this 
    setContentPane(panel);//to show you content(you miss it) 
    pack();//set content the same design in GUI 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//close form to exte 
    setVisible(true); 
    convertButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
     int fahrDegree = (int)(Double.parseDouble(celsiusDegree.getText())); 
     fahrenheitDeg.setText(fahrDegree+"Fahrenheit"); 
     } 
    }); 
    } 

    public static void main(String[] args){ 
    new CelsiusConverterGUI(); 
    } 
}