请帮助尝试使用此代码在热敏打印机中打印,我在系统托盘中打印了一份打印作业,但我的热敏打印机没有打印.PrintReceiptUtil类:
import java.awt.JobAttributes;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
public static void doPrintReceipt() throws IOException, PrintException{
String defaultPrinter = PrintServiceLookup.lookupDefaultPrintService().getName();
System.out.println("Default printer: " + defaultPrinter);
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
// prints the famous hello world! plus a form feed
InputStream is = new ByteArrayInputStream("hello world!\f".getBytes("UTF8"));
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; …Run Code Online (Sandbox Code Playgroud) 我们有一段代码根据mysql定义的函数进行选择.我们已经发现,一旦启用了这部分,那么runtime.totalMemory() 使用的内存会不断增加.以下是代码片段.问题始于String selectQuery4.知道为什么这是一个问题?
double currentLong = Double.parseDouble(longitude);
double currentLat = Double.parseDouble(latitude);
Statement stmt6 = null;
stmt6 = dbconn.createStatement();
String selectQuery3 = "Select geoFenceID,enterpriseID,clientID,geoFenceString,geoFenceType,geoFenceName,geoFenceDescription,geoFencePreference, geoFenceRadius From tblGeoFence Where ((enterpriseID="+enterpriseID+" And clientID=0) Or (enterpriseID="+enterpriseID+" And clientID="+clientID+")) And geoFenceStatus='a'";
ResultSet rs3 = stmt6.executeQuery(selectQuery3);
while(rs3.next()) {
geoFenceID = rs3.getInt("geoFenceID");
geoFenceType = rs3.getString("geoFenceType");
geoFenceString = rs3.getString("geoFenceString");
geoFenceRadius = rs3.getInt("geoFenceRadius");
geoFencePreference = rs3.getString("geoFencePreference");
String polygon = "GeomFromText('"+geoFenceString+"')";
String point = "GeomFromText('POINT("+currentLong+" "+currentLat+")')";
Statement stmt7 = null;
stmt7 = dbconn.createStatement();
String selectQuery4 = "SELECT …Run Code Online (Sandbox Code Playgroud) 好的,所以我有以下代码:
Random rnd = new Random();
int i = 0;
while(i<1000) {
String name = "event" + i;
Event name = new Event(rnd.nextInt(100000)); //ERROR duplicate variable
SimEngine.getScheduler().addEventToQueue(event);
i++;
}
System.out.println(SimEngine.getScheduler().getQueue().iterator());
Run Code Online (Sandbox Code Playgroud)
我知道两次宣布名字毫无意义,但我希望你能看到我想要做的事情.因为我想要名称为event1,event2,event3等的Event对象.
我怎样才能让它使用String名称作为Event对象的名称?
我有一个程序&我不会随机读取ARRAY我没有程序错误但在输出期间我有空值我能给你解决方案...
import java.io.FileInputStream;
import java.util.Properties;
import java.util.Random;
import javax.swing.JOptionPane;
public class ReadDB {
public static void main(String[] args) {
Properties prob = new Properties();
String word [] = new String [20] ;
try{
prob.load( new FileInputStream("words.txt"));
}catch(Exception e){
}
for(int i=1; i<6; i++){
String temp = prob.getProperty(""+i);
word[i] = temp;
Random ran = new Random ();
String Val = word[ran.nextInt(word.length)];
System.out.println(Val);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我在Android eclipse中使用SQLite,但它在函数createEntry中给了我一个java.lang.nullpointerexception.我尝试使用Questoid SQLite管理器来查看数据库文件,它确实显示了创建的表.错误在哪里?
public class Transact {
public static final String KEY_ROWID = "_id";
public static final String KEY_TAG = "saved_tag";
private static final String DATABASE_NAME = "MyDatabaseName";
private static final String DATABASE_TABLE = "tagsTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"CREATE TABLE …Run Code Online (Sandbox Code Playgroud) 这是我的示例程序.
class parent
{
void display(int i)
{
System.out.println("parent");
}
}
class child extends parent
{
void display(byte i) //Line 0
{
System.out.println("child");
return;
}
}
class impl
{
public static void main(String...args)
{
parent p = new parent();
p.display(5); //Line 1
child c = new child();
c.display(3); //Line 2
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的输出.
varun@\:~/Desktop/JavaFiles$ java impl
parent
parent
Run Code Online (Sandbox Code Playgroud)
我理解第1行从父级调用display()方法并输出预期的"parent".
但是我不明白为什么第2行从父节点而不是子节点调用display(),即使我没有使用多态初始化(只是常规初始化子类是我做的).
基本上我的问题是我有一个随机数生成器,double在给定int + - 0.5的用户之间生成一个随机值:
double actualClickInterval = (ThreadLocalRandom.current().nextDouble(clickinterval - 0.5, clickinterval + 0.5 +1));
Run Code Online (Sandbox Code Playgroud)
clickinterval用户在哪里给int.
应该将此double值传递给Thread.sleep(),这需要Long值.如果我将其转换为Long值,小数将消失.有没有办法解决?
当前Thread.sleep()代码如下:
Thread.sleep((long)actualClickInterval * 1000 - 500);
Run Code Online (Sandbox Code Playgroud)
我需要将值设置为double,因为执行的任务需要使用<500ms长的随机延迟来完成.任何其他方法也是受欢迎的.
我只是想知道为什么这段代码会抛出错误.错误是:
"线程中的异常"Thread-1"java.lang.Error"
class Salmon extends Thread
{
public static long id;
public void run()
{
for(int i = 0;i<4; i++){
if(i==2&& id ==Thread.currentThread().getId()){
//if(i==2){
new Thread(new Salmon()).start();
throw new Error();
}
System.out.println(i + " ");
}
}
public static void main(String[] args)
{
Thread t1 = new Salmon();
id = t1.getId();
t1.start();
}
}
Run Code Online (Sandbox Code Playgroud) 我有这样的链接:
/add.php?id=50&link=page2
Run Code Online (Sandbox Code Playgroud)
我希望它重定向到这个:
/add/50/page2
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?我尝试了以下,但它不起作用:
Redirect /add.php?id=50&link=page2 /add/50/page2
Run Code Online (Sandbox Code Playgroud)