编写java程序实现右图所示的界面,并为按钮添加事件处理,要求每按一下按钮,数字加5,初始值为0。
2个回答
展开全部
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class chen {
static int i = 0;
public static void main(String args[]){
final JButton button = new JButton("按钮(+5)");
JFrame frame = new JFrame("图形界面与事件处理");
JLabel lable = new JLabel();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==button){
i = +5;
}
}
});
lable.setText("i");
frame.add(lable,BorderLayout.NORTH);
frame.add(button);
frame.setBounds(30, 30, 200, 200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.exit(0);
}
});
chen che = new chen();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class chen {
static int i = 0;
public static void main(String args[]){
final JButton button = new JButton("按钮(+5)");
JFrame frame = new JFrame("图形界面与事件处理");
JLabel lable = new JLabel();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==button){
i = +5;
}
}
});
lable.setText("i");
frame.add(lable,BorderLayout.NORTH);
frame.add(button);
frame.setBounds(30, 30, 200, 200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.exit(0);
}
});
chen che = new chen();
}
}
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test extends JFrame implements ActionListener
{
JFrame jframe = new JFrame("图形界面与事件处理");
JPanel jp = new JPanel();
int count =0;
JButton add = new JButton("按钮"+"("+count+")");
Test(){
jframe.add(jp);
jp.add(add);
add.addActionListener(this);
jframe.setLocation(300, 300);
jframe.setSize(300, 300);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object soc = e.getSource();
if(soc==add){
count=count+5;
add.setText("按钮"+"("+count+")");
}
}
public static void main(String[] args) {
Test test = new Test();
}
}
你测试下 看符合你的要求吗
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test extends JFrame implements ActionListener
{
JFrame jframe = new JFrame("图形界面与事件处理");
JPanel jp = new JPanel();
int count =0;
JButton add = new JButton("按钮"+"("+count+")");
Test(){
jframe.add(jp);
jp.add(add);
add.addActionListener(this);
jframe.setLocation(300, 300);
jframe.setSize(300, 300);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object soc = e.getSource();
if(soc==add){
count=count+5;
add.setText("按钮"+"("+count+")");
}
}
public static void main(String[] args) {
Test test = new Test();
}
}
你测试下 看符合你的要求吗
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询