Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have tried to add action listener to the buttons in my source code but i get a contenuos error that says "you can add action listener to your abstruct buttons"
so i was wondering how i would do that...some one pliz help me i have just started learning this java language

What I have tried:

Java
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextField;
import javax.swing.JLabel;
 
public class My_name extends JPanel
{
	JButton b1,b2,b3,b4,b5,b6,b7;
	GridBagConstraints gbc = new GridBagConstraints();
	JTextField f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12;
	JLabel l1,l2,l3,l4;
	
	public My_name()
	{
		setLayout (new GridBagLayout());
		gbc.insets = new Insets(8,8,8,8);
		
		l1 = new JLabel("Mark Entry");
		gbc.gridx = 0;
		gbc.gridy = 0;
		add(l1,gbc);
		
		f1 = new JTextField("100%");
		f1.setEditable(false);
		gbc.gridx = 1;
		gbc.gridy = 0;
		add(f1,gbc);
		
		l2 = new JLabel("%");
		gbc.gridx = 1;
		gbc.gridy = 2;
		add(l2,gbc);
		
		l3 = new JLabel("Grade");
		gbc.gridx = 2;
		gbc.gridy = 2;
		add(l3,gbc);
		
		//These are the buttons
	b1 = new JButton("Business inteligence");
	gbc.gridx = 0;
	gbc.gridy = 3;
	add(b1,gbc);
	
	b2 = new JButton("DataBase Systems   ");
	gbc.gridx = 0;
	gbc.gridy = 4;
	add(b2,gbc);
	 
	b3 = new JButton("       Networks             ");
	gbc.gridx = 0;
	gbc.gridy = 5;
	add(b3,gbc);
	
	b4 = new JButton("Programming         ");
	gbc.gridx = 0;
	gbc.gridy = 6;
	add(b4,gbc);
	
	b5 = new JButton("System Security     ");
	gbc.gridx = 0;
	gbc.gridy = 7;
	add(b5,gbc);
	
//These are text fields
	
	//Fast column
	f1 = new JTextField(5);
	gbc.gridx = 1;
	gbc.gridy = 3;
	add(f1,gbc);
	
	f2 = new JTextField(5);
	gbc.gridx = 1;
	gbc.gridy = 4;
	 add(f2,gbc);
	 
	f3 = new JTextField(5);
	gbc.gridx = 1;
	gbc.gridy = 5;
	add(f3,gbc);
	
	f4= new JTextField(5);
	gbc.gridx = 1;
	gbc.gridy = 6;
	add(f4,gbc);
	
	f5 = new JTextField(5);
	gbc.gridx = 1;
	gbc.gridy = 7;
	add(f5,gbc);//End of first column
	
	
	//second column
	f6 = new JTextField(5);
	gbc.gridx = 2;
	gbc.gridy = 3;
	add(f6,gbc);
	
	f7 = new JTextField(5);
	gbc.gridx = 2;
	gbc.gridy = 4;
	add(f7,gbc);
	
	f8 = new JTextField(5);
	gbc.gridx = 2;
	gbc.gridy = 5;
	add(f8,gbc);
	
	f9 = new JTextField(5);
	gbc.gridx = 2;
	gbc.gridy = 6;
	add(f9,gbc);
	
	f10 = new JTextField(5);
	gbc.gridx = 2;
	gbc.gridy = 7;
	add(f10,gbc);//End of second column
	
		//additional components
	f11 = new JTextField(5);
	gbc.gridx = 1;
	gbc.gridy = 9;
	add(f11,gbc);
	
	f12 = new JTextField(5);
	gbc.gridx = 2;
	gbc.gridy = 9;
	add(f12,gbc);
	
	l1 = new JLabel("Over All");
	gbc.gridx = 0;
	gbc.gridy = 9;
	add(l1,gbc);
	
	b6 = new JButton("Over All");
	gbc.gridx = 1;
	gbc.gridy = 10;
	add(b6,gbc);
	
	b7 = new JButton("Clear ");
	gbc.gridx = 2;
	gbc.gridy =  10;
	add(b7,gbc);
	
	//Object for event handling.....
 /*this code has been elimineted cause it incomplete just find ways of completing it thank you*/
	/*
	Thehandler handler = new Thehandler();
	b1.addActionListener(handler);
	b2.addActionListener(handler);
	b3.addActionListener(handler);
	b4.addActionListener(handler);
	b5.addActionListener(handler);
	
	}
	 private class Thehandler implements ActionListener
	 {
	 	public void actionPerformed (ActionEvent e)
	 	{
	 		if(event.getSource()==b1)
	 		
	 		
	 	}*/
	 }
    
    public static void main(String[] args) 
    {
    	
    		My_name m = new My_name();
    	JFrame f = new JFrame();
    	
    	
    	f.setTitle("My work");
    	f.setSize(600,800);
    	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	f.setVisible(true);
    	
    	f.add(m);
    }
}
Posted
Updated 8-Jun-18 22:02pm
v2

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900