简体   繁体   中英

Swing Components invisible

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.;
    }
}

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);
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM