189 8069 5689

java中按钮响应代码,html按钮响应

在Java中,Button类的响应函数怎么写

下面以点击按钮后改变窗口背景色为例:

成都创新互联-专业网站定制、快速模板网站建设、高性价比山海关网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式山海关网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖山海关地区。费用合理售后完善,10余年实体公司更值得信赖。

import java.awt.*;

import java.awt.event.*;

public class MyAWTDemo extends Frame{

public MyAWTDemo() {

Button bt = new Button("=点击按钮,改变背景色=");

//给按钮添加响应代码

bt.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

//这里写,你想点击按钮后,执行的代码

setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));

}

});

add(bt);

setLayout(new FlowLayout());

setTitle("窗口标题");

setSize(260, 100);

setLocationRelativeTo(null);

setVisible(true);

//给窗口添加一个响应代码,当我们点击窗口右上角的关闭按钮时,系统就退出JVM虚拟机

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);//系统退出

}

});

}

public static void main(String[] args) {

new MyAWTDemo();

}

}

效果图

JAVA按钮响应的问题,下面是代码

经调试,已经解决:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class GUI1 extends JFrame {

private JButton b1 = new JButton("button1");

private JButton b2 = new JButton("button2");

private JTextField txt = new JTextField(10);

private ActionListener bl = new ActionListener() { // 定义一个内部类

public void actionPerformed(ActionEvent e) {

String name = ((JButton) e.getSource()).getText();

txt.setText(name);

}

};

public GUI1(int a, int b) {

setSize(a, b);

b1.addActionListener(bl);

b2.addActionListener(bl);

setLayout(new FlowLayout());

add(b1);

add(b2);

add(txt);

}

public static void main(String[] args) {

GUI1 s = new GUI1(200, 110);

s.setDefaultCloseOperation(EXIT_ON_CLOSE);

s.setLocationRelativeTo(null);

s.setVisible(true);

}

}

java中的按钮响应

addActionListener的参数是ActionListener你的this是Applet。当然不对啦。

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class Jsq extends Applet implements ActionListener{

Button b1;

public void init() {

Button b1=new Button("+");

b1.addActionListener(this);

add(b1);

}

public void actionPerformed(ActionEvent e)

{

System.out.println("you click the button");

}

}


新闻标题:java中按钮响应代码,html按钮响应
文章路径:http://cdxtjz.cn/article/dsgepjj.html

其他资讯