2011-03-21 69 views
0

我試圖修改我們的老師給我的計算器程序。它應該由主程序和GUI程序組成。我唯一的問題是如何處理事件。正如你可以看到 我除了主程序外還創建了Numerics類。我想要發生的事情是,當我單擊一個數字時,它應該將數值程序的值從主程序的NorthPanel類中的文本字段中移出。但我不知道如何。任何人都可以給我任何想法如何做到這一點?Java GUI:修改計算器程序

這裏是主程序

public class NorthPanel extends JPanel { 
    private JTextField display; 
    private JLabel filler; 

    public NorthPanel() { 
    //receive the thrown value from Numerics program to be displayed 
     setLayout(new BorderLayout()); 
     String calcTF="0."; 
     display = new JTextField(calcTF); 
     display.setEditable(false); 
     display.setFont(new Font("Century Gothic",Font.BOLD,19)); 
     display.setHorizontalAlignment(JTextField.RIGHT);         
     add(display,BorderLayout.CENTER);     
    } 
} 

public class CenterPanel extends JPanel { 

    private Numerics numeric; 
    private Operations operator; 
    private Functions function; 

    public CenterPanel() { 
     setLayout(null); 

     numeric = new Numerics(); 
     numeric.setBounds(5,5,150,150); 

     operator = new Operations(); 
     operator.setBounds(158,5,45,150); 

     function = new Functions(); 
     function.setBounds(204,5,55,150); 

     add(numeric);    
     add(operator); 
     add(function,0); 
    } 
} 

的一部分,這裏是Numerics的計劃的一部分

public class Numerics extends JPanel implements ActionListener 
{ 
    private JButton c7; 
    String value=""; 
    public Numerics() 
    { 
     UIManager.put("Button.background", Color.gray); 
     setLayout(new GridBagLayout()); 

     GridBagConstraints gbc=new GridBagConstraints(); 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.insets = new Insets(0,3,5,3); 

     c7=new JButton("7"); 
     c7.setFont(new Font("Arial",Font.BOLD,20)); 
     c7.setBorder(BorderFactory.createRaisedBevelBorder()); 
     gbc.gridx=0; 
     gbc.gridy=0; 
     add(c7,gbc); 
     c7.addActionListener(this); 
public void actionPerformed(ActionEvent ae1) 
    { 
     if(ae1.getSource()==c7) 
     { 
      value+="7"; 
      //throw the value to display in the NorthPanel... 
     } 
    } 
+0

1 SSCCE(http://pscode.org/sscce.html)比2個代碼片段更有效地獲得幫助。 ;) – 2011-03-21 04:45:21

回答