2016-05-31 3 views
0

Javaを使用して電卓アプリケーションを作成しようとしています。今のところ私はすべてのボタンとパネルをクリックして数字を表示するウィンドウを作成しています。 何とかフレームに追加したパネルが表示されません。誰でも助けてくれますか?私がやっている間違いは何ですか?パネルがJavaフレームで表示されない

package com.wells.eda; 
import java.awt.*; 
public class Calc { 
    public static void main(String[] args) { 
    Calculator c = new Calculator(); 

    try{ 
     Frame fm= c.NewFrame("Calculator",330,310); 
     c.NewButton(fm, "1",10,110); 
     c.NewButton(fm, "2",90,110); 
     c.NewButton(fm, "3",170,110); 
     c.NewButton(fm, "4",10,160); 
     c.NewButton(fm, "5",90,160); 
     c.NewButton(fm, "6",170,160); 
     c.NewButton(fm, "7",10,210); 
     c.NewButton(fm, "8",90,210); 
     c.NewButton(fm, "9",170,210); 
     c.NewButton(fm, "00",10,260); 
     c.NewButton(fm, "0",90,260); 
     c.NewButton(fm, "Reserved",170,260); 
     c.NewButton(fm, "+",250,110); 
     c.NewButton(fm, "-",250,160); 
     c.NewButton(fm, "*",250,210); 
     c.NewButton(fm, "/",250,260); 
     c.NewPanel(fm); 
     //Thread.sleep(3000); 
     //fm.dispose(); 

     } 
    catch(Exception e) 
     { 
     System.out.println("Exception happened"); 
     } 
    } 

} 
class Calculator{ 

    public Frame NewFrame(String name,int length, int width) 
    { 
     Frame fm = new Frame(name); 
     fm.setLayout(null); 
     fm.setResizable(false); 
     fm.setSize(length,width); 
     fm.setVisible(true); 
     return(fm); 
    } 
    public void NewButton(Frame fm,String number,int xcordinate,int ycordinate) throws Exception 
    { 
     Button b = new Button (number); 
     b.setBounds(xcordinate,ycordinate,70,40); 
     //b.setVisible(true); 
     fm.add(b);   
    } 
    public void NewPanel(Frame fm) 
    { 
     Panel p= new Panel(null); 
     fm.add(p); 
     //p.setSize(100, 100); 
     p.setBounds(10,10,250,100); 
     p.setName("Panel"); 
     p.setVisible(true); 
    } 

} 

答えて

1

あなたのパネルは空で、表示可能な特定のレイアウトを設定していないため表示されません。

例:fm.setLayout(null); ==> fm.setLayout(new BorderLayout());

public void NewPanel(Frame fm) 
{ 
    Panel p= new Panel(null); 
    fm.add(p); 
    //p.setSize(100, 100); 
    p.setBounds(10,10,250,100); 
    p.setName("Panel"); 
    p.setVisible(true); 
} 

これを試してみてください。

public void NewPanel(Frame fm) 
{ 
    Panel p= new Panel(null); 
    fm.add(p); 
    //p.setSize(100, 100); 
    p.setBounds(10,10,250,100); 
    p.setName("Panel"); 
    p.add(new JLabel("Test")); 
    p.setVisible(true); 
} 

JLabelのは

+0

私はレイアウトすることなく、既存のフレームにこれを追加することはできません作成arroundのあなたのパネルが表示されますか?私はまだレイアウトを知らない。 –

+0

レイアウトは、コンポーネントの配置方法を示すために使用されます。 GridLayoutやFlowLayoutなどを見ると、毎回それを使用する必要があります。これはあまり難しくありません。 – MedAl

+0

私の悪い。座標を使ってコンポーネントを配置するためにnullレイアウトを設定することを理解しました。それでは唯一の問題はパネルが空であることです。パネルにではなく、フレームにボタンを追加しています – MedAl

関連する問題