java 两个按钮_java 两个按钮 事件

java 两个按钮 事件

关注:294  答案:4  mip版

解决时间 2021-01-26 04:28

e6cb1a03ad541b3098697807b7bf1798.png

提问者阳光在浪尖跳动

2021-01-25 10:49

写一个界面 有两个按钮,一个文本框。按第一个按钮,文本框显示1,按第二个按钮,文本框显示2.

在线等。。。

zqkun 你的运行不起来耶,没main。能给我个直接能运行的吗?

最佳答案

e6cb1a03ad541b3098697807b7bf1798.png

二级知识专家可惜感動不是心動

2021-01-25 11:17

import javax.swing.*;

import java.awt.GridLayout;

class WindowUI implments {

JFrame frame;

JPanel panel;

JButton but1, but2;

JTextField tf;

public WIndowUI() {

frame = new JFrame("WindowUI");

JFrame.setDefaultLookAndFeelDecorated(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel(new GridLayout(0,1));

but1 = new JButton("Button1");

but1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

tf.setText("1");

}

});

but2 = new JButton("Button2");

but2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

tf.setText("2");

}

tf = new JTextField();

panel.add(but1);

panel.add(but2);

panel.add(tf);

frame.add(panel);

fame.setVisible(true);

frame.pack();

});

}

}

全部回答

e6cb1a03ad541b3098697807b7bf1798.png

1楼抚弦思华年

2021-01-25 12:01

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class ButtonAction {

private JFrame frame;

private JPanel panel;

private JButton b1,b2;

private JTextField t;

public ButtonAction(){

frame=new JFrame("按钮事件");

panel=new JPanel();

b1=new JButton("1");

b2=new JButton("2");

t=new JTextField(10);

b1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

t.setText("1");

}

});

b2.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

t.setText("2");

}

});

panel.add(t);

panel.add(b1);

panel.add(b2);

frame.getContentPane().add(panel);

frame.pack();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

public static void main(String[] args) {

new ButtonAction();

}

}

e6cb1a03ad541b3098697807b7bf1798.png

2楼棄療尐钕

2021-01-25 11:47

这都要别人写,太对不起自己的学费了。

e6cb1a03ad541b3098697807b7bf1798.png

3楼在時光深處躲貓貓

2021-01-25 11:33

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.JTextField;

public class AwtEvent extends JFrame implements ActionListener { //文件名为:AwtEvent.java

private JButton but1, but2;

private JTextField text;

public AwtEvent() {

super("AwtEvent");

Container c = getContentPane();

c.setLayout(new FlowLayout());

text = new JTextField(10);

but1 = new JButton("1");

but2 = new JButton("2");

c.add(text);

c.add(but1);

c.add(but2);

setSize(300, 80);

setVisible(true);

validate();

but1.addActionListener(this);

but2.addActionListener(this);

}

public static void main(String[] args) {

new AwtEvent();

}

@Override

public void actionPerformed(ActionEvent e) {

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

text.setText("1");

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

text.setText("2");

}

}

}

我要举报

如果感觉以上信息为低俗/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

点此我要举报以上信息!

推荐资讯

大家都在看

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值