RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java实现按钮代码,java做窗口和按钮

java怎么做个简单按钮

你写的按钮计算吧,这个类是一个Applet,其中有一个按钮,这个类本身也是按钮的动作监听器,所以实现了ActionListener 接口用来给按钮调用(也就是 actionPerformed方法),其中的参数e是事件参数,当点击按钮时会发送给按钮使用。e.getSource() == b 就是如果点击是b这个按钮,当监听器给一个按钮使用时没有必要加此判断,e.getSource就是获取发生事件的源对象,比如

成都创新互联专业为企业提供株洲网站建设、株洲做网站、株洲网站设计、株洲网站制作等企业网站建设、网页设计与制作、株洲企业网站模板建站服务,十余年株洲做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

c = new JButton("点我有次数哦");

f.getContentPane().add(c);

c.setVisible(true);

c.addActionListener(this);

此时又增加了一个按钮,就可以用e.getSource() 判断点击的是哪一个按钮。

建议你把面向对象搞懂在学swing编程吧,很容易看懂的

JAVA中怎么实现按钮功能?

使用图形用户界面

class Gui extends JFrame implements ActionListener {

private JButton jb = new JButton() ;

Gui() {

super("Gui") ;

this.add(jb) ;//添加按钮

jb.addActionListener(this) ;//按钮事件监听

//当然你可以按自己的想法做布局

this.pack();

this.setVisible(true);//可见

this.setResizable(false);//不可修改大小

this.setLocation(100, 100);//起始位置

}

//覆写ActionListener接口中的事件处理方法

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource() == jb) {

//事件处理

}

}

}

Java,按钮怎么实现?

给你个示例程序:

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class Test extends JFrame implements ActionListener {

private JPanel panel0 = null, panel2 = null;

private JButton b1 = null, b2 = null, b3 = null, b4 = null;

public Test() {

Container c = this.getContentPane();

//边框布局

c.setLayout(new BorderLayout());

//创建panel

panel0 = new JPanel();

panel2 = new JPanel();

//为2个panel设置底色

panel0.setBackground(Color.red);

panel2.setBackground(Color.BLUE);

//2个panel都是用流水布局

panel0.setLayout(new FlowLayout());

panel2.setLayout(new FlowLayout());

//创建按钮

b1 = new JButton("panel2黄色");

b2 = new JButton("panel2绿色");

b3 = new JButton("panel0橙色");

b4 = new JButton("panel0灰色");

/**

* 添加按钮事件

*/

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

/**

* 将按钮添加相应panel上

*/

panel0.add(b1);

panel0.add(new JLabel());

panel0.add(b2);

panel2.add(b3);

panel2.add(b4);

/**

* 将panel添加到容器

*/

c.add(panel0, BorderLayout.CENTER);

c.add(panel2, BorderLayout.EAST);

this.setSize(500, 500);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}    

public static void main(String[] args) {

new Test();    

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if (e.getSource() == b1) {

panel2.setBackground(Color.yellow);

} else if (e.getSource() == b2) {

panel2.setBackground(Color.green);

} else if (e.getSource() == b3) {

panel0.setBackground(Color.ORANGE);

}  else if (e.getSource() == b4) {

panel0.setBackground(Color.GRAY);

}

}


网页标题:java实现按钮代码,java做窗口和按钮
路径分享:http://scyingshan.cn/article/hchooj.html