When you click on JButton that has caption of Button1 - read the contents of JTextField and change the caption of other JButton (Button2) to the string value from JTextfield.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ButtonPanel extends JPanel
implements ActionListener
{
private JButton JButton2;
private JButton JButton1;
private JTextField textArea;
public ButtonPanel()
{
JButton1 = new JButton("JButton1");
JButton2 = new JButton("JButton2");
String a = "Come on everybody, clap your hands!"; //random text
setLayout (new FlowLayout(FlowLayout.LEADING));
add(JButton2);
add(JButton1);
JButton1.addActionListener(this);
JButton2.addActionListener(this);
textArea = new JTextField(a, 40);
}
public void actionPerformed(ActionEvent evt)
{
Object source = evt.getSource();
if (source == JButton1)
{
//
}
else if (source == JButton2)
{
//
}
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(500, 400);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane();
contentPane.add(new ButtonPanel());
}
}
public class HelloWorldFrame
{ public static void main(String[] args)
{
JFrame frame = new ButtonFrame();
frame.show();
}
}

New Topic/Question
Reply



MultiQuote







|