2016-04-24 26 views
-1

私はGUIを含むこのアクティビティを持っています。このアクティビティには、長さと幅、xとy(キャンバス内のシェイプの位置)、およびシェイプ、長方形または円を選択するボタンの入力が含まれます。ボタンをクリックした後、テキストフィールド内の長さ、幅、x、yを受け入れます。Java - JFrame、JPanel、コンポーネントは表示されません

手動でGUIコードを操作すると、JPanelsとJFramesはサイズを設定した後も表示されません。純粋な空白です。

私はキャンバスを左に、ラベル、テキストフィールド、ボタンを右に置くことを期待していました。私は間違って何をしていますか?私はまだプログラムの初期段階にあります。ありがとうございました。

public class Problem02{ 
    Problem02(){ 
     JFrame framer = new JFrame("Problem 02"); 
     framer.setLayout(new FlowLayout()); 
     framer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel LPanel = new JPanel(); 
     JPanel RPanel = new JPanel(); 
     Canvas canvasses = new Canvas();Label XLabel = new Label("X"); 
     JLabel YLabel = new JLabel("Y"); 
     JLabel WidthLabel = new JLabel("Width"); 
     JLabel LengthLabel = new JLabel("Length"); 
     JButton RectangleButton = new JButton("Rectangle"); 
     JButton CircleButton = new JButton("Circle"); 
     JTextField XText = new JTextField(""); 
     JTextField YText = new JTextField(""); 
     JTextField WidthText = new JTextField(""); 
     JTextField LengthText = new JTextField(""); 

     framer.add(LPanel, BorderLayout.WEST); 
     framer.add(RPanel, BorderLayout.EAST); 
     LPanel.add(canvasses); 
     RPanel.add(XLabel); 
     RPanel.add(XText); 
     RPanel.add(YLabel); 
     RPanel.add(YText); 
     RPanel.add(WidthLabel); 
     RPanel.add(WidthText); 
     RPanel.add(LengthLabel); 
     RPanel.add(LengthText); 
     RPanel.add(RectangleButton); 
     RPanel.add(CircleButton); 

     framer.setSize(500,500); 
     framer.setTitle("Problem 02"); 
     framer.setVisible(true); 
    } 
    public static void main(String[] args) { 
    new Problem02(); 

    } 


    public void actionPerformed(ActionEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    public void windowOpened(WindowEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    public void windowClosing(WindowEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    public void windowClosed(WindowEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    public void windowIconified(WindowEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    public void windowDeiconified(WindowEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    public void windowActivated(WindowEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    public void windowDeactivated(WindowEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

} 
+0

このようにメインメソッドにsetVisible(true)を設定します。新しいProblem02.setVisible(true); –

+0

また、JPanelをJFrameに追加して、追加されたコンポーネントを表示する必要があります。 use:framer.add(xPanel); –

+0

私はすでにframer.add(LPanel)しました。とframer.add(RPanel); – user3255372

答えて

1

あなたはJFrameのから延びるいるPLUSあなたはJFrameの変数(フレーマ)を使用している...あなたは変数にすべてのコンポーネントを追加していますが、framerを示していない、あなたはthisを示しています。

は、

framer.setSize(500,500); 
    framer.setTitle("Problem 02"); 
    framer.setVisible(true); 

プラスへの最後の行は、命名規則に固執、PLEASE変更のJFrameを拡張しないでください:変数のための小文字を、あなたもStackOverflowsテキストの強調表示パーサに;-)

+0

ありがとうございます!ところで、かなり新しいStackOverflowに、私はこのコメントのレイアウトについてここで尋ねることができますか?キャンバスを左に、ボタンを右に表示したいのですが、どのようにすればいいですか?先生が私たちに教えてくれた唯一のレイアウトはFlowLayoutでした。それはボタンを中心にしていて、キャンバスは見えませんでした。 – user3255372

+0

Theresたくさんの良いレイアウトのチュートリアルがあります。 FlowLayoutの代わりにBorderLayoutを使うことができ、 'framer.add(LPanel);行は' framer.add(LPanel、BorderLayout.WEST);に変更する必要があります。 framer.add(RPanel、BorderLayout.EAST); ' また、「CENTER」に設定することもできます。 – JayC667

+0

あなたはEastとWestのBorderLayoutについてあなたが言ったことを行い、framer.setLayout(new FlowLayout())を変更しました。 framer.setLayout(new BorderLayout());に追加します。キャンバスは灰色であるはずですか?キャンバスを画面のようにパネルの側面に合わせるにはどうすればいいですか?左パネル全体をキャンバスにしたい。 – user3255372

0

をだましますあなたはあなたのようにJframeからProblem02を拡張したままにすることができますが、オブジェクトフレームを使用する必要はなく、その代わりにそれを行うことができます
this.setLayout(new FlowLayout()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

これを忘れないでください。 this.getContentPane()。add(RPanel); this.getContentPane()。add(LPanel);

+0

私は、getContentPane()についてのGoogle検索しました。定義はあまりにも教科書です。それの目的は何ですか? – user3255372

+0

Jframeはバッグのようなもので、すべてのGUI要素をその上に置きます。このメソッドは、それを行う責任があります –

関連する問題