Я пытаюсь изменить программу калькулятора, которую мне дал наш учитель.Предполагается, что он состоит из основных программ и программ с графическим интерфейсом.Моя единственная проблема - как обрабатывать события.Как видите, я создал класс Numerics отдельно от основной программы.Я хочу, чтобы, когда я щелкаю число, оно выбрасывало значение из числовой программы в текстовое поле класса NorthPanel в основной программе.Но я не знаю как.Кто-нибудь может дать мне какие-либо идеи о том, как это сделать?
Вот часть основной программы
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...
}
}