Results 1 to 4 of 4
- 02-13-2013, 04:30 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 21
- Rep Power
- 0
Adding Text from button to text field.
Hi All,
This is my code so far.
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.Java 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"); } }
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.
- 02-13-2013, 04:34 PM #2
Re: Adding Text from button to text field.
theNorthLabel.setText(theNorthLabel.getText()+ "0");
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 02-13-2013, 05:01 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 21
- Rep Power
- 0
Re: Adding Text from button to text field.
Haha I can't believe i never tried that.
Thanks :)
- 02-13-2013, 06:38 PM #4
Similar Threads
-
How would you go about adding text and images to a button or label?
By goatjugsoup in forum New To JavaReplies: 5Last Post: 05-21-2012, 10:33 AM -
adding text to a text area dynamically
By tom2zip in forum AWT / SwingReplies: 3Last Post: 01-17-2012, 05:58 AM -
getting text from a text field
By tomandhisjones in forum New To JavaReplies: 9Last Post: 04-17-2011, 08:21 AM -
Regarding Text Field
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-05-2008, 11:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks