gpt4 book ai didi

java - 每次执行新计算时更新 TextArea。

转载 作者:太空宇宙 更新时间:2023-11-04 12:15:52 25 4
gpt4 key购买 nike

我正在构建我的第一个图形用户界面,它是一个相当简单的计算器应用程序。我遇到的问题是,当我尝试执行另一个计算时,它会不断为输出添加新的 JTextArea。我尝试了 remove()revalidate()repaint(),但它仍然继续添加新的 JTextArea 框。我做错了什么?

 import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static java.lang.Thread.currentThread;
import java.util.concurrent.TimeUnit;

public class MovieCalculatorJFrame extends JFrame
{
private static final double PERCENT_TOTAL = 0.20;
private double grossChildTicketSold;
private double grossAdultTicketSold;
private double netChildTicketSold;
private double netAdultTicketSold;
private double grossRevenue;
private double netRevenue;
private MovieTextBoxPanel textBoxPanel;
private JPanel outPutPanel;
private JTextArea outPut;
private JButton calcButton;
private JPanel calcButtonPanel;
private final int WINDOW_WIDTH = 800;
private final int WINDOW_HEIGHT = 1500;

//Create constructor
public MovieCalculatorJFrame()
{
//Display title
setTitle("Profits Calculator");

//Set window size
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

//Create GridLayout
// 3 row with 1 columns
setLayout(new GridLayout(3,1));

//Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create custom Panels
textBoxPanel = new MovieTextBoxPanel();

//Add the components to the content pane
add(textBoxPanel);

//Build calculate button panel
buildCalculateButton();


//Allows time for splash to appear.
try
{
TimeUnit.MILLISECONDS.sleep(2000);
}
catch(Exception e)
{
System.out.println("Exception caught");
}

// Pack the contents of the window and display it
pack();
setVisible(true);
}

private void buildCalculateButton()
{
//Create a JPanel for the button
calcButtonPanel = new JPanel();

//Create Button and add it to the JPanel
calcButton = new JButton("Calculate");
calcButtonPanel.add(calcButton);

//add JPanel to content pane
add(calcButtonPanel);

//Register action listener
calcButton.addActionListener(new CalcButtonListener());
}

//inner class with action listener.
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//Check fields for errors
try{
grossChildTicketSold = (textBoxPanel.getChildCost() * textBoxPanel.getChildTotal());
grossAdultTicketSold = (textBoxPanel.getAdultCost() * textBoxPanel.getAdultTotal());
netChildTicketSold = (PERCENT_TOTAL * grossChildTicketSold);
netAdultTicketSold = (PERCENT_TOTAL * grossAdultTicketSold);
grossRevenue = (grossAdultTicketSold + grossChildTicketSold);
netRevenue = (PERCENT_TOTAL * grossRevenue);

}
catch(Exception a)
{
System.out.println("Fields are not completely filled out or letters may have been enter");
}


//Create new JPanel and JText and outputs result to JText.
JPanel newPanel = new JPanel();
if(outPut !=null)
{
newPanel.remove(outPut);
newPanel.revalidate();
newPanel.repaint();
}

outPut = new JTextArea("Gross revenue for adult tickets sold: " + grossAdultTicketSold + "\n");
outPut.append("Net revenue for adult tickets sold: " + netAdultTicketSold + "\n");
outPut.append("Gross revenue for child tickets sold: " + grossChildTicketSold + "\n");
outPut.append("Net revenue for child tickets sold: " + netChildTicketSold + "\n");
outPut.append("Gross revenue: " + grossRevenue + "\n");
outPut.append("Net revenue: " + netRevenue + "\n");

//Build output JPanel and add it to the content pane

newPanel.add(outPut);
add(newPanel);
setVisible(true);


}
}
public static void main(String[] args)
{
new MovieCalculatorJFrame();
}
}

这是我使用的自定义 JPanel,以防您想要一切。

 import javax.swing.*;
