2014-11-14 17 views
-1

標題說明它。我想要發生的事情是,當我點擊'指甲'按鈕時,文本框上會顯示'5'的輸出,當我點擊'鋼材'按鈕時,將會出現5 + 40(即45)的輸出文本區域,當我再次擊中「指甲」時,它會是50等等。Java Swing使用按鈕輸出添加數字在文本框中

到目前爲止我的代碼

**UPDATE: I GOT IT******************* 

private int price; 
private int setPrice; 

*othercodes here* 

private void setPrice(int price) { 
    setPrice = setPrice + price; 
    textField.setText(Integer.toString(setPrice)); 
} 

btnNails.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      setPrice(5); 
     } 
************************************* 

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JTextField; 
import javax.swing.JTable; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 


public class Frame52 extends JFrame { 

    private JPanel contentPane; 
    private JTextField textField; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        Frame52 frame = new Frame52(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public Frame52() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null);    
     JButton btnNails = new JButton("Nails"); 
     btnNails.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       int getPrice = 0; 
       getPrice = getPrice + 5; 
      } 
     }); 
     btnNails.setBounds(43, 70, 114, 23); 
     contentPane.add(btnNails);   
     JButton btnWood = new JButton("Wood"); 
     btnWood.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       int getPrice = 0; 
       getPrice = getPrice + 20; 
      } 
     }); 
     btnWood.setBounds(43, 114, 114, 23); 
     contentPane.add(btnWood); 

     JButton btnNewButton = new JButton("Galvanized Iron"); 
     btnNewButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       int getPrice = 0; 
       getPrice = getPrice + 30; 
      } 
     }); 
     btnNewButton.setBounds(43, 159, 114, 23); 
     contentPane.add(btnNewButton);   
     JButton btnNewButton_1 = new JButton("Steel"); 
     btnNewButton_1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       int getPrice = 0; 
       getPrice = getPrice + 40; 
      } 
     }); 
     btnNewButton_1.setBounds(43, 203, 114, 23); 
     contentPane.add(btnNewButton_1);    
     JLabel lblTotalIs = new JLabel("Total is:"); 
     lblTotalIs.setBounds(203, 163, 46, 14); 
     contentPane.add(lblTotalIs);    
     textField = new JTextField(); 
     textField.setText(Integer.toString(getPrice)); 
     textField.setBounds(259, 160, 86, 20); 
     contentPane.add(textField); 
     textField.setColumns(10);   
     JButton btnConfirm = new JButton("Confirm"); 
     btnConfirm.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
      } 
     }); 
     btnConfirm.setBounds(257, 203, 91, 23); 
     contentPane.add(btnConfirm); 
    } 
} 
+0

Java GUI必須適用於不同的操作系統,屏幕尺寸,屏幕分辨率等。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 – 2014-11-14 13:22:06

+0

另請參閱['Adder'](http://stackoverflow.com/a/8703807/230513)。 – trashgod 2014-11-14 16:17:29

回答

1

每個按鈕監聽器裏,你應該調用一個新的private方法setPrice(),看起來像這樣:

private void setPrice(int price) { 
    getPrice = price; 
    textField.setText(Integer.toString(getPrice)); 
} 

如果你用它來設定的值price字段,文本字段將同時爲您更改。

您還需要int getPriceJTextField textField作爲實例字段,而不是局部變量。按照現狀,每當你想更新它時,你都會創建一個新的int getPrice,這是不對的:如果你將它移動到實例字段,你可以在你的ActionListeners內使用它。

+0

似乎無法寫入私人無效setPrice(詮釋價格),有一個錯誤 – user3736846 2014-11-14 13:29:00

+2

「有一個錯誤」是肯定的信息和有用的。讓我得到我的水晶球,並幫助你。 – markspace 2014-11-14 13:48:05

+0

我忘了刪除,即時通訊抱歉,但我想通了。我很抱歉 – user3736846 2014-11-14 14:02:02

0

你不使用你的實際價格在每ActionListener

public void actionPerformed(ActionEvent arg0) { 
    int getPrice = 0; 
    getPrice = getPrice + 40; 
} 

你在哪裏得到的實際價格的textfield了?

代替上述使用這樣的:

public void actionPerformed(ActionEvent arg0) { 
    textfield.setText(Integer.parseInt(textField.getText) + 40); 
} 

當然,你的方法有很多臃腫的代碼,這樣你就可以通過檢查ActionEvent參數,看看哪個按鈕是使用單一ActionListener,而不是匿名者的按下。

0

您可以嘗試每個按鈕做這樣的事情:

JButton btnNails = new JButton("Nails"); 
btnNails.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent arg0) { 
     incrementPrice(5); // change the integer insede the method 
    } 
}); 

,並定義下面的方法類(Frame52)內:

private synchronized void incrementPrice(int value) { 
     getPrice = getPrice + value; 
     textField.setText(Integer.toString(getPrice)); 
    } 

這個解決你的問題。此解決方案是線程安全的。

相關問題