제 동생이 자신이 플레이하는 게임에서 세금을 계산하는 간단한 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
이며 결과를 보유하고 있습니다. Mac에서나 Windows에서 작동하지 않는 특별한 이유가 있습니까? 모든 입력을 부탁드립니다.
[이벤트 발송 스레드] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)에서 _only_ 스윙 GUI 객체를 생성하고 조작하십시오. – trashgod
@trashgod 링크를 보았지만 이해가되지 않았습니다. 예를 들어 주시겠습니까? – tomSurge
'main()'은 [example] (http://stackoverflow.com/search?tab=votes&q=user%3a230513%20EventQueue.invokeLater)에 대해'EventQueue.invokeLater()'를 호출해야합니다. – trashgod