Adding Text from button to text field.
Hi All,
This is my code so far.
Code:
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NestedLayoutsLab extends JFrame implements ActionListener{
private JPanel masterPanel = new JPanel(new BorderLayout());
private JPanel minorPanel = new JPanel(new GridLayout(4, 2));
private JButton Clear = new JButton("CE");
private JButton Dot = new JButton(".");
private JButton Zero = new JButton("0");
private JButton One = new JButton("1");
private JButton Two = new JButton("2");
private JButton Three = new JButton("3");
private JButton Four = new JButton("4");
private JButton Five = new JButton("5");
private JButton Six = new JButton("6");
private JButton Seven = new JButton("7");
private JButton Eight = new JButton("8");
private JButton Nine = new JButton("9");
private JTextField theNorthLabel = new JTextField("No Input Entered");
public NestedLayoutsLab(String aStr){
super(aStr);
Clear.addActionListener(this);
Dot.addActionListener(this);
Zero.addActionListener(this);
One.addActionListener(this);
Two.addActionListener(this);
Three.addActionListener(this);
Four.addActionListener(this);
Five.addActionListener(this);
Six.addActionListener(this);
Seven.addActionListener(this);
Eight.addActionListener(this);
Nine.addActionListener(this);
masterPanel.add(theNorthLabel, BorderLayout.NORTH);
minorPanel.add(Nine);
minorPanel.add(Eight);
minorPanel.add(Seven);
minorPanel.add(Six);
minorPanel.add(Five);
minorPanel.add(Four);
minorPanel.add(Three);
minorPanel.add(Two);
minorPanel.add(One);
minorPanel.add(Zero);
minorPanel.add(Dot);
minorPanel.add(Clear);
theNorthLabel.setHorizontalAlignment(JLabel.CENTER);
masterPanel.add(minorPanel);
getContentPane().add(masterPanel);
setSize(350,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(Nine)){
theNorthLabel.setText("9");
}else if(e.getSource().equals(Eight)){
theNorthLabel.setText("8");
}else if(e.getSource().equals(Seven)){
theNorthLabel.setText("7");
}else if(e.getSource().equals(Six)){
theNorthLabel.setText("6");
}else if(e.getSource().equals(Five)){
theNorthLabel.setText("5");
}else if(e.getSource().equals(Four)){
theNorthLabel.setText("4");
}else if(e.getSource().equals(Three)){
theNorthLabel.setText("3");
}else if(e.getSource().equals(Two)){
theNorthLabel.setText("2");
}else if(e.getSource().equals(One)){
theNorthLabel.setText("1");
}else if(e.getSource().equals(Zero)){
theNorthLabel.setText("0");
}else if(e.getSource().equals(Dot)){
theNorthLabel.setText(".");
}else if(e.getSource().equals(Clear)){
theNorthLabel.setText("");
}
}
public static void main(String[] args){
new NestedLayoutsLab("Border Layout");
}
}
What I'm trying to so is when you click a button instead of setting the Field to = the button i want to add the digit to the field.
I'm still looking into this and if i figure it out i will post my solution but some help would be appropriated as it may help speed up things.
Thank you.
Re: Adding Text from button to text field.
theNorthLabel.setText(theNorthLabel.getText()+ "0");
Re: Adding Text from button to text field.
Haha I can't believe i never tried that.
Thanks :)
Re: Adding Text from button to text field.
Moved from Advanced Java. Not in any way an advanced question either.
db