2016-12-26 67 views
0

當我運行我的代碼時,只有空框架。要查看JButton和JTextFields,我必須搜索並在它們可見之前單擊它們。我在互聯網上到處搜索,但我什麼也沒找到。我還將可見性設置爲true,並添加了JComponents。這是我的代碼:Swing組件隱形

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)添加孩子父容器後。

一般與Swing它被認爲是很好的做法,把代碼更新有形成分在事件分派線程,就像這樣:

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

對不起,我新,我必須將它粘貼到代碼? – Donny

+0

'add'語句適用於我。 –

+0

是的,它的工作,但組件仍然是不可見的 – Donny