Java actionPerformed 在调整 GUI 大小之前不会添加更改的标签
Java actionPerformed does not add the changed label until the GUI is resized
我是一名努力学习 java 的高中生,非常感谢任何帮助或建议。
按下按钮后代码没有设置文本。在我出于某种原因更改 GUI 的大小之前,actionPerformed 不会执行它应该执行的操作。当我 运行 它并按下按钮 a 。弹出但没有别的,如果你要 运行 它那么你会明白我在说什么。我认为它出于某种原因在动作表演者期间停止,但我真的只是在猜测。谢谢你。
import java.awt.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
public class GUI extends JFrame implements ActionListener{
public TextField mpgEnter;
public Label lblResult;
public Button btnEnter;
public TextField gasPriceEnter;
public TextField distanceEnter;
public double mpg;
public double distanceTravel;
double priceOfgas;
public GUI() {
setTitle("GUI Test");
setSize(280, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Label lbl2 = new Label("MPG ||");
add(lbl2);
mpgEnter = new TextField(5);
Label lbl3 = new Label("Price of gas ||");
add(lbl3);
gasPriceEnter = new TextField(5);
Label lbl4 = new Label("Distance");
add(lbl4);
distanceEnter = new TextField(5);
setLayout(new FlowLayout());
add(mpgEnter);
add(gasPriceEnter);
add(distanceEnter);
// add button
btnEnter = new Button("Enter");
btnEnter.addActionListener(this);
add(btnEnter);
//add label
//setResizable(false);
lblResult = new Label("");
add(lblResult);
setVisible(true);
}
public static double calculateGPM(double mpg, double gas,double distance){
double gasPerMile = gas/mpg;
double cost1 = gasPerMile * distance;
NumberFormat formatter = new DecimalFormat("#0.00");
double costFinal = Double.parseDouble(formatter.format(cost1));
return costFinal;
}
@Override
public void actionPerformed(ActionEvent e) {
//System.out.print(test.getText());
mpg = Double.parseDouble(mpgEnter.getText());
priceOfgas= Double.parseDouble(gasPriceEnter.getText());
distanceTravel = Double.parseDouble(distanceEnter.getText());
lblResult.setText("The price for your trip is: $" + calculateGPM(mpg, priceOfgas, distanceTravel));
add(lblResult);
}
}
lblResult 标签已用“”初始化。当您设置值(大文本)时,流布局内没有空间。
我认为您必须为结果标签设置更详细的布局或最小宽度:
标签宽度:
setSize(400, 400);
lblResult.setPreferredSize(new Dimension(200, 20));
示例布局:
final JPanel north = new JPanel(new FlowLayout());
north.add(mpgEnter);
north.add(gasPriceEnter);
north.add(distanceEnter);
// add button
btnEnter = new Button("Enter");
btnEnter.addActionListener(this);
north.add(btnEnter);
// add label
add(north, BorderLayout.NORTH);
// setResizable(false);
lblResult = new Label("");
lblResult.setPreferredSize(new Dimension(200, 20));
add(lblResult, BorderLayout.CENTER);
首先,您将重量级组件与重量级组件混合在一起,这会给您带来无穷无尽的问题,最好避免。
而不是:
Label
使用 JLabel
TextField
使用 JTextField
Button
使用 JButton
您的主要问题是当文本更改时您的容器没有更新,这是 java.awt.Label
的限制,只需将其更改为 javax.swing.JLabel
将自动导致容器更改标签文本后将重新生效。
见...
- Creating a GUI With JFC/Swing
- Laying Out Components Within a Container
- Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
了解更多详情
我是一名努力学习 java 的高中生,非常感谢任何帮助或建议。 按下按钮后代码没有设置文本。在我出于某种原因更改 GUI 的大小之前,actionPerformed 不会执行它应该执行的操作。当我 运行 它并按下按钮 a 。弹出但没有别的,如果你要 运行 它那么你会明白我在说什么。我认为它出于某种原因在动作表演者期间停止,但我真的只是在猜测。谢谢你。
import java.awt.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
public class GUI extends JFrame implements ActionListener{
public TextField mpgEnter;
public Label lblResult;
public Button btnEnter;
public TextField gasPriceEnter;
public TextField distanceEnter;
public double mpg;
public double distanceTravel;
double priceOfgas;
public GUI() {
setTitle("GUI Test");
setSize(280, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Label lbl2 = new Label("MPG ||");
add(lbl2);
mpgEnter = new TextField(5);
Label lbl3 = new Label("Price of gas ||");
add(lbl3);
gasPriceEnter = new TextField(5);
Label lbl4 = new Label("Distance");
add(lbl4);
distanceEnter = new TextField(5);
setLayout(new FlowLayout());
add(mpgEnter);
add(gasPriceEnter);
add(distanceEnter);
// add button
btnEnter = new Button("Enter");
btnEnter.addActionListener(this);
add(btnEnter);
//add label
//setResizable(false);
lblResult = new Label("");
add(lblResult);
setVisible(true);
}
public static double calculateGPM(double mpg, double gas,double distance){
double gasPerMile = gas/mpg;
double cost1 = gasPerMile * distance;
NumberFormat formatter = new DecimalFormat("#0.00");
double costFinal = Double.parseDouble(formatter.format(cost1));
return costFinal;
}
@Override
public void actionPerformed(ActionEvent e) {
//System.out.print(test.getText());
mpg = Double.parseDouble(mpgEnter.getText());
priceOfgas= Double.parseDouble(gasPriceEnter.getText());
distanceTravel = Double.parseDouble(distanceEnter.getText());
lblResult.setText("The price for your trip is: $" + calculateGPM(mpg, priceOfgas, distanceTravel));
add(lblResult);
}
}
lblResult 标签已用“”初始化。当您设置值(大文本)时,流布局内没有空间。
我认为您必须为结果标签设置更详细的布局或最小宽度:
标签宽度:
setSize(400, 400);
lblResult.setPreferredSize(new Dimension(200, 20));
示例布局:
final JPanel north = new JPanel(new FlowLayout());
north.add(mpgEnter);
north.add(gasPriceEnter);
north.add(distanceEnter);
// add button
btnEnter = new Button("Enter");
btnEnter.addActionListener(this);
north.add(btnEnter);
// add label
add(north, BorderLayout.NORTH);
// setResizable(false);
lblResult = new Label("");
lblResult.setPreferredSize(new Dimension(200, 20));
add(lblResult, BorderLayout.CENTER);
首先,您将重量级组件与重量级组件混合在一起,这会给您带来无穷无尽的问题,最好避免。
而不是:
Label
使用JLabel
TextField
使用JTextField
Button
使用JButton
您的主要问题是当文本更改时您的容器没有更新,这是 java.awt.Label
的限制,只需将其更改为 javax.swing.JLabel
将自动导致容器更改标签文本后将重新生效。
见...
- Creating a GUI With JFC/Swing
- Laying Out Components Within a Container
- Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
了解更多详情