私の兄は私に、自分がプレイしているゲームで税金を計算する簡単なGUIアプリケーションを作成するようにリクエストしました。私はすぐにこのコードを組み立てました。私はちょうどそれが迅速に作業したかったように私は文字通り、5分を使用:MacではJTextFieldが表示されますが、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;
}
}
をJavaクラスMainApplication.javaに私はインスタンス化JFrame
:テキストフィールドのすべてを除いて表示
public class MainApplication {
public static void main(String args[]){
new MainGUI();
}
}
resultField
JTextField
であり、これは結果を保持するものである。 Windows上ではなくMac上で動作する特別な理由はありますか?すべての入力をいただければ幸いです。
[イベントディスパッチスレッド](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)でSwing GUIオブジェクト_only_を作成し、操作します。 – trashgod
@trashgod私はリンクを見たが、私はかなり理解していなかった。あなたは私に例を教えていただけますか? – tomSurge
'main()'は[example](http://stackoverflow.com/search?tab=votes&q=user%3a230513%20EventQueue.invokeLater)のために 'EventQueue.invokeLater()'を呼び出す必要があります。 – trashgod