⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textpanedemo.java

📁 Java与面向对象程序设计实验教学讲义.复数类的实现,复数类的复杂运算,身份证号码的分析,图形界面设计
💻 JAVA
字号:
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;

public class TextPaneDemo{
	
	private String newline = "\n";
	
	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		JFrame frame = new JFrame("Text Panel Test");
		TextPaneDemo d = new TextPaneDemo();
		frame.getContentPane().add(d.createTextPane());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}


  JTextPane createTextPane() {
        JTextPane textPane = new JTextPane();
        
        //定义Text Panel中以各种字体显示的文本。
        String[] initString =
                { "This is an editable JTextPane, ",		//regular
                  "another ",					//italic
                  "styled ",					//bold
                  "text ",					//small
                  "component, ",				//large
                  "which supports embedded components..." + newline,//regular
                  " " + newline,				//button
                  "...and embedded icons..." + newline,		//regular
                  " " 						//icon
                 };
		
		//定义了一些自定义字体名称。
        String[] initStyles = 
                { "regular", "italic", "bold", "small", "large",
                  "regular", "button", "regular", "icon"
                 };

        initStylesForTextPane(textPane); //定义各种字体。

        Document doc = textPane.getDocument(); //创建Text Panel的数据模型。

        //将initString中定义的各字符串以对应的字体在TextPanel中显示。
        try {
            for (int i=0; i < initString.length; i++) {
                doc.insertString(doc.getLength(), initString[i],
                                 textPane.getStyle(initStyles[i]));
            }
        } catch (BadLocationException ble) {
            System.err.println("Couldn't insert initial text.");
        }

        return textPane;
    }

    //定义一些字体。
    protected void initStylesForTextPane(JTextPane textPane) {
        //定义"regular"。
        Style def = StyleContext.getDefaultStyleContext().
                                        getStyle(StyleContext.DEFAULT_STYLE);
        Style regular = textPane.addStyle("regular", def);
        StyleConstants.setFontFamily(def, "SansSerif");

        //定义italic。
        Style s = textPane.addStyle("italic", regular);
        StyleConstants.setItalic(s, true);

        //定义bold。
        s = textPane.addStyle("bold", regular);
        StyleConstants.setBold(s, true);

        //定义bold。
        s = textPane.addStyle("bold", regular);
        StyleConstants.setFontSize(s, 10);

         //定义large。
        s = textPane.addStyle("large", regular);
        StyleConstants.setFontSize(s, 16);

        //定义icon。
        s = textPane.addStyle("icon", regular);
        StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
        StyleConstants.setIcon(s, new ImageIcon("Cat.gif"));

        //定义button。
        s = textPane.addStyle("button", regular);
        StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
        JButton button = new JButton(new ImageIcon("right.gif"));
        button.setMargin(new Insets(0,0,0,0));
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Toolkit.getDefaultToolkit().beep();
            }
        });
        StyleConstants.setComponent(s, button);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -