2017-10-31 4 views
0

このプログラムを実行すると、フルスクリーン表示されるまで空のウィンドウとして表示され、好きなようにサイズを変更することができます。Javaの基本GUIブランク

プログラムは非常に基本的なメニューバーであり、2つのパネルが分割されています。

public class SplitPane { 


    public static void main(String[] args) { 
     window view = new window(); 
    } 

    private static class window extends JFrame { 



     public window() { 
      this.setSize(1000, 750); 
      this.setVisible(true); 
      this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

     //menubar is here, must lower code quantity for stack 

     //panels 
      //graph half 
      JPanel graphRep = new JPanel(); 
      //Background colour - graphRep.setBackground(Color.RED); 
      graphRep.setVisible(true); 
      String graphTitle = "Textual Representation."; 
      Border graphBorder = BorderFactory.createTitledBorder(graphTitle); 
      graphRep.setBorder(graphBorder); 
      //text half 
      JPanel textRep = new JPanel(); 
      textRep.setVisible(true); 
      String textTitle = "Graphical Representation."; 
      Border textBorder = BorderFactory.createTitledBorder(textTitle); 
      textRep.setBorder(textBorder); 

      //splitpane 
      JSplitPane splitPane = new JSplitPane(); 
      splitPane.setSize(600, 750); 
      splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT); 
      splitPane.setOneTouchExpandable(true); 
      splitPane.setDividerSize(10); 
      splitPane.setDividerLocation(250); 
      splitPane.setLeftComponent(graphRep); 
      splitPane.setRightComponent(textRep); 

      this.add(splitPane); 
     } 
    } 

答えて

2
this.setVisible(true); 

あなたはフレームにコンポーネントを追加する前にフレームを可視化しています。レイアウトマネージャは呼び出されないので、すべてのコンポーネントのサイズが(0、0)のままであるため、ペイントするものはありません。

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

そして、コードは次のようになります。

frame.pack(); 
frame.setVisible(); 

ので、各コンポーネントは、その適切なサイズで表示されます。ユーザー画面のサイズがわからないため、size()をハードコードしないでください。

+0

ニース、ありがとう、男! – JHurst