import java.awt.*;
public class MovieTextBoxPanel extends JPanel
{
private JLabel childTicketCostText;
private JTextField childTicketCostTextBox;
private JLabel AdultTicketCostText;
private JTextField AdultTicketCostTextBox;
private JLabel childTicketSoldText;
private JTextField childTicketSoldTextBox;
private JLabel AdultTicketSoldText;
private JTextField AdultTicketSoldTextBox;

private double childTicketCost;
private double childTicketTotal;
private double adultTicketCost;
private double adultTicketTotal;
private String input;

//Create constructor
public MovieTextBoxPanel()
{
//Create GridLayout
// 2 row with 2 columns
setLayout(new GridLayout(2,2));

//Create child price text message
childTicketCostText = new JLabel("Price per child's ticket");
//Create child price text box
childTicketCostTextBox = new JTextField(5);
//Create adult price text message
AdultTicketCostText = new JLabel("Price per adult's ticket");
//Create adult price text box
AdultTicketCostTextBox = new JTextField(5);
//Create child tickets sold text message
childTicketSoldText = new JLabel("Number of children's tickets sold");
//Create child tickets sold text box
childTicketSoldTextBox = new JTextField(5);
//Create adult tickets sold text message
AdultTicketSoldText = new JLabel("Number of adult's tickets sold");
//Create adult tickets sold text box
AdultTicketSoldTextBox = new JTextField(5);

//Create border
setBorder(BorderFactory.createLineBorder(Color.black));

//Add text boxes to MovieTheaterProfits Jpanel
add(childTicketCostText);
add(childTicketCostTextBox);
add(AdultTicketCostText);
add(AdultTicketCostTextBox);
add(childTicketSoldText);
add(childTicketSoldTextBox);
add(AdultTicketSoldText);
add(AdultTicketSoldTextBox);


}

public double getChildCost()
{
input = childTicketCostTextBox.getText();
childTicketCost = Double.parseDouble(input);
return childTicketCost;
}
public double getAdultCost()
{
input = AdultTicketCostTextBox.getText();
adultTicketCost = Double.parseDouble(input);
return adultTicketCost;
}
public double getChildTotal()
{
input = childTicketSoldTextBox.getText();
childTicketTotal = Double.parseDouble(input);
return childTicketTotal;
}
public double getAdultTotal()
{
input = AdultTicketSoldTextBox.getText();
adultTicketTotal = Double.parseDouble(input);
return adultTicketTotal;
}

}

最佳答案

the error is that whenever you click on the button create a new instance of JPanel(newPanel)

要解决该问题,建议尝试声明您的 JPanel 类级别

 private JPanel newPanel; // create variable of class level
private JTextArea outPut;
//Create constructor
public MovieCalculatorJFrame() //constructor
{
setTitle("Profits Calculator");
newPanel = new JPanel(); //instancia
outPut = new JTextArea();
....
// end to constructor
newPanel.add(outPut);
add(newPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
}

不需要删除面板中的 TextArea 就足以清理您的内容,然后执行 jTextArea.append("New Text");

你的CalcButtonListener可能看起来像

 private class CalcButtonListener implements ActionListener 
{
public void actionPerformed(ActionEvent e)
{
try{
grossChildTicketSold = (textBoxPanel.getChildCost() * textBoxPanel.getChildTotal());
grossAdultTicketSold = (textBoxPanel.getAdultCost() * textBoxPanel.getAdultTotal());
netChildTicketSold = (PERCENT_TOTAL * grossChildTicketSold);
netAdultTicketSold = (PERCENT_TOTAL * grossAdultTicketSold);
grossRevenue = (grossAdultTicketSold + grossChildTicketSold);
netRevenue = (PERCENT_TOTAL * grossRevenue);
}
catch(Exception a)
{
System.out.println("Fields are not completely filled out or letters may have been enter");
}
outPut.setText(""); //clear text to JTextArea
outPut.append("Gross revenue for adult tickets sold: " + grossAdultTicketSold + "\n");
outPut.append("Net revenue for adult tickets sold: " + netAdultTicketSold + "\n");
outPut.append("Gross revenue for child tickets sold: " + grossChildTicketSold + "\n");
outPut.append("Net revenue for child tickets sold: " + netChildTicketSold + "\n");
outPut.append("Gross revenue: " + grossRevenue + "\n");
outPut.append("Net revenue: " + netRevenue + "\n");
}
}

关于java - 每次执行新计算时更新 TextArea。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39417047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com