简体   繁体   English

Java actionPerformed在GUI调整大小之前不会添加更改的标签

[英]Java actionPerformed does not add the changed label until the GUI is resized

I am a high school student trying to learn java and any help or advice is appreciated. 我是一名试图学习Java的高中生,因此对您的帮助或建议深表感谢。 The code does not set the text after I press the button. 按下按钮后,代码未设置文本。 The actionPerformed does not do what it is supposed to until I change the size of the GUI for some reason. 在我出于某种原因更改了GUI的大小之前,actionPerformed不会执行应有的操作。 When I run it and press the button a . 当我运行它并按按钮a时。 pops up but nothing else, if you were to run it then you would see what I am talking about. 弹出但没有其他内容,如果要运行它,则您会看到我在说什么。 I think it stops during the action performer for some reason but I am really just guessing. 我认为由于某些原因,它会在动作执行者中停止,但是我只是在猜测。 Thank you. 谢谢。

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);

    }
    }

First, you're mixing heavy weight components with light weight components, this is going to cause you no end of issues and is best avoided. 首先,您要混合重组分和轻组分,这不会给您带来麻烦,最好避免。

Instead of: 代替:

  • Label use JLabel Label使用JLabel
  • TextField use JTextField TextField使用JTextField
  • Button use JButton Button使用JButton

Your primary issue is the fact that your container isn't been updated when the text is changed, this is a limitation of java.awt.Label and simply changing it to javax.swing.JLabel will automatically cause the container to be revalidated once the text of the label has been changed. 您的主要问题是,更改文本后不会更新容器,这是java.awt.Label的局限性,只需将其更改为javax.swing.JLabel即可在容器中自动验证容器的有效性。标签的文本已更改。

See... 看到...

for more details 更多细节

The lblResult label is initialized with "". lblResult标签用“”初始化。 When you set the value (a large text) there is no room for it inside the flow layout. 设置值(大文本)时,流布局内没有空间。

I think you must set a more elaborated layout or a minimum width for the result label: 我认为您必须为结果标签设置更精细的布局或最小宽度:

Label width: 标签宽度:

  setSize(400, 400);
  lblResult.setPreferredSize(new Dimension(200, 20));

Example Layout: 示例布局:

    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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM