【教程】将面板和其他Swing组件绘制到PDF文档

关联产品

java代码示例:

public class PrintPanelToPDF implements Printable
{
    public static void main (String [] args)
    {
    	try
    	{
    	 // create a PDF Printer Job
    	 PDFPrinterJob printer = (PDFPrinterJob)PDFPrinterJob.getPrinterJob ();
    	 // set the printable object 
    	 printer.setPrintable (new PrintPanelToPDF());
    	 // set number of copies to 1 
    	 printer.setCopies (1);
    	 // print and save the document
    	 printer.print("C:\\test\\mydoc.pdf");
    	 // output done message 
    	 System.out.println("Done!");
    	}
    	catch (Throwable t)
    	{
    	 t.printStackTrace();
    	}
    }
 
    public int print (Graphics g, PageFormat pf, int pageIndex) 
    {
    	if (pageIndex == 0)
    	{
 
          // translate the graphics for margins
    	  g.translate(100, 100);
 
    	  // create a panel with a label and a textfield
    	  JPanel myPanel = new javax.swing.JPanel();
    	  myPanel.setName("myPanel");
    	  myPanel.setBorder(new javax.swing.border.EtchedBorder());
    	  myPanel.setBounds(new java.awt.Rectangle(0, 0, 300, 300));
    	  // create a label
    	  JLabel myLabel = new JLabel("MyLabel");
    	  myLabel.setText("Label");
    	  myLabel.setLocation(10, 10);
    	  myLabel.setSize(50, 20);
    	  myLabel.setVisible(true);
    	  // add label to panel
    	  myPanel.add(myLabel);
    	  // create a text field 
    	  JTextField myTextField = new JTextField();
    	  myTextField.setText("My Text");
    	  myTextField.setLocation(65, 10);
    	  myTextField.setSize(50, 20);
    	  myTextField.setVisible(true);
    	  // add text field to panel
    	  myPanel.add(myTextField);
    	 // you can add any swing component here
    	 // ..... 
    	 // revalidate and repaint the panel
    	 myPanel.revalidate();
         myPanel.repaint();
    	 // print the panel to the graphics on page 0
    	 myPanel.print (g);
    	 return Printable.PAGE_EXISTS;
    	}
    	else
    	{
    	 return Printable.NO_SUCH_PAGE;
    	}
    }
}

转载于:https://my.oschina.net/u/3006003/blog/908351

