2016-04-29 3 views
1

学校のプロジェクト用に電卓を作ろうとしていて、JTextAreaが表示されません。私は窓が必要なので、電卓や出力に入力している数字を見ることができます。JTextAreaが電卓に表示されない

これは私のコードです:

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

public class Calculator extends JFrame 
{ 

JButton[] nums = new JButton [10]; 

JButton num1 = new JButton ("1"); 
JButton num2 = new JButton ("2"); 
JButton num3 = new JButton ("3"); 
JButton num4 = new JButton ("4"); 
JButton num5 = new JButton ("5"); 
JButton num6 = new JButton ("6"); 
JButton num7 = new JButton ("7"); 
JButton num8 = new JButton ("8"); 
JButton num9 = new JButton ("9"); 
JButton num0 = new JButton ("0"); 
JButton decimal = new JButton ("."); 
JButton clear = new JButton ("C"); 
JButton sqrt = new JButton ("\u221A"); 
JButton mod = new JButton ("%"); 
JButton dividebyone = new JButton ("1/x"); 
JButton factorn = new JButton ("!n"); 

public Calculator() 
{ 
    JFrame window = new JFrame ("Calculator"); 
    setSize (260, 325); 
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    setVisible (true); 

    JTextArea screen = new JTextArea (20, 20); 
    JScrollPane pls = new JScrollPane (screen); 
    screen.setBackground (Color.WHITE); 
    screen.setVisible (true); 
    getContentPane().add (pls); 

    getContentPane().add (stylize (num1)); 
    num1.setBounds (20, 75, 45, 45); 
    num1.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      //Execute when button is pressed 
      System.out.println ("1"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (num2)); 
    num2.setBounds (70, 75, 45, 45); 
    num2.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("2"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (num3)); 
    num3.setBounds (120, 75, 45, 45); 
    num3.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("3"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (num4)); 
    num4.setBounds (20, 125, 45, 45); 
    num4.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("4"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (num5)); 
    num5.setBounds (70, 125, 45, 45); 
    num5.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("5"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (num6)); 
    num6.setBounds (120, 125, 45, 45); 
    num6.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("6"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (num7)); 
    num7.setBounds (20, 175, 45, 45); 
    num7.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("7"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (num8)); 
    num8.setBounds (70, 175, 45, 45); 
    num8.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("8"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (num9)); 
    num9.setBounds (120, 175, 45, 45); 
    num9.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("9"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (num0)); 
    num0.setBounds (20, 225, 45, 45); 
    num0.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("0"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (decimal)); 
    decimal.setBounds (70, 225, 45, 45); 
    decimal.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("."); 
     } 
    } 
    ); 

    getContentPane().add (stylize (clear)); 
    clear.setBounds (120, 225, 45, 45); 
    clear.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("c"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (sqrt)); 
    sqrt.setBounds (170, 75, 51, 45); 
    sqrt.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("sqrt"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (mod)); 
    mod.setBounds (170, 125, 51, 45); 
    mod.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("mod"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (factorn)); 
    factorn.setBounds (170, 175, 51, 45); 
    factorn.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("!n"); 
     } 
    } 
    ); 

    getContentPane().add (stylize (dividebyone)); 
    dividebyone.setBounds (170, 225, 51, 45); 
    dividebyone.addActionListener (new ActionListener() 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      System.out.println ("1/x"); 
     } 
    } 
    ); 

} //end Calculator 


public static JButton stylize (JButton button) 
{ 
    button.setBackground (Color.WHITE); 
    return button; 
} //end stylize 


public static void main (String args[]) 
{ 
    new Calculator(); 
} //end main 
} //end Calculator class 
+0

@服用したコード与えられたテキスト領域を使用して、CTRL + Aを使用して、すべてのテキストを選択してください表示されるはずですし、コードが整形されるようにCTRL + SHIFT + Fを使用してフォーマットします。 – randominstanceOfLivingThing

+0

パネルにすべてのコンポーネントを追加し、そのパネルを 'setContentPane(Container yourNewPanel)'を使ってコンテンツとして設定することができます – iestync

+0

