Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help me .

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me .

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;


    public class ButtonGUI extends JFrame implements ActionListener{
    private JButton button1;
    private JButton button2;

    public ButtonGUI(String title){
    buildGUI();
    setTitle(title);
    pack();
    setVisible(true);
    }

    public void buildGUI(){
    Container contentPane = getContentPane();
    contentPane.setLayout(new BorderLayout());
    button1 = new JButton("Left");
    button1.addActionListener(this);
    button2 = new JButton("Right!");
    button2.addActionListener(this);
    JPanel inputPanel = new JPanel();
    inputPanel.add(button1);
    inputPanel.add(button2);
    contentPane.add(inputPanel);
    }

    public void actionPerformed(ActionEvent e){
    String bText=button1.getText();
    button1.setText(button2.getText());
    button2.setText(bText);
    }

    public static void main(String args[])
    {
    new ButtonGUI("Button Game");
    }

    }


    How I make it when clicks a button , display its label in the JTextField . A JButton()'s labal can be gotten with the getText() method ?
    Help please .

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me .

    When posting code, please use the highlight tags. Also please use a more descriptive title, otherwise people are just going to ignore your post.

    As always, the API is your best friend: Java Platform SE 7
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Location
    Windsor, Ontario
    Posts
    9
    My Mood
    Bored
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help me .

    First you will need to create a JTextField

    private JTextField text1;

    You need to create a JTextField and add it to the screen

    JTextField text1= new JTextField(15);
    inputPanel.add(text1);

    In your action listener you need to create a string that will store the getText and then you use setText to put it on the JTextField.

    if(e.getSource() == button1){
    			String buttonText = button1.getText( );
    			text1.setText(buttonText);
    		}
    Hopefully that can help you. Also try and use tags other wise people will not respond or nag.