Hi everyone, hope all is well
I have a project which models an ATM machine. There needs to be an option in the welcome screen where the user has the option to set all of the numeric buttons in each class to a certain colour(red, blue or yellow).
Because all of the numeric buttons are not inherited, i do not know of a method which can set a specific set of buttons to a certain colour..
Below is my code of the welcome screen and the balance screen to show how they are linked:
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.lang.*;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
public class Welcome extends JFrame {
private Toolkit toolkit;
private static String passy = "123456";
JPasswordField password = new JPasswordField(6);
public Welcome()
{
setSize(1440,900);
setTitle("Welcome");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
JLabel label = new JLabel("Enter Pin Code:");
password.setEchoChar('*');
password.addActionListener(new AL());
label.setBounds(585,170,290,30);
password.setBounds(620,233,200,25);
Font font = new Font("Verdana", Font.BOLD, 32);
label.setFont(font);
label.setForeground(Color.BLACK);
final JButton Backg = new JButton();
Backg.setBounds(565,295,310,410);
final JButton b1 = new JButton("1");
b1.setBounds(570,300,100,100);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
password.setText(String.copyValueOf(password.getPassword()) + b1.getText());
}});
final JButton b2 = new JButton("2");
b2.setBounds(670,300,100,100);
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
password.setText(String.copyValueOf(password.getPassword()) + b2.getText());
}});
final JButton b3 = new JButton("3");
b3.setBounds(770,300,100,100);
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
password.setText(String.copyValueOf(password.getPassword()) + b3.getText());
}});
final JButton b4 = new JButton("4");
b4.setBounds(570,400,100,100);
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
password.setText(String.copyValueOf(password.getPassword()) + b4.getText());
}});
final JButton b5 = new JButton("5");
b5.setBounds(670,400,100,100);
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
password.setText(String.copyValueOf(password.getPassword()) + b5.getText());
}});
final JButton b6 = new JButton("6");
b6.setBounds(770,400,100,100);
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
password.setText(String.copyValueOf(password.getPassword()) + b6.getText());
}});
final JButton b7 = new JButton("7");
b7.setBounds(570,500,100,100);
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
password.setText(String.copyValueOf(password.getPassword()) + b7.getText());
}});
final JButton b8 = new JButton("8");
b8.setBounds(670,500,100,100);
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
password.setText(String.copyValueOf(password.getPassword()) + b8.getText());
}});
final JButton b9 = new JButton("9");
b9.setBounds(770,500,100,100);
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
password.setText(String.copyValueOf(password.getPassword()) + b9.getText());
}});
final JButton b0 = new JButton("0");
b0.setBounds(670,600,100,100);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
password.setText(String.copyValueOf(password.getPassword()) + b0.getText());
}});
final JButton Enter = new JButton("Enter");
Enter.setBounds(770,600,100,100);
Enter.addActionListener(new AL());
final JButton Clear = new JButton("Clear");
Clear.setBounds(570,600,100,100);
Clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
password.setText("");}});
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
panel.add(b7);
panel.add(b8);
panel.add(b9);
panel.add(b0);
panel.add(Enter);
panel.add(Clear);
panel.add(password);
panel.add(label);
panel.add(Backg);
}
class AL implements ActionListener{
public void actionPerformed(ActionEvent e) {
//JPasswordField input = (JPasswordField) e.getSource();
char [] pass = password.getPassword();
String p = new String(pass);
if (p.equals(passy)){
JOptionPane.showMessageDialog(null, "Correct");
setVisible(false);
dispose();
ATM();
}
else
JOptionPane.showMessageDialog(null, "InCorrect");
}
}
public void Keypad() {
Keypad keypad = new Keypad();
keypad.setVisible(true);
}
public void ATM() {
ATM atm = new ATM();
atm.setVisible(true);
}
}
___________________________________________
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.lang.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
public class Balance extends JFrame {
public Balance() {
setSize(1440,900);
setTitle("Balance");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Driver D = new Driver();
String £ = "£";
String Blnc = Integer.toString(D.Money);
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
JLabel label = new JLabel("Your Balance is:");
label.setBounds(580,170,450,50);
JLabel Balance = new JLabel(£ + Blnc);
Balance.setBounds(705,233,200,25);
Font font = new Font("Verdana", Font.BOLD, 32);
label.setFont(font);
label.setForeground(Color.BLACK);
final JButton Backg = new JButton();
Backg.setBounds(565,295,310,410);
final JButton b1 = new JButton("1");
b1.setBounds(570,300,100,100);
final JButton b2 = new JButton("2");
b2.setBounds(670,300,100,100);
final JButton b3 = new JButton("3");
b3.setBounds(770,300,100,100);
final JButton b4 = new JButton("4");
b4.setBounds(570,400,100,100);
final JButton b5 = new JButton("5");
b5.setBounds(670,400,100,100);
final JButton b6 = new JButton("6");
b6.setBounds(770,400,100,100);
final JButton b7 = new JButton("7");
b7.setBounds(570,500,100,100);
final JButton b8 = new JButton("8");
b8.setBounds(670,500,100,100);
final JButton b9 = new JButton("9");
b9.setBounds(770,500,100,100);
final JButton b0 = new JButton("0");
b0.setBounds(670,600,100,100);
final JButton Enter = new JButton("Enter");
Enter.setBounds(770,600,100,100);
final JButton Clear = new JButton("Clear");
Clear.setBounds(570,600,100,100);
final JButton back = new JButton("Back");
back.setBounds(0,10,200,30);
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
setVisible(false);
dispose();
ATM();
}});
JButton close = new JButton("Close");
close.setBounds(1240,10,200,30);
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
panel.add(b7);
panel.add(b8);
panel.add(b9);
panel.add(b0);
panel.add(Enter);
panel.add(Clear);
panel.add(label);
panel.add(Backg);
panel.add(back);
panel.add(close);
panel.add(Balance);
}
public void ATM() {
ATM atm = new ATM();
atm.setVisible(true);
}
}
Sorry about all the code, but i am unsure how to explain this in any other way.
Thanks