简体   繁体   English

摆动组件不可见

[英]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. 要查看JButton和JTextField,我必须先搜索并单击它们,然后才能看到它们。 I searched everywhere on the Internet but I found nothing. 我在Internet上到处搜索,但是什么也没找到。 I also set the visibility to true and added the JComponents. 我还将可见性设置为true并添加了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. 将子级添加到父容器后,尝试移动setVisible(true)

Generally with Swing it's considered good practice to put code that updates visible components in the event dispatching thread, like this: 通常,在Swing中,最好将代码更新可见的组件放在事件分派线程中,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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