1)Java GUIは、異なるOS、画面サイズ、画面解像度などで動作する必要があります。異なるロケールで異なるPLAFを使用します。したがって、ピクセルの完全なレイアウトには役立ちません。代わりに、レイアウトマネージャや[それらの組み合わせ](http://stackoverflow.com/a/5630271/418556)と[空白](http://stackoverflow.com/a/17874718/)のレイアウトパディングとボーダーを使用してください。 418556)。 2)この[計算例](http://stackoverflow.com/a/7441804/418556)も参照してください。 'ScriptEngine'を使ってテキストフィールドの式を評価します。 –

答えて

0

はこれをやってみてください。

Public static void main(String arts[]) 
{ 
    Calculator c= new Calculator(); 
    c.setVisible(true); 
} 

は、コンストラクタからのsetVisible(true)を取り外します。 これはうまくいきます。
コンポーネントを追加する前にフレームが表示されているため、コンポーネントが表示されていません。このような

+0

レイアウトマネージャがないように見えるので、配置が絶対的なので、scrollPaneのサイズを設定してみてください。 'pls.setBounds(20、10、200、45);'もしあなたがチャンスを得るならば、レイアウトマネージャについてもちょっと読んでみる価値があるでしょう – iestync

0

何かがあなたがEclipseのようなIDEエン使用している場合は、開始とアビシェークのアドバイス

public class Calculator extends JFrame { 

    JButton[] nums = new JButton[10]; 

    JButton num1 = new JButton("1"); 
    JButton num2 = new JButton("2"); 
    JButton num3 = new JButton("3"); 
    JButton num4 = new JButton("4"); 
    JButton num5 = new JButton("5"); 
    JButton num6 = new JButton("6"); 
    JButton num7 = new JButton("7"); 
    JButton num8 = new JButton("8"); 
    JButton num9 = new JButton("9"); 
    JButton num0 = new JButton("0"); 
    JButton decimal = new JButton("."); 
    JButton clear = new JButton("C"); 
    JButton sqrt = new JButton("\u221A"); 
    JButton mod = new JButton("%"); 
    JButton dividebyone = new JButton("1/x"); 
    JButton factorn = new JButton("!n"); 

    public Calculator() { 
     setSize(260, 325); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     final JPanel content = new JPanel(); 
     content.setLayout(null); 

     JTextArea screen = new JTextArea(); 
     JScrollPane pls = new JScrollPane(screen); 
     pls.setBounds(20, 10, 200, 45); 
     screen.setBackground(Color.WHITE); 
     content.add(pls); 

     content.add(stylize(num1)); 
     num1.setBounds(20, 75, 45, 45); 
     num1.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       // Execute when button is pressed 
       System.out.println("1"); 
      } 
     }); 

     content.add(stylize(num2)); 
     num2.setBounds(70, 75, 45, 45); 
     num2.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("2"); 
      } 
     }); 

     content.add(stylize(num3)); 
     num3.setBounds(120, 75, 45, 45); 
     num3.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("3"); 
      } 
     }); 

     content.add(stylize(num4)); 
     num4.setBounds(20, 125, 45, 45); 
     num4.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("4"); 
      } 
     }); 

     content.add(stylize(num5)); 
     num5.setBounds(70, 125, 45, 45); 
     num5.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("5"); 
      } 
     }); 

     content.add(stylize(num6)); 
     num6.setBounds(120, 125, 45, 45); 
     num6.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("6"); 
      } 
     }); 

     content.add(stylize(num7)); 
     num7.setBounds(20, 175, 45, 45); 
     num7.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("7"); 
      } 
     }); 

     content.add(stylize(num8)); 
     num8.setBounds(70, 175, 45, 45); 
     num8.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("8"); 
      } 
     }); 

     content.add(stylize(num9)); 
     num9.setBounds(120, 175, 45, 45); 
     num9.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("9"); 
      } 
     }); 

     content.add(stylize(num0)); 
     num0.setBounds(20, 225, 45, 45); 
     num0.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("0"); 
      } 
     }); 

     content.add(stylize(decimal)); 
     decimal.setBounds(70, 225, 45, 45); 
     decimal.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("."); 
      } 
     }); 

     content.add(stylize(clear)); 
     clear.setBounds(120, 225, 45, 45); 
     clear.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("c"); 
      } 
     }); 

     content.add(stylize(sqrt)); 
     sqrt.setBounds(170, 75, 51, 45); 
     sqrt.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("sqrt"); 
      } 
     }); 

     content.add(stylize(mod)); 
     mod.setBounds(170, 125, 51, 45); 
     mod.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("mod"); 
      } 
     }); 

     content.add(stylize(factorn)); 
     factorn.setBounds(170, 175, 51, 45); 
     factorn.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("!n"); 
      } 
     }); 

     content.add(stylize(dividebyone)); 
     dividebyone.setBounds(170, 225, 51, 45); 
     dividebyone.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.out.println("1/x"); 
      } 
     }); 
     setContentPane(content); 
    } // end Calculator 

    public static JButton stylize(JButton button) { 
     button.setBackground(Color.WHITE); 
     return button; 
    } // end stylize 

    public static void main(String args[]) { 
     final Calculator calc = new Calculator(); 
     calc.setVisible(true); 
    } // end main 
} // end Calculator class 
+0

* "このようなものがテキスト領域を表示するはずです" * 'content.setLayoutそれは非常に壊れやすく、おそらく次のマシンを壊すでしょう。 –

+0

これはグリッドベースのレイアウトマネージャを使用する方が良いでしょう。マシンに応じて実際に破損することはありますか? – iestync

+0

*「マシンによっては本当に壊れますか?」と考えてみましょう。画面サイズ、画面解像度、JREのブランド(またはバージョン)、OS、PLAF、デフォルトのフォント..そういうわけではないかもしれませんが、プログラマーにはないものがたくさんあります。コントロールはほとんどありません。 –

関連する問題