Print.java--打印内容定义 [code] import java.awt.*; import java.awt.event.*; import java.awt.print.*; import java.awt.image.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.event.*; import java.util.*; import java.io.*; public class Print implements Printable{ int m_wPage; int m_hPage; int m_orientation; Printable m_target; int maxNumPage=1; String title="数据表格打印"; Font titleFont=new Font("黑体",Font.BOLD,14); boolean hasTail=true; int tailAlign=0; int headAlign=0; int topSpace=0; int leftSpace=0; int yStart=0; int yEnd=0; int xStart=topSpace; int xEnd=0; int x=0,y=0; String strTemp="打印内容"; public void doPrint(){ try{ m_orientation=PageFormat.PORTRAIT; //设置打印对象,默认纸张 PrinterJob prnJob=PrinterJob.getPrinterJob(); PageFormat pageFormat=prnJob.defaultPage(); pageFormat.setOrientation(m_orientation); m_wPage=(int)(pageFormat.getWidth()); m_hPage=(int)(pageFormat.getHeight()); //将待打印的窗体根据默认纸张设置传入打印对象 prnJob.setPrintable(this,pageFormat); if(!prnJob.printDialog()) return; prnJob.print(); }catch(PrinterException ex){ ex.printStackTrace(); System.err.println("打印错误:"+ex.toString()); } } /** * 初始化打印参数 */ public void initPrintParameter() { } /** *构造打印内容,以送打印机打印 */ public int print(Graphics pg,PageFormat pageFormat, int pageIndex) throws PrinterException{ //初始化打印参数 initPrintParameter(); //将画布设置为页面大小 pg.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY()); int wPage=0; int hPage=0; //根据打印机页面设置调整画布大小 if(pageFormat.getOrientation()==pageFormat.PORTRAIT){ wPage=(int)pageFormat.getImageableWidth(); hPage=(int)pageFormat.getImageableHeight(); } else{ wPage=(int)pageFormat.getImageableWidth(); wPage+=wPage/2; hPage=(int)pageFormat.getImageableHeight(); pg.setClip(0,0,wPage,hPage); } wPage=wPage-2*leftSpace; hPage=hPage-2*topSpace; xStart=leftSpace; xEnd=wPage-2; //为画布设置颜色和字体 int y=topSpace; pg.setFont(titleFont); pg.setColor(Color.black); //画标题,并使其居中 Font fn=pg.getFont(); FontMetrics fm=pg.getFontMetrics(); y+=fm.getAscent(); alignText(title,pg,y,xStart,xEnd,headAlign); y+=30; x=leftSpace+2; Font headerFont=new Font("宋体",Font.BOLD,14); pg.setFont(headerFont); fm=pg.getFontMetrics(); int h=fm.getAscent(); yStart=y-1; y+=h; pg.setFont(headerFont); fm=pg.getFontMetrics(); int header=y; h=fm.getHeight(); //计算行高,每页行数,总行数和指定页码的起始行、结束行 int rowH=Math.max(h,10); int tailH=rowH+30; int rowPerPage=0; int leftPix=0; if(hasTail){ rowPerPage=(hPage-header-tailH)/rowH; leftPix=(hPage-header-tailH)%rowH; yEnd=hPage-leftPix-tailH+2; } else{ rowPerPage=(hPage-header)/rowH; leftPix=(hPage-header)%rowH; yEnd=hPage-leftPix+2; } pg.drawString(strTemp,x,y); //画表格边框 pg.drawLine(xStart,yStart,xStart,yEnd); pg.drawLine(xStart,yStart,xEnd,yStart); pg.drawLine(xEnd,yStart,xEnd,yEnd); pg.drawLine(xStart,yEnd,xEnd,yEnd); //打印页码 if(hasTail){ int pageNumber=pageIndex+1; String s="第"+pageNumber+"页"; alignText(s,pg,yEnd+30,xStart,xEnd,tailAlign); } System.gc(); return PAGE_EXISTS; } /** * 文字排列,坐标在y处,显示范围(start-end) * 0表示居中显示,1表示左对齐,2表示右对齐 */ private void alignText(String s,Graphics pg,int y,int start, int end,int mode){ Font fn=pg.getFont(); FontMetrics fm=pg.getFontMetrics(); int wString=fm.stringWidth(s); int x=start; switch(mode) { case 0: if((end-start-wString)0) x=start+(end-start-wString)/2; break; case 1: break; case 2: if((end-start-wString)0) x=start+(end-start-wString); break; } pg.drawString(s,x,y); } public static void main(String[] args){ Print p=new Print(); p.doPrint(); } } [code] 运行方法: javac -d . Print.java java Print 自己运行一下 from:
在平乡等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计、网站建设 网站设计制作按需规划网站,公司网站建设,企业网站建设,品牌网站制作,营销型网站建设,外贸营销网站建设,平乡网站建设费用合理。
采纳哦
public
String
getStr(String
aa,String
bb)
{
String
str="abcdefg"
String
str1=str1.replace(aa,bb);//aa是被替换的字符串,bb是要替换成的内容
return
str;
}
如果有什么疑问,继续追问
首先导入 mysql-connector-java-5.1.45-bin.jar 包7a686964616f31333365633936
代码如下:
import java.io.*;import java.sql.*; public class App { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); // 数据库用户 String user = "root"; // 数据库密码 String password = ""; Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_sale", user, password); Statement stmt = conn.createStatement(); // 查询 , 从数据库 db_sale 的 product 表中查询 id, name, qty 字段 ResultSet rs = stmt.executeQuery("SELECT id, name, qty FROM product"); // 创建输出文件 result.txt File file = new File("d://result.txt"); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file)); while (rs.next()) { writer.write(String.valueOf(rs.getLong(1)) + "\t"); writer.write(rs.getString(2) + "\t"); writer.write(String.valueOf(rs.getInt(3))); writer.write("\r\n"); //System.out.println(rs.getLong(1)); //System.out.println(rs.getString(2)); //System.out.println(rs.getLong(3)); } writer.flush(); writer.close(); rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } }}
常用的输入语句是:
输入字符串:new Scanner(System.in).next();
输入整数:new Scanner(System.in).nextInt();
输入小数:new Scanner(System.in).nextDouble();
常用的输出语句:
换行输出: System.out.println(变量或字符串);
非换行输出: System.out.print(变量或字符串);
换行输出错误提示(默认是红字):System.err.println(变量或字符串);
不换行输出错误提示(默认是红字): System.err.print(变量或字符串));