2017-04-21 12 views
0

大きなプロジェクトのために、単純なカラーセレクタパネルをJavaで作成しようとしています。 RGBスライダー用のパネルを含むはずのフレームがあり、3つのテキストフィールドに値が表示されます。私は問題なくスライダーパネルを追加することができますが、テキストフィールドパネルを追加しようとすると全体が壊れ、パネルは表示されません。私の唯一の疑問は、パネルのこの問題を解決する方法です。ありがとうございました。ここでJFrameは複数のパネルを表示しません

が私のコードです:

//importing necessary libraries 
import java.awt.*; 
import javax.swing.*; 

//Object extends JFrame 
public class FrameObject extends JFrame 
{ 
    //declaring the panels, one for the color sliders and the other for the text fields 
    private JPanel color_panel; 
    private JPanel textFileds; 

    //arrays to hold the J components for further efficiency 
    private JSlider[] RGB = new JSlider[3]; 
    private JTextField[] RGBFileds = new JTextField[3]; 

    public FrameObject() 
    { 
     //Preparing the frame 
     super("Color panel"); 
     setVisible(true); 
     setSize(400, 400); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //A grid layout to give desired orientation 
     color_panel = new JPanel(new GridLayout(3, 1)); 
     textFileds = new JPanel(new GridLayout(3, 1)); 

     //initializing the individual components through a loop in the arrays 
     for(int c=0; c<RGB.length; c++) 
     { 
      RGB[c] = new JSlider(SwingConstants.HORIZONTAL,0,255,100); 
      RGBFileds[c] = new JTextField(12); 

      //Adding each component to its specific panel 
      color_panel.add(RGB[c]); 
      textFileds.add(RGBFileds[c]); 
     } 

     //adding the sub panels to the main panel. 
     add(color_panel,BorderLayout.CENTER); 
     add(textFileds,BorderLayout.EAST); 
    } 


} 

public class FrameTest 
{ 
    public static void main(String[] args) 
    { 
     FrameObject f = new FrameObject(); 
    } 

} 
+0

フレームの内容を入力した後でのみ、 'setVisible(true)'を呼び出します。実行時にコンポーネントを可視コンポーネントに追加する変更を行うには、再検証と再描画が必要です。 – kiheru

+0

ありがとう。 –

答えて

0

コンストラクタFrameObjectの終わりには、以下の行を追加する必要があります。

this.pack(); 

packメソッドは、すべての内容が望ましいサイズまたはそれ以上になるようにフレームのサイズを決めます。

0

フレームを梱包する必要があります。

//importing necessary libraries 
import java.awt.*; 
import javax.swing.*; 

//Object extends JFrame 
public class FrameObject extends JFrame 
{ 
    //declaring the panels, one for the color sliders and the other for the text fields 
    private JPanel color_panel; 
    private JPanel textFileds; 

    //arrays to hold the J components for further efficiency 
    private JSlider[] RGB = new JSlider[3]; 
    private JTextField[] RGBFileds = new JTextField[3]; 

    public FrameObject() 
    { 
     //Preparing the frame 
     super("Color panel"); 
     setVisible(true); 
     setSize(400, 400); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //A grid layout to give desired orientation 
     color_panel = new JPanel(new GridLayout(3, 1)); 
     textFileds = new JPanel(new GridLayout(3, 1)); 

     //initializing the individual components through a loop in the arrays 
     for(int c=0; c<RGB.length; c++) 
     { 
      RGB[c] = new JSlider(SwingConstants.HORIZONTAL,0,255,100); 
      RGBFileds[c] = new JTextField(12); 

      //Adding each component to its specific panel 
      color_panel.add(RGB[c]); 
      textFileds.add(RGBFileds[c]); 
     } 

     //adding the sub panels to the main panel. 
     add(color_panel,BorderLayout.CENTER); 
     add(textFileds,BorderLayout.EAST); 
     pack(); 
    } 

    public static void main(String[] args) 
    { 
     FrameObject f = new FrameObject(); 
    } 
} 
0

それはUIを更新するために来るときスイングは怠け者である

public FrameObject() { 
    //Preparing the frame 
    super("Color panel"); 
    //setVisible(true); 
    //setSize(400, 400); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //... 

    //adding the sub panels to the main panel. 
    add(color_panel, BorderLayout.CENTER); 
    add(textFileds, BorderLayout.EAST); 

    pack(); 
    setVisible(true); 
} 

UI

setVisible(true);を取り、それはあなたを確立した後、あなたが呼び出す最後のものにし、それはあなたが数を追加および削除することができますUIを更新したり、新しいレイアウト・パスを実行したりすることなく、「バッチ」内のコンポーネントを使用することができます。

あなたは、動的にUIを更新するには、UIはまた

を更新したいときrevalidaterepaint続い呼び出すことを覚えて、packpackとしてsetSize上を好むする必要がある場合は、アカウントに、フレームの装飾との違いがかかりますプラットフォームやシステム間で変更可能なフォントメトリックなど他のもの

関連する問題