JTextField 出现在 Mac 但没有出现在 Windows
JTextField showing up on Mac but not on Windows
我哥哥要求我制作一个简单的 GUI 应用程序来计算他正在玩的游戏中的税收。所以我很快组装了这段代码。我真的用了 5 分钟,因为我只是想让它快速工作:
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 class MainApplication.java 我实例化 JFrame
:
public class MainApplication {
public static void main(String args[]){
new MainGUI();
}
}
除保存结果的 resultField
JTextField
外,所有文本字段都会显示。这适用于 Mac 而不是 windows 的任何特殊原因?感谢所有意见。
我修正了一些错误。现在它在 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;
}
}`
通过在事件调度线程上构建 GUI Swing GUI 选项解决了问题(用户@trashgod 强烈建议。澄清一下,而不是这样做:
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();
}
});
}
}
请始终尝试并使用第二种方法,即使您的程序在使用第一种方法时似乎可以运行。使用第一种方法会产生非常奇怪的副作用。
我哥哥要求我制作一个简单的 GUI 应用程序来计算他正在玩的游戏中的税收。所以我很快组装了这段代码。我真的用了 5 分钟,因为我只是想让它快速工作:
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 class MainApplication.java 我实例化 JFrame
:
public class MainApplication {
public static void main(String args[]){
new MainGUI();
}
}
除保存结果的 resultField
JTextField
外,所有文本字段都会显示。这适用于 Mac 而不是 windows 的任何特殊原因?感谢所有意见。
我修正了一些错误。现在它在 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;
}
}`
通过在事件调度线程上构建 GUI Swing GUI 选项解决了问题(用户@trashgod 强烈建议。澄清一下,而不是这样做:
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();
}
});
}
}
请始终尝试并使用第二种方法,即使您的程序在使用第一种方法时似乎可以运行。使用第一种方法会产生非常奇怪的副作用。