JTextField在Mac上显示,但不在Windows上
问题描述:
我的兄弟要求我制作一个简单的GUI应用程序,用于计算他在玩的游戏中的税收。所以我很快汇编了这个代码。我简直用了5分钟,我只是想让它迅速开展工作:在Java类MainApplication.javaJTextField在Mac上显示,但不在Windows上
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;
}
}
我实例化JFrame
:
public class MainApplication {
public static void main(String args[]){
new MainGUI();
}
}
所有文本字段的显示出来,除了resultField
JTextField
,这是持有结果的人。任何特定的原因,这在Mac上,而不是在Windows上工作?所有的输入是赞赏。
答
问题通过构建事件调度线程(由用户@trashgod强烈建议在GUI上Swing GUI的选项解决工作,澄清,而不是做这样的:
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();
}
});
}
}
请始终尝试使用第二种方法,即使它可能看起来,你的程序使用第一种方式时,工作使用第一种方法。产生非常奇怪的副作用。
答
我修正了一些错误。现在,它在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;
}
}`
仅在[事件派发线程](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)上构建和操作Swing GUI对象_only_。 – trashgod
@trashgod我看到了链接,但我不太明白。你能举个例子吗? – tomSurge
你的'main()'应该调用'EventQueue.invokeLater()',用于[示例](http://stackoverflow.com/search?tab=votes&q=user%3a230513%20EventQueue.invokeLater)。 – trashgod