2016-12-26 6 views
0

コードを実行すると、空のフレームのみが表示されます。 JButtonとJTextFieldを見るには、私が検索してそれを見る前にクリックしなければなりません。私はインターネット上のいたる所で検索しましたが、何も見つかりませんでした。また、可視性をtrueに設定し、JComponentsを追加しました。ここに私のコードは次のとおりです。スイングコンポーネントが表示されない

Frame Fenster = new Frame(); 

そして、この...あなたは親コンテナに子を追加した後

package me.JavaProgramm; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.text.NumberFormat; 
import java.text.ParseException; 
import java.util.Locale; 

public class Frame extends JFrame { 
    private JButton bChange; 
    private JTextField tvonEur; 
    private JTextField tzuOCur; //andere Währung (other Currency) 
    private JTextField tzuEur; 
    private JTextField tvonOCur; 
    private JComboBox cbCur; //Wärhung wählen 
    private String curName; 
    private double faktorUSD; 
    private double faktorGBP; 

    private static String[] comboCur = {"USD", "GBP"}; 

    public Frame() { 
     setLayout(null); 
     setVisible(true); 
     setSize(400, 400); 
     setTitle("Währungsrechner"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setAlwaysOnTop(true); 
     setResizable(false); 

     Font schrift = new Font("Serif", Font.PLAIN + Font.ITALIC, 30); 

     tvonEur = new JTextField("Euro"); 
     tvonEur.setSize(80, 25); 
     tvonEur.setLocation(20, 50); 
     tvonEur.requestFocusInWindow(); 
     tvonEur.selectAll(); 

     tzuEur = new JTextField("Euro"); 
     tzuEur.setSize(80, 25); 
     tzuEur.setLocation(20, 150); 
     tzuEur.requestFocusInWindow(); 
     tzuEur.selectAll(); 

     bChange = new JButton("Euro zu US-Dollar"); 
     bChange.setSize(120, 25); 
     bChange.setLocation(110, 50); 

     tzuOCur = new JTextField("US-Dollar"); 
     tzuOCur.setSize(80, 25); 
     tzuOCur.setLocation(240, 50); 
     tzuOCur.requestFocusInWindow(); 
     tzuOCur.selectAll(); 

     tvonOCur = new JTextField("US-Dollar"); 
     tvonOCur.setSize(80, 25); 
     tvonOCur.setLocation(240, 50); 
     tvonOCur.requestFocusInWindow(); 
     tvonOCur.selectAll(); 

     cbCur = new JComboBox(comboCur); 
     cbCur.setSize(100, 20); 
     cbCur.setLocation(100, 100); 

     tvonEur.setVisible(true); 
     tzuEur.setVisible(true); 
     tzuOCur.setVisible(true); 
     tvonOCur.setVisible(true); 
     bChange.setVisible(true); 
     cbCur.setVisible(true); 

     add(tvonEur); 
     add(bChange); 
     add(tzuOCur); 
     add(cbCur); 

     Currency currency = new Currency(); 

     String strUSD = currency.convertUSD(); 
     try { 
      NumberFormat formatUSD = NumberFormat.getInstance(Locale.GERMANY); 
      Number numberUSD = formatUSD.parse(strUSD); 
      faktorUSD = numberUSD.doubleValue(); 
      System.out.println(faktorUSD); 
     } catch (ParseException e) { 
      System.out.println(e); 
     } 

     String strGBP = currency.convertGBP(); 
     try { 
      NumberFormat formatGBP = NumberFormat.getInstance(Locale.GERMANY); 
      Number numberGBP = formatGBP.parse(strGBP); 
      faktorGBP = numberGBP.doubleValue(); 
      System.out.println(faktorGBP); 
     } catch (ParseException e) { 
      System.out.println(e); 
     } 

     cbCur.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       cbCur = (JComboBox) e.getSource(); 
       curName = (String) cbCur.getSelectedItem(); 
       if (curName == "USD") { 
        tzuOCur.setText("US-Dollar"); 
       } else if (curName == "GBP") { 
        tzuOCur.setText("British-Pound"); 
       } 
      } 
     }); 

     bChange.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (curName == "USD") { 
        try { 
         Double doubleEUR = Double.parseDouble(tvonEur.getText()); 
         Double doubleUSD = doubleEUR * faktorUSD; 
         tzuOCur.setText(Double.toString(roundScale3(doubleUSD))); 
        } catch (NumberFormatException nfe) { 
         System.out.println("Gebe einen richten Wert ein!"); 
        } 
       } else if (curName == "GBP") { 
        try { 
         Double doubleEUR = Double.parseDouble(tvonEur.getText()); 
         Double doubleGBP = doubleEUR * faktorGBP; 
         tzuOCur.setText(Double.toString(roundScale3(doubleGBP))); 
        } catch (NumberFormatException nfe) { 
         System.out.println("Gebe einen richten Wert ein!"); 
        } 
       } 
      } 
     }); 
    } 

    public static double roundScale3(double d) { 
     return Math.rint(d * 1000)/1000.; 
    } 
} 

答えて

0

setVisible(true)を移動してみてください。

一般スイングで、それはこのように、イベントディスパッチスレッドに見えるコンポーネントを更新するコードを置くことをお勧めと考えられています:

SwingUtilities.invokeLater(new Runnable() { 
    @Override 
    public void run() { 
    Frame.this.setVisible(true); 
    } 
}); 
+0

私は、コードに貼り付けする必要がどこに新たなんだ申し訳ありませんか? – Donny

+0

'add'文の後で私の仕事ができます。 –

+0

はい、動作しますが、コンポーネントはまだ表示されません – Donny

関連する問題