每次执行新计算时都更新JTextArea。

时间:2016-09-09 17:42:15

标签: java

我正在构建我的第一个图形用户界面,它是一个相当简单的计算器应用程序。我遇到的问题是当我尝试预先形成另一个计算时,它会继续为输出添加一个新的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;
}      

}

2 个答案:

答案 0 :(得分:0)

  

错误是每当你点击按钮创建一个新的   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,您的Panel就足以清理您的内容,然后执行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");
    }  
 }

答案 1 :(得分:0)

在Frame类中,在calculate按钮actionPerformed方法中,您正在创建新的JtextArea。这就是为什么每按一次按钮,它就会创建并添加一个JTextArea。

因此,您可以在类级别创建一次outPut JTextArea,并根据您的要求附加结果。