2016-12-20 38 views
0

私は、特定の形状の面積を計算するための小さなプログラムを作ろうとしています。 ユーザーは、テキストフィールドを使用して入力を行うことができます(図形の高さやものなど)。彼はボタンを押す必要があり、価格は印刷されるはずです。 しかしそれは現れません。JAVA JTextFieldが表示されません

コード:

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 




public class Rechner extends JFrame implements ActionListener{ 
private static JButton button1; 
private static JButton button2; 
private static JButton button3; 
private static JButton button4; 
private static JTextField numberField; 

private JPanel jpanel; 

public Rechner(String titel){ 

    super(titel); 

    jpanel = new JPanel(); 

    numberField = new JTextField(1500); 
    add(numberField, BorderLayout.CENTER); 

    button1 = new JButton("Rechteck"); 
    button1.setBounds(10, 10, 150, 30); 
    button1.addActionListener(this); 
    add(button1); 

    button2 = new JButton("Dreieck"); 
    button2.setBounds(170, 10, 150, 30); 
    button2.addActionListener(this); 
    add(button2); 

    button3 = new JButton("Trapez"); 
    button3.setBounds(330, 10, 150, 30); 
    button3.addActionListener(this); 
    add(button3); 

    button4 = new JButton("Parallelogramm"); 
    button4.setBounds(490, 10, 150, 30); 
    button4.addActionListener(this); 
    add(button4); 
    setResizable(false); 


} 


public static void main(String[] args) { 

    Rechner frame = new Rechner("Menu"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(660, 400); 
    frame.setLayout(null); 

    frame.setVisible(true); 
} 


public void actionPerformed(ActionEvent e) { 

    if(e.getSource() == button1){ 

     System.out.println("fff"); 

    } 
    String numberStr = numberField.getText(); 


} 

} 
+0

私は[尋ねる]と[ツアー]にリンクして帽子を得るつもりです。 –

+0

私は 'swing'を使っていませんが、このクラスは' ActionListener'を実装しているので、 'actionPerformed'はそのリスナークラスの一部であると仮定します。それは '@ Override'であってはなりませんか? –

+1

正確に表示されるものは何ですか?あなたの 'actionPerformed'は単に" fff "を標準出力に出力します(そしてすぐに破棄されるローカル変数に値を割り当てます)。あなたはそれが何を期待していますか? – RealSkeptic

答えて

2

JFrame(まあ、実際にはそのコンテンツ・ペインの)のデフォルトのレイアウトマネージャはBorderLayoutです。指定された制約なし

は、コンポーネントがそう

add(component); 

add(component, BorderLayout.CENTER); 

と同じとに追加最後のコンポーネントを交換します。この方法で追加された各構成要素である、BorderLayout.CENTERに追加されますセンター。

setBoundsは、レイアウトマネージャが存在し、決して使用しないJPanelを作成した場合は効果がありません。

最後に、あなたがこのガイドを見てしたいことがあります。A Visual Guide to Layout Managers

2

この行は、主な問題である:

add(numberField, BorderLayout.CENTER); 

は、TextField全体のスペースを埋めるために引き起こしています。次に、BorderLayout.CENTERでJFrameにコンポーネントを追加すると、JTextFieldが置き換えられます。この問題を解決するには、次の

super(titel); 
jpanel = new JPanel(); 
add(jpanel, BorderLayout.NORTH); //adding the jpanel 

button1 = new JButton("Rechteck"); 
jpanel.add(button1); 
button1.setBounds(10, 10, 150, 30); 
//adding the other buttons to the JPanel... 
//... 
//... 
button4.addActionListener(this); 
button3.addActionListener(this); 
button2.addActionListener(this); 
button1.addActionListener(this); 

numberField = new JTextField(1500); 
add(numberField);//this will cause it to fill the remaining space 
setResizable(false); 

説明:

ボタンがに行くべきJPanelあなたが作成した、とJPanelJFrameさんNORTHに行く必要があります。そのように彼らはカバーしないでくださいJFrame

関連する問題