如何将动作侦听器添加到按钮 [英] How can I add the action listener to buttons

查看:83
本文介绍了如何将动作侦听器添加到按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的源代码中添加动作监听器但是我得到一个contenuos错误,上面写着你可以为你的abstruct按钮添加动作监听器

所以我想知道我是怎么回事会这样做...有一个PLIZ帮助我,我刚刚开始学习这种java语言



我尝试过:



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:

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);
    }
}

推荐答案

将动作监听器添加到 My_name 类而不是单独的类。请参阅如何使用按钮,复选框和单选按钮(The Java™Tutorials> Creating使用JFC / Swing的GUI>使用Swing组件) [ ^ ]
Add the action listener to your My_name class rather than a separate class. See How to Use Buttons, Check Boxes, and Radio Buttons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)[^]


这篇关于如何将动作侦听器添加到按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