2016-11-27 22 views
1

私の兄は私に、自分がプレイしているゲームで税金を計算する簡単な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(); 
    } 
} 

resultFieldJTextFieldであり、これは結果を保持するものである。 Windows上ではなくMac上で動作する特別な理由はありますか?すべての入力をいただければ幸いです。

+0

[イベントディスパッチスレッド](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)でSwing GUIオブジェクト_only_を作成し、操作します。 – trashgod

+0

@trashgod私はリンクを見たが、私はかなり理解していなかった。あなたは私に例を教えていただけますか? – tomSurge

+1

'main()'は[example](http://stackoverflow.com/search?tab=votes&q=user%3a230513%20EventQueue.invokeLater)のために 'EventQueue.invokeLater()'を呼び出す必要があります。 – trashgod

答えて

0

問題が強く、ユーザー@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(); 
      } 
     }); 
    } 
} 

してください常に最初のアプローチを使用しているとき、あなたのプログラムが動作するように見える場合であっても最初のアプローチを使用して、試してみて、第二のアプローチを使用します。非常に奇妙な副作用を引き起こす。

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; 
} 
}` 
+0

あなたは全く変更を行いませんでした。あなたは何について話していますか?これらのインポートの変更だけを追加しました。また、あなたが何かを「固定」していると言うときは、固定したものを指定してください。鉱山をコピーしてIDEに貼り付け、ライブラリをインポートしてここに貼り付けます。投稿は混乱していて、最初に作成されたはずはありません。 – tomSurge

+0

@tomSurge:これは、正しく同期されたプログラム、つまり 'EventQueue.invokeLater()'がなくても、あるプラットフォームで正しく動作し、別のプラットフォームで正常に動作する方法の例です。 – trashgod

関連する問題