Mein Bruder bat mich, eine einfache GUI-Anwendung zu erstellen, die seine Steuer in einem Spiel berechnet, das er gerade spielt. Also habe ich diesen Code schnell zusammengestellt. Früher habe ich buchstäblich 5 Minuten, wie ich es wollte nur schnell arbeiten:JTextField erscheint auf Mac, aber nicht auf 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;
}
}
in der Java-Klasse MainApplication.java i instanziiert die JFrame
:
public class MainApplication {
public static void main(String args[]){
new MainGUI();
}
}
alle Textfelder zeigen sich außer der resultField
JTextField
, das ist das Ergebnis. Aus irgendeinem Grund funktioniert das auf Mac und nicht auf Windows? Alle Eingaben werden geschätzt.
Konstruieren und bearbeiten Sie Swing-GUI-Objekte _only_ im [Ereignisversand-Thread] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod
@trashgod Ich sah den Link, aber ich habe es nicht ganz verstanden. Kannst du mir ein Beispiel geben? – tomSurge
Ihre 'main()' sollte 'EventQueue.invokeLater()' aufrufen, für [Beispiel] (http://stackoverflow.com/search?tab=votes&q=user%3a230513%20EventQueue.invokeLater). – trashgod