在java中 上一个 按钮监听代码怎么写
3个回答
展开全部
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TestRandom extends JFrame {
JTextField field;
JButton randomButton;
RandomNumber t = new RandomNumber();
public TestRandom() {
field = new JTextField(15);
randomButton = new JButton("产生随机数");
randomButton.addActionListener(new RandomListener());
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(field);
this.getContentPane().add(randomButton);
this.setSize(300,100);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
class RandomListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String actionCommand = randomButton.getText();
if(actionCommand.equals("产生随机数")) {
randomButton.setText("停止");
t.stop = false;
new Thread(t).start();
} else if(actionCommand.equals("停止")) {
randomButton.setText("产生随机数");
t.stop = true;
}
}
}
class RandomNumber implements Runnable {
Random random;
StringBuffer num;
boolean stop = false;
public RandomNumber() {
random = new Random();
num = new StringBuffer("");
}
public void run() {
while(!stop) {
num.setLength(0);
for(int i=0;i<6;i++) {
num.append(random.nextInt(9));
}
field.setText(num.toString());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new TestRandom();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TestRandom extends JFrame {
JTextField field;
JButton randomButton;
RandomNumber t = new RandomNumber();
public TestRandom() {
field = new JTextField(15);
randomButton = new JButton("产生随机数");
randomButton.addActionListener(new RandomListener());
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(field);
this.getContentPane().add(randomButton);
this.setSize(300,100);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
class RandomListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String actionCommand = randomButton.getText();
if(actionCommand.equals("产生随机数")) {
randomButton.setText("停止");
t.stop = false;
new Thread(t).start();
} else if(actionCommand.equals("停止")) {
randomButton.setText("产生随机数");
t.stop = true;
}
}
}
class RandomNumber implements Runnable {
Random random;
StringBuffer num;
boolean stop = false;
public RandomNumber() {
random = new Random();
num = new StringBuffer("");
}
public void run() {
while(!stop) {
num.setLength(0);
for(int i=0;i<6;i++) {
num.append(random.nextInt(9));
}
field.setText(num.toString());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new TestRandom();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
实例:
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Record record=RecordService.getRecordByComputorId(Integer.parseInt(cbComputorId.getSelectedItem().toString()));
Calendar calendar=Calendar.getInstance();
calendar.setTime(record.getStartTime());
float startTime=calendar.getTimeInMillis();
Date endTime=new Date();
calendar.setTime(endTime);
float endTimeMillis= System.currentTimeMillis();
int costTime= (int)Math.ceil((endTimeMillis-startTime)/1000/60/60+0.001);//计算上机时间,单位为小时.
int costMoney =(int) (costTime*NetBar.PRICE_PER_HOUR);
record.setEndTime(endTime);
record.setCostMoney(costMoney);
new Information(parentFrame,true,record);
}
});
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Record record=RecordService.getRecordByComputorId(Integer.parseInt(cbComputorId.getSelectedItem().toString()));
Calendar calendar=Calendar.getInstance();
calendar.setTime(record.getStartTime());
float startTime=calendar.getTimeInMillis();
Date endTime=new Date();
calendar.setTime(endTime);
float endTimeMillis= System.currentTimeMillis();
int costTime= (int)Math.ceil((endTimeMillis-startTime)/1000/60/60+0.001);//计算上机时间,单位为小时.
int costMoney =(int) (costTime*NetBar.PRICE_PER_HOUR);
record.setEndTime(endTime);
record.setCostMoney(costMoney);
new Information(parentFrame,true,record);
}
});
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你能说的详细一点吗? 上一个指的是什么?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询