Please help how i can add functionality to this...How can i make the buttons respond to mouse clicks. thanks for your time.i used gridbaglayout by the way.
package ui;
import func.*;
import java.awt.*;
import javax.swing.*;
public class CalcMainFrame
extends JFrame {
final static boolean SHOULD_FILL = true;
final static boolean SHOULD_WEIGHT_X = true;
final static boolean RIGHT_TO_LEFT = false;
public CalcMainFrame() {
super("MOUSE CLICK PROB");
}
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
JTextField textField1;
textField1 = new JTextField("", 5);
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (SHOULD_FILL) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
if (SHOULD_WEIGHT_X) {
c.weightx = 1.0;
c.ipady = 10;
c.ipadx = 10;
}
c.gridx = 0;
c.gridy = 0;
pane.add(textField1);
button = new JButton(" 1 ");
c.gridx = 0;
c.gridy = 1;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 2 ");
c.gridx = 1;
c.gridy = 1;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 3 ");
c.gridx = 2;
c.gridy = 1;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" + ");
c.gridx = 3;
c.gridy = 1;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 4 ");
c.gridx = 0;
c.gridy = 2;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 5 ");
c.gridx = 1;
c.gridy = 2;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 6 ");
c.gridx = 2;
c.gridy = 2;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" - ");
c.gridx = 3;
c.gridy = 2;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 7 ");
c.gridx = 0;
c.gridy = 3;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 8 ");
c.gridx = 1;
c.gridy = 3;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 9 ");
c.gridx = 2;
c.gridy = 3;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" / ");
c.gridx = 3;
c.gridy = 3;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" C ");
c.gridx = 0;
c.gridy = 4;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 0 ");
c.gridx = 1;
c.gridy = 4;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" = ");
c.gridx = 2;
c.gridy = 4;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" x ");
c.gridx = 3;
c.gridy = 4;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
}
public static void main(String argv[]) {
JFrame.setDefaultLookAndFeelDecorated(true);
CalcMainFrame frame = new CalcMainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
}
package ui;
import func.*;
import java.awt.*;
import javax.swing.*;
public class CalcMainFrame
extends JFrame {
final static boolean SHOULD_FILL = true;
final static boolean SHOULD_WEIGHT_X = true;
final static boolean RIGHT_TO_LEFT = false;
public CalcMainFrame() {
super("MOUSE CLICK PROB");
}
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
JTextField textField1;
textField1 = new JTextField("", 5);
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (SHOULD_FILL) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
if (SHOULD_WEIGHT_X) {
c.weightx = 1.0;
c.ipady = 10;
c.ipadx = 10;
}
c.gridx = 0;
c.gridy = 0;
pane.add(textField1);
button = new JButton(" 1 ");
c.gridx = 0;
c.gridy = 1;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 2 ");
c.gridx = 1;
c.gridy = 1;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 3 ");
c.gridx = 2;
c.gridy = 1;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" + ");
c.gridx = 3;
c.gridy = 1;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 4 ");
c.gridx = 0;
c.gridy = 2;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 5 ");
c.gridx = 1;
c.gridy = 2;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 6 ");
c.gridx = 2;
c.gridy = 2;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" - ");
c.gridx = 3;
c.gridy = 2;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 7 ");
c.gridx = 0;
c.gridy = 3;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 8 ");
c.gridx = 1;
c.gridy = 3;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 9 ");
c.gridx = 2;
c.gridy = 3;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" / ");
c.gridx = 3;
c.gridy = 3;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" C ");
c.gridx = 0;
c.gridy = 4;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" 0 ");
c.gridx = 1;
c.gridy = 4;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" = ");
c.gridx = 2;
c.gridy = 4;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
button = new JButton(" x ");
c.gridx = 3;
c.gridy = 4;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);
}
public static void main(String argv[]) {
JFrame.setDefaultLookAndFeelDecorated(true);
CalcMainFrame frame = new CalcMainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
}