2015-04-26 25 views
0

這個問題措辭不佳,但我會盡力解釋。我創建了一個自動售貨機,彈出一個窗口並要求用戶輸入金額。然後,他們移動到主機窗口,並顯示他們輸入的數量作爲他們擁有的金額。我有一個按鈕,「添加金錢」,應該爲當前的金額增加資金,但我不知道該怎麼做。如何清除舊窗口的內容並向其添加新信息?

例如,用戶輸入他們有2美元,然後點擊輸入鍵,它將他們帶到主機界面,表明他們有2美元。用戶點擊'添加錢'按鈕和類型3,表示他們還有3美元。這應該意味着他們有5美元,並且將在主界面上表示他們有5美元。

代碼爲錢輸入...

public void actionPerformed(ActionEvent arg0) { 
    double moneyInput; 
    String text = mInput.getText(); 
    moneyInput = Double.parseDouble (text); 

    VendingMachineInterface frame; 
    try { 
     frame = new VendingMachineInterface(vendingMachineName, moneyInput); 
     frame.setVisible(true); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
+1

發佈一個[MCVE](http://stackoverflow.com/help/mcve)(不要發佈所有的程序,創建一個新的小程序來演示問題,並且我們可以自己運行)。 – user1803551

+1

我個人無法嘗試查看場景在這裏。你能爲你的代碼段添加一個或兩個截圖嗎? – snickers10m

+1

你可以使用一個CardLayout,尤其是你想重新顯示以前的視圖 – MadProgrammer

回答

-1

我不知道這是否是完成任務的最好辦法,但我在一個類似的應用程序的類型使用它之前。

基本上,在JFrame內部,我有一個JPanel,它只存在於使用add()和remove()方法在其他面板之間切換。

我創建了一個ManagerPanel類,它有下面的方法:

public void switchPanel(JPanel removePanel, JPanel addPanel) { 
    this.remove(removePanel); 
    this.add(addPanel); 
    validate(); 
    repaint(); 
} 

要切換面板,我使用的動作事件內執行以下操作:

((ManagerPanel)this.getParent()).switchPanel(currentPanel.this, newPanel); 

就像我說的,有可能是一個更好的解決方案,但這很容易,爲我工作。

+0

使用['CardLayout'](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html),如[本答案]中所示(http://stackoverflow.com /一個/418556分之5786005)。 –

1

這是一種方法來做你想做的事情,但既然你沒有提供一個MCVE,這可能不是你想要的。

public class VendingMachine extends JFrame { 

    static int amount = 0; 

    VendingMachine() { 

     JButton add = new JButton("Add amount"); 
     JTextField moneyInput = new JTextField(8); 
     JLabel currentAmount = new JLabel("Current amount:"); 
     JLabel amountLabel = new JLabel(String.valueOf(amount)); 

     setLayout(new FlowLayout()); 
     add(currentAmount); 
     add(amountLabel); 
     add(moneyInput); 
     add(add); 

     add.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 

       String addAmountString = moneyInput.getText(); 
       int addAmount = 0; 
       try { 
        addAmount = Integer.parseInt(addAmountString); 
       } catch (NumberFormatException exp) { 
        System.out.println("Not a number, amount to add will be 0."); 
       } 
       amount += addAmount; 
       moneyInput.setText(""); 
       amountLabel.setText(String.valueOf(amount)); 
      } 
     }); 

     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     pack(); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 

     String initialString = JOptionPane.showInputDialog("Enter initial $"); 
     try { 
      amount = Integer.parseInt(initialString); 
     } catch (NumberFormatException e) { 
      System.out.println("Not a number, initial amount will be 0."); 
     } 
     new VendingMachine(); 
    } 
} 

注:

  • 您可以使用文本字段而不是輸入後,檢查值InputVerifier
  • 您可以使用每次按下按鈕時使用的輸入對話框,而不是在主窗口中顯示文本字段。
相關問題