SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
Hi,
I have created the buttons along in the GUI, but I need help adding the ActionListener interface to make the buttons respond. Any assistance would be greatly appreciated...
Code :
import javax.swing.*;
import javax.swing.JFrame;//page 298 Core Java
import javax.swing.JTextField;//page 379 Core Java
import javax.swing.JButton;//page 331 Core Java
import java.awt.Container;//page 331 Core Java
import java.awt.FlowLayout;//page 371 Core Java
class theButtons {
public static void main(String args[]) {
//declare/initialize variables
JFrame frame;
Container contentPane;
JTextField textfield;
JButton button;
FlowLayout layout;
String thePrincipal;
String interestRate;
String theTerm;
String pleaseCalculate;
String toRefresh;
JFrame theFrame = new JFrame("McBride Financial Services");
final int FRAME_WIDTH = 550;
final int FRAME_HEIGHT = 300;
theFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
textfield = new JTextField("Type your text here.");
contentPane = theFrame.getContentPane();
thePrincipal = "Principal Amount";
button = new JButton(thePrincipal);
contentPane.add(textfield);
contentPane.add(button);
layout = new FlowLayout();
contentPane.setLayout(layout);
interestRate = "Interest Rate";
button = new JButton(interestRate);
contentPane.add(textfield);
contentPane.add(button);
layout = new FlowLayout();
contentPane.setLayout(layout);
theTerm = "Loan Term";
button = new JButton(theTerm);
contentPane.add(textfield);
contentPane.add(button);
layout = new FlowLayout();
contentPane.setLayout(layout);
pleaseCalculate = "Calculate";
button = new JButton(pleaseCalculate);
contentPane.add(textfield);
contentPane.add(button);
layout = new FlowLayout();
contentPane.setLayout(layout);
toRefresh = "Refresh";
button = new JButton(toRefresh);
contentPane.add(textfield);
contentPane.add(button);
layout = new FlowLayout();
contentPane.setLayout(layout);
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theFrame.pack();
theFrame.setVisible(true);
}
}
Re: SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
hi beginning2understand,
welcome to the java programming forums.
below is the code: sorry because its not that great but hope it helps.
i put the action listener in the textfield. if you click the button, it prints the text of that button.
Code :
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
class Guib {
public static void main(String[] args) {
Container contentPane;
final JTextField textfield = new JTextField();
final JButton button1, button2, button3, button4, button5;
FlowLayout layout;
String thePrincipal;
String interestRate;
String theTerm;
String pleaseCalculate;
String toRefresh;
ActionListener a1 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
textfield.setText("Principal Amount");
}
};
ActionListener a2 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
textfield.setText("Interest Rate");
}
};
ActionListener a3 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
textfield.setText("Loan Term");
}
};
ActionListener a4 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
textfield.setText("Calculate");
}
};
ActionListener a5 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
textfield.setText("Refresh");
}
};
JFrame theFrame = new JFrame("McBride Financial Services");
final int FRAME_WIDTH = 550;
final int FRAME_HEIGHT = 300;
theFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
textfield.setText("Type your text here.");
contentPane = theFrame.getContentPane();
thePrincipal = "Principal Amount";
button1 = new JButton(thePrincipal);
contentPane.add(textfield);
contentPane.add(button1);
layout = new FlowLayout();
contentPane.setLayout(layout);
interestRate = "Interest Rate";
button2 = new JButton(interestRate);
contentPane.add(textfield);
contentPane.add(button2);
layout = new FlowLayout();
contentPane.setLayout(layout);
theTerm = "Loan Term";
button3 = new JButton(theTerm);
contentPane.add(textfield);
contentPane.add(button3);
layout = new FlowLayout();
contentPane.setLayout(layout);
pleaseCalculate = "Calculate";
button4 = new JButton(pleaseCalculate);
//button.addActionListener(button);
contentPane.add(textfield);
contentPane.add(button4);
layout = new FlowLayout();
contentPane.setLayout(layout);
toRefresh = "Refresh";
button5 = new JButton(toRefresh);
contentPane.add(textfield);
contentPane.add(button5);
layout = new FlowLayout();
contentPane.setLayout(layout);
//button5.addActionListener(a1);
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theFrame.pack();
theFrame.setVisible(true);
button1.addActionListener(a1);
theFrame.getContentPane().add(button1);
button2.addActionListener(a2);
theFrame.getContentPane().add(button2);
button3.addActionListener(a3);
theFrame.getContentPane().add(button3);
button4.addActionListener(a4);
theFrame.getContentPane().add(button4);
button5.addActionListener(a5);
theFrame.getContentPane().add(button5);
}
}
Cheers,
Truffy:cool:
Re: SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
thank you for your post Truffy... I apologize for taking so long in getting back to you... life has a way of doing that sometimes...
Re: SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
Quote:
Originally Posted by
beginning2Understand
thank you for your post Truffy... I apologize for taking so long in getting back to you... life has a way of doing that sometimes...
its okay? if it solved your problem pls marked as solved. thanks. :)
Cheers,
Truffy 8->
Re: SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
Re: SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
Quote:
Originally Posted by
beginning2Understand
will do...
great.
Cheers,
Truffy. :D