java文本牛逼代码 java代码怎么写( 三 )


e.printStackTrace(); //异常捕获,不用多说 。
}
}
}
//以下是上面设置的事件监听的具体处理办法,即监听时间处理方法 , 自动调用
public void actionPerformed(ActionEvent e){ //传入一个动作事件的参数e
String s = e.getActionCommand(); //设置字符串s来存储获得动作监听,上面的start
/*
以下这个条件语句块的作用为:用户点击开始后(捕获start , 用方法getActionCommand()),将命令触发设置为true,从而执行上面的go方法中的循环体(因为循环体中要求isGo参数为true,而初始为false) 。
执行循环快产生随机数,并将开始按钮不可编辑化,而用户只可以使用停止按钮去停止 。如果用户按下停止时,也就是没有传入参数“start”的时候,
执行else语句块中的语句,isGo设置为false,将不执行上面go中的循环语句块,从而停止产生随机数,并显示,并且把开始按钮设置为可用,而把
停止按钮设置为不可用 , 等待用户按下开始再去开始新一轮循环产生随机数 。
*/
if(s.equals("start")){ //如果捕获到start,也就是用户触发了动作监听器,那么下面处理
isGo = true; //设置isGo为true
b1.setEnabled(false); //将开始按钮设置为不可用
b2.setEnabled(true); //将停止按钮设置为可用
}else{
isGo = false; //将isGo设置为false,isGo为循环标志位
b2.setEnabled(false); //设置停止按钮为不可用(注意看是b2,b2是停止按钮)
b1.setEnabled(true); //设置开始按钮为可用
}
}
public static void main(String[] args){
new GoodLucky(); //产生类的实例,执行方法
}
}
想设计一个java小程序,在文本框输入一个数,然后每一秒+1 , 求给完整代码package com.demo;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TimerTest {
private Timer timer;
private JTextField field;
private JButton button;
private boolean flag = true;
public TimerTest() {
timer = new Timer();
addview();
}
private void addview() {
JFrame frame = new JFrame("Timer test");
field = new JTextField();
field.setPreferredSize(new Dimension(0, 30));
button = new JButton("start");
button.setPreferredSize(new Dimension(100, 30));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (flag) {
auto();
flag = false;
button.setText("stop");
} else {
timer.cancel();
flag = true;
button.setText("start");
}
}
});
frame.add(field, BorderLayout.CENTER);
frame.add(button, BorderLayout.EAST);
frame.setBounds(200, 200, 300, 60);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TimerTest();
}
private void auto() {
timer.schedule(new TimerTask() {
@Override
public void run() {
int num = Integer.parseInt(field.getText().trim());
num += 1;
field.setText(num + "");
auto();
}
}, 1000);
}
}
java读取文本文件代码java读取文本文件的方法有很多 这个例子主要介绍最简单 最常用的BufferedReader类 完整例子如下 package net chinaunix blog hzm text;import java io BufferedReader;import java io FileReader;import java io IOException;public class ReadFile {private String path;public ReadFile(String filePath){path = filePath;}public String[] openFile() throws IOException{FileReader fr = new FileReader(path) BufferedReader textReader = new BufferedReader(fr) String[] textData = https://www.04ip.com/post/new String[readLines()];int i;for(i= ; ireadLines() i++){textData[i] = textReader readLine() }textReader close() return textData;}int readLines() throws IOException{FileReader fileToRead = new FileReader(path) BufferedReader bf = new BufferedReader(fileToRead) int numberOfLines = ;@SuppressWarnings( unused )String oneLine;while((oneLine = bf readLine()) != null){numberOfLines++;}bf close() return numberOfLines;}}package net chinaunix blog hzm text;import java io IOException;public class FileData {public static void main(String[] args) throws IOException{String filePath = C:/text txt ;try{ReadFile reader = new ReadFile(filePath) String[] content = reader openFile() int i;for(i= ;icontent length;i++){System out println(content[i]) }}catch(IOException e){System out println( 异常信息 + e getMessage()) }}}java io BufferedReaderThe buffer size may be specified or the default size may be used The default is large enough for most purposes In general each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly such as FileReaders and InputStreamReaders For example BufferedReader in = new BufferedReader(new FileReader( foo in )) will buffer the input from the specified file Without buffering each invocation of read() or readLine() could cause bytes to be read from the file converted into characters and then returned which can be very inefficient Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader java io FileReaderFileReader is meant for reading streams of characters For reading streams of raw bytes consider using a FileInputStream lishixinzhi/Article/program/Java/hx/201311/26249

推荐阅读