JTextField显示在Mac上,但不显示在Windows上

时间:2016-11-27 17:58:07

标签: java swing cross-platform jtextfield compatibility

我的兄弟要求我制作一个简单的GUI应用程序,在他正在玩的游戏中计算他的税。所以我很快就收集了这段代码。我确实用了5分钟,因为我只是想让它快速运作:

    public class MainGUI extends JFrame implements ActionListener  {

    private static final double EA_TAX = 0.05;

    private JButton btnProfit;
    private JTextField buyPrice;
    private JTextField sellPrice;
    private JTextField resultField;
    private JLabel buyLabel;
    private JLabel sellLabel;
    private static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance();
    JPanel container;

    public MainGUI(){
        this.setSize(400,400);
        container = new JPanel();
        btnProfit = new JButton("Calculate");
        buyPrice = new JFormattedTextField(getIntFormatter(NUMBER_FORMAT));
        sellPrice = new JFormattedTextField(getIntFormatter(NUMBER_FORMAT));
        resultField = new JTextField();
        buyLabel = new JLabel("The price you intend to pay");
        sellLabel = new JLabel("Price you intend to sell the player for");
        resultField.setEditable(false);
        btnProfit.addActionListener(this);
        GridLayout gridLayout = new GridLayout(3,2);
        container.setLayout(gridLayout);
        container.add(buyLabel);
        container.add(sellLabel);
        container.add(buyPrice);
        container.add(sellPrice);
        container.add(btnProfit);
        container.add(resultField);

        container.setVisible(true);
        this.add(container);

        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    private NumberFormatter getIntFormatter(NumberFormat NUMBER_FORMAT)    {
        NumberFormatter formatter = new NumberFormatter(NUMBER_FORMAT);
        formatter.setValueClass(Integer.class);
        formatter.setMinimum(0);
        formatter.setMaximum(Integer.MAX_VALUE);
        //formatter.setAllowsInvalid(false);
        formatter.setCommitsOnValidEdit(true);


        return formatter;
        }


    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == this.btnProfit){
            this.resultField.setText("" +determineProfitAfterTax(Integer.parseInt(buyPrice.getText().replace(",", "")), Integer.parseInt(sellPrice.getText().replace(",", ""))));
        }
    }

    private int determineProfitAfterTax(int buyPrice, int sellPrice){
        return (int) (sellPrice * (1.00 - EA_TAX)) - buyPrice;
    }
}

在Java类MainApplication.java中实例化JFrame

public class MainApplication {

    public static void main(String args[]){
        new MainGUI();
    }
}

除了resultField JTextField之外,所有文本字段都会显示,而1.0.7 r0.10是保存结果的字段。任何特殊原因这适用于Mac而不是Windows?所有的意见都表示赞赏。

2 个答案:

答案 0 :(得分:1)

我修正了一些错误。现在它可以在Windows中运行。

  import javax.swing.*;

  import javax.swing.text.NumberFormatter;
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.text.NumberFormat;

  public class MainGUI extends JFrame implements ActionListener {

private static final double EA_TAX = 0.05;

private JButton btnProfit;
private JTextField buyPrice;
private JTextField sellPrice;
private JTextField resultField;
private JLabel buyLabel;
private JLabel sellLabel;
private static final NumberFormat NUMBER_FORMAT =     NumberFormat.getInstance();
JPanel container;

public MainGUI(){
    this.setSize(400,400);
    container = new JPanel();
    btnProfit = new JButton("Calculate");
    buyPrice = new JFormattedTextField(getIntFormatter(NUMBER_FORMAT));
    sellPrice = new JFormattedTextField(getIntFormatter(NUMBER_FORMAT));
    resultField = new JTextField();
    buyLabel = new JLabel("The price you intend to pay");
    sellLabel = new JLabel("Price you intend to sell the player for");
    resultField.setEditable(false);
    btnProfit.addActionListener(this);
    GridLayout gridLayout = new GridLayout(3,2);
    container.setLayout(gridLayout);
    container.add(buyLabel);
    container.add(sellLabel);
    container.add(buyPrice);
    container.add(sellPrice);
    container.add(btnProfit);
    container.add(resultField);

    container.setVisible(true);
    this.add(container);

    this.pack();
    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

private NumberFormatter getIntFormatter(NumberFormat NUMBER_FORMAT)    {
    NumberFormatter formatter = new NumberFormatter(NUMBER_FORMAT);
    formatter.setValueClass(Integer.class);
    formatter.setMinimum(0);
    formatter.setMaximum(Integer.MAX_VALUE);
    //formatter.setAllowsInvalid(false);
    formatter.setCommitsOnValidEdit(true);


    return formatter;
}



public void actionPerformed(ActionEvent e) {
    if(e.getSource() == this.btnProfit){
        this.resultField.setText("" +determineProfitAfterTax(Integer.parseInt(buyPrice.getText().replace(",", "")), Integer.parseInt(sellPrice.getText().replace(",", ""))));
    }
}

private int determineProfitAfterTax(int buyPrice, int sellPrice){
    return (int) (sellPrice * (1.00 - EA_TAX)) - buyPrice;
}
}`

答案 1 :(得分:0)

通过在事件调度线程上构建GUI Swing GUI选项解决了这个问题(用户@trashgod强烈建议。要澄清,而不是这样做:

public class MainApplication {

    public static void main(String args[]){
        new MainGUI();
    }
}

这样做:

public class MainApplication {

    public static void main(String args[]){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MainGUI();
            }
        });
    }
}

请始终尝试使用第二种方法,即使在使用第一种方法时您的程序似乎有效。使用第一种方法会产生非常奇怪的副作用。