0

When I run my code there is only the empty frame. To see the JButton and the JTextFields I have to search and click on them before they are visible. I searched everywhere on the Internet but I found nothing. I also set the visibility to true and added the JComponents. Here is my Code:

Frame Fenster = new Frame();

And this...

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.;
    }
}
nick1699
  • 9
  • 3

1 Answers1

0

Try moving setVisible(true) after you add the children to the parent container.

Generally with Swing it's considered good practice to put code that updates visible components in the event dispatching thread, like this:

SwingUtilities.invokeLater(new Runnable() {
  @Override
  public void run() {
    Frame.this.setVisible(true);
  }
});
Matt Morgan
  • 4,900
  • 4
  • 21
  • 30
  • Sorry i'm new where I have to paste it in the code? – nick1699 Dec 26 '16 at 14:39
  • I think everything what manipulates the GUI (also adding invisible components) should happen on the EDT as discussed [here](http://stackoverflow.com/a/7158505/7274990). – Calculator Dec 26 '16 at 14:56
  • Sorry i don't understand. What I have to do now? – nick1699 Dec 26 '16 at 17:35
  • @Donny which components are still invisible? I ask because there are some you've defined that you never add to the frame (`tzuEur` and `tvonOCur`). LMK and I can try to help you. – Matt Morgan Dec 26 '16 at 19:02
  • I'm sorry that i'm answering so early. All componetns are still invisible. tzuEur and TvonOCur are still unused. – nick1699 Dec 31 '16 at 12:23