2016-04-23 14 views
0

スイングで小さなUIプログラムを作成しようとしています。私はいくつかのテキストフィールドを用意する必要がありますが、新しいテキストフィールドやテキストエリアを定義すると(フレームに追加する必要はありません)、コード内に追加されたものや自分自身をランダムに防ぐことができます。変更を行わずに同じコードを再コンパイルすることができ、おそらく1/3の時間で正しく動作します。この問題の原因は何ですか?それを変更できる方法はありますか?コードを貼り付けます:コードが変更されていなくてもJTextFieldがランダムに表示されない

import java.awt.*; 
import javax.swing.*; 

public class ThisApp 
{ 
    public static void main(String [] Args) 
    { 
     new ThisUI();  
    } 
} 


class ThisUI extends JFrame //implements ActionListener 
{ 
    public ThisUI() 
    { 
     setTitle("ThisApp - Best in the business"); 
     setResizable(false); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(300, 200); 
     setLayout(new GridLayout(2,1)); 

     JButton cat = new JButton("Cat"); 

     this.add(new JButton("Button")); 
     this.add(new JTextField("CAT CAT",10));  
    } 
} 

ありがとうございました!

答えて

3

問題は、フレームが表示された後にコンポーネントをフレームに追加し、すべてのコンポーネントのサイズが0になるようにレイアウトマネージャを起動しないためです。

すべてのコンポーネントがフレームに追加された後、フレームを表示する必要があります。それをしなかった

setTitle("ThisApp - Best in the business"); 
setResizable(false); 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

setLayout(new GridLayout(2,1)); 

JButton cat = new JButton("Cat"); 
this.add(new JButton("Button")); 
this.add(new JTextField("CAT CAT",10));  

setSize(300, 200); 
setLocationRelativeTo(null); 
setVisible(true); 
+0

だからあなたのコードの構造は、次のようになります。感謝万円! – cafemolecular

関連する問題