Java 图形界面开发简介 .............. ................................ ................................ ..... 5 1. Swing1. Swing1. Swing 1. Swing1. Swing1. Swing1. Swing简介 ................................ ................................ ................................ ................................ ............... 5 2. Swing2. Swing2. Swing 2. Swing2. Swing2. Swing2. Swing组件 ................................ ................................ ................................ ................................ ............... 5 3. 3. 3. 布局管理器 布局管理器 ................................ ................................ ................................ ................................ ............. 8 4. 4. 4. 代码实例 代码实例 : 一个简单的窗口程序 一个简单的窗口程序 一个简单的窗口程序 一个简单的窗口程序 一个简单的窗口程序 ................................ ................................ ................................ ..... 9 1.1: FlowLayo1.1: FlowLayo1.1: FlowLayo1.1: FlowLayo1.1: FlowLayo 1.1: FlowLayo 1.1: FlowLayout (流式布局) (流式布局) (流式布局) (流式布局) ................................ ................................ ................................ ...................... 10 1. 概述 ................................ ................................ ................................ ................................ ....................... 10 2. 代码实例 ................................ ................................ ................................ ................................ .............. 11 1.2: GridLayout(网格布局) (网格布局) (网格布局) (网格布局) ................................ ................................ ................................ ..................... 13 1. 概述 ................................ ................................ ................................ ................................ ....................... 13 2. 代码演示 ................................ ................................ ................................ ................................ .............. 14 1.3: GridBagLayout1.3: GridBagLayout1.3: GridBagLayout1.3: GridBagLayout1.3: GridBagLayout 1.3: GridBagLayout 1.3: GridBagLayout1.3: GridBagLayout 1.3: GridBagLayout1.3: GridBagLayout 1.3: GridBagLayout1.3: GridBagLayout(网格袋布局) (网格袋布局) (网格袋布局) (网格袋布局) ................................ ................................ ................................ ........... 17 1. 布局 : GridBagLayout ................................ ................................ ................................ ...................... 17 2. 约束 : GridBagConstraints ................................ ................................ ................................ ............. 17 3. 属性 : GridBagConstraints 的属性 ................................ ................................ ............................. 18 4. 案例 : GridBagLayout使用实例 使用实例 ................................ ................................ ................................ ... 19 1.4: BoxLayout1.4: BoxLayout1.4: BoxLayout1.4: BoxLayout1.4: BoxLayout 1.4: BoxLayout1.4: BoxLayout1.4: BoxLayout 1.4: BoxLayout1.4: BoxLayout(箱式布局) (箱式布局) (箱式布局) (箱式布局) ................................ ................................ ................................ ........................ 25 1. 概述 ................................ ................................ ................................ ................................ ....................... 25 2. 代码实例 ................................ ................................ ................................ ................................ .............. 27 1.5: GroupLayout(分组布局) (分组布局) (分组布局) ................................ ................................ ................................ ................. 29 1. 概述 ................................ ................................ ................................ ................................ ....................... 29 2. 代码实例 ................................ ................................ ................................ ................................ .............. 31 1.6: CardLayout(卡片布局) (卡片布局) (卡片布局) ................................ ................................ ................................ .................... 36 1. 概述 ................................ ................................ ................................ ................................ ....................... 36 2. 代码实例 ................................ ................................ ................................ ................................ .............. 37 1.7: BorderLayout(边界布局) (边界布局) (边界布局) ................................ ................................ ................................ ................ 40 1. 概述 ................................ ................................ ................................ ................................ ....................... 40 2. 代码实例 ................................ ................................ ................................ ................................ .............. 41 1.8: SpringLayout(弹性布局) (弹性布局) (弹性布局) ................................ ................................ ................................ ................ 43 1. 概述 ................................ ................................ ................................ ................................ ....................... 43 2. 代码实例 ................................ ................................ ................................ ................................ .............. 47 1.9: null(绝对布局) (绝对布局) (绝对布局) ................................ ................................ ................................ ................................ .... 52 1. 概述 ................................ ................................ ................................ ................................ ....................... 52 2. 代码实例 ................................ ................................ ................................ ................................ .............. 53 2.1: JLabel(标签) (标签) (标签) ................................ ................................ ................................ ................................ ....... 56 1. 概述 ................................ ................................ ................................ ................................ ....................... 56 2. 代码实例 ................................ ................................ ................................ ................................ .............. 61 2.2: JButton(按钮) (按钮) (按钮) ................................ ................................ ................................ ................................ ..... 64 1. 概述 ................................ ................................ ................................ ................................ ....................... 64 2. 代码实例 : 默认按钮 默认按钮 ................................ ................................ ................................ ........................ 66 3. 代码实例 : 自定义图片按钮 自定义图片按钮 自定义图片按钮 自定义图片按钮 ................................ ................................ ................................ .......... 68 2.3: JRadioButton(单选按钮) (单选按钮) (单选按钮) ................................ ................................ ................................ ................ 71 2 1. 概述 ................................ ................................ ................................ ................................ ....................... 71 2. 代码实例 ................................ ................................ ................................ ................................ .............. 73 2.4: JCheckBox(复选框) (复选框) (复选框) ................................ ................................ ................................ ......................... 75 1. 概述 ................................ ................................ ................................ ................................ ....................... 75 2. 代码实例 ................................ ................................ ................................ ................................ .............. 77 2.5: JToggleButton(开关按钮) (开关按钮) (开关按钮) ................................ ................................ ................................ .............. 80 1. 概述 ................................ ................................ ................................ ................................ ....................... 80 2. 代码实例 : 默认 的开关按钮 的开关按钮 的开关按钮 ................................ ................................ ................................ .......... 82 3. 代码实例 : 自定义图片开关 自定义图片开关 自定义图片开关 自定义图片开关 ................................ ................................ ................................ .......... 84 2.6: JTextField(文本框) (文本框) (文本框) ................................ ................................ ................................ ........................... 87 1. 概述 ................................ ................................ ................................ ................................ ....................... 87 2. 实例代码 ................................ ................................ ................................ ................................ .............. 91 2.7:PasswordField(密码框) (密码框) (密码框) ................................ ................................ ................................ ................... 93 1. 概述 ................................ ................................ ................................ ................................ ....................... 93 2. 代码实例 ................................ ................................ ................................ ................................ .............. 96 2.8: JTextArea(文本区域) (文本区域) (文本区域) (文本区域) ................................ ................................ ................................ ....................... 98 1. 概述 ................................ ................................ ................................ ................................ ....................... 98 2. 代码实例 ................................ ................................ ................................ ................................ ............ 103 2.9: JComboBox(下拉列表框) (下拉列表框) (下拉列表框) ................................ ................................ ................................ ............ 105 1. 概述 ................................ ................................ ................................ ................................ ..................... 105 2. 代码实例 ................................ ................................ ................................ ................................ ............ 107 2.10: JList(列 表框) 表框) ................................ ................................ ................................ ................................ .. 110 1. 概述 ................................ ................................ ................................ ................................ ..................... 110 2. 代码实例 ................................ ................................ ................................ ................................ ............ 113 2.11: JProgressBar(进度条) (进度条) (进度条) ................................ ................................ ................................ ................ 117 1. 概述 ................................ ................................ ................................ ................................ ..................... 117 2. 代码实例 ................................ ................................ ................................ ................................ ............ 119 2.12: JSlider(滑块) (滑块) (滑块) ................................ ................................ ................................ ................................ .. 123 1. 概述 ................................ ................................ ................................ ................................ ..................... 123 2. 代码实例 : 默认刻度值 默认刻度值 默认刻度值 ................................ ................................ ................................ .................. 126 3. 代码实例 : 自定义标签刻度值 自定义标签刻度值 自定义标签刻度值 自定义标签刻度值 ................................ ................................ ................................ .... 128 3.1: JPanel(面板) (面板) ................................ ................................ ................................ ................................ ..... 132 1. 概述 ................................ ................................ ................................ ................................ ..................... 132 2. 代码实例 ................................ ................................ ................................ ................................ ............ 133 3.2: JScrollPane(滚动面板) (滚动面板) (滚动面板) (滚动面板) ................................ ................................ ................................ ................. 135 1. 概述 ................................ ................................ ................................ ................................ ..................... 135 2. 代码实例 ................................ ................................ ................................ ................................ ............ 138 3.2: JScrollPane(滚动面板) (滚动面板) (滚动面板) (滚动面板) ................................ ................................ ................................ ................. 140 1. 概述 ................................ ................................ ................................ ................................ ..................... 140 2. 代码实例 ................................ ................................ ................................ ................................ ............ 142 3.4: JTabbedPane(选项卡面板) (选项卡面板) (选项卡面板) (选项卡面板) ................................ ................................ ................................ ......... 145 1. 概述 ................................ ................................ ................................ ................................ ..................... 145 2. 代码实例 ................................ ................................ ................................ ................................ ............ 149 3.5: JLayeredPane(层级面板) (层级面板) (层级面板) ................................ ................................ ................................ ............
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值