java中JTextPane 怎么把不同的行,设置为不同的对齐方式,我尝试了一下,怎么都是左对齐呀! 10

下面是一个很简单的测试:packageexecitive;importjava.awt.BorderLayout;importjava.awt.Container;imp... 下面是一个很简单的测试:

package execitive;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class Test3 {

private static String message = "In the beginning, there was COBOL, then there was FORTRAN, "
+ "then there was BASIC, ... and now there is Java.\n";

public static void main(String[] args) {
String title = (args.length == 0 ? "JTextPane Example" : args[0]);

JFrame frame = new JFrame(title);
Container content = frame.getContentPane();

StyleContext context = new StyleContext();
StyledDocument document = new DefaultStyledDocument(context);

Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);// 左对齐

try {
document.insertString(document.getLength(), message, style);
} catch (BadLocationException badLocationException) {
System.err.println("Oops");
}

SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setAlignment(attributes, StyleConstants.ALIGN_RIGHT);//右对齐
document.setParagraphAttributes(0, document.getLength(), style, true);
try {//为什么没有右对齐呀
document.insertString(document.getLength(), "Hello Java",
attributes);
} catch (BadLocationException badLocationException) {
}

JTextPane textarea = new JTextPane();
textarea.setEditable(false);
textarea.setBackground(UIManager.getColor("label.background"));
textarea.setFont(UIManager.getFont("label"));
textarea.setDocument(document);
JScrollPane textAreascrollPane = new JScrollPane(textarea);
content.add(textAreascrollPane, BorderLayout.CENTER);

JLabel AliasNameLabel = new JLabel("Local Name: ");
content.add(AliasNameLabel, BorderLayout.SOUTH);

frame.setSize(300, 150);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}

}
展开
 我来答
老冯文库
推荐于2018-03-14 · 知道合伙人软件行家
老冯文库
知道合伙人软件行家
采纳数:1139 获赞数:8734

向TA提问 私信TA
展开全部

修改方法:

在JTextPane中使用带HTML格式的文本就可实际各部分文本按照不同格式显示的效果。


实际效果:


修改后的代码:

import java.awt.BorderLayout;

import java.awt.Container;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JScrollPane;

import javax.swing.JTextPane;

import javax.swing.UIManager;

import javax.swing.text.BadLocationException;

import javax.swing.text.DefaultStyledDocument;

import javax.swing.text.SimpleAttributeSet;

import javax.swing.text.Style;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyleContext;

import javax.swing.text.StyledDocument;


public class Test3 {

//TODO 修改代码

private static String message = "<p align='left'>In the beginning, there was COBOL, </p>"

+ "<p align='center'>then there was FORTRAN, </p>"

+ "<p align='right'>then there was BASIC, ... </p>" 

+ "and now there is Java.\n";


public static void main(String[] args) {

String title = (args.length == 0 ? "JTextPane Example" : args[0]);


JFrame frame = new JFrame(title);

Container content = frame.getContentPane();


StyleContext context = new StyleContext();

StyledDocument document = new DefaultStyledDocument(context);


Style style = context.getStyle(StyleContext.DEFAULT_STYLE);

StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);// 左对齐


try {

document.insertString(document.getLength(), message, style);

}

catch (BadLocationException badLocationException) {

System.err.println("Oops");

}


SimpleAttributeSet attributes = new SimpleAttributeSet();

StyleConstants.setAlignment(attributes, StyleConstants.ALIGN_RIGHT);//右对齐

document.setParagraphAttributes(0, document.getLength(), style, true);

try {//为什么没有右对齐呀

document.insertString(document.getLength(), "Hello Java", 

attributes);

catch (BadLocationException badLocationException) {

}


JTextPane textarea = new JTextPane();

//TODO 新增代码开始

textarea.setContentType("text/html");

textarea.setText(message);

//TODO 新增代码结束


textarea.setEditable(false);

//TODO 被注释代码

/*

textarea.setBackground(UIManager.getColor("label.background"));

textarea.setFont(UIManager.getFont("label"));

textarea.setDocument(document);

*/


JScrollPane textAreascrollPane = new JScrollPane(textarea);

content.add(textAreascrollPane, BorderLayout.CENTER);

JLabel AliasNameLabel = new JLabel("Local Name: ");

content.add(AliasNameLabel, BorderLayout.SOUTH);

frame.setSize(300, 200);

frame.setVisible(true);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}

}


欢迎继续交流。

xiangjuan314
2016-04-25 · TA获得超过3.3万个赞
知道大有可为答主
回答量:2.9万
采纳率:0%
帮助的人:2872万
展开全部
参考
JTextArea多行文本,想设置文字的对齐方式为居中对齐,却发现死活没有合适有效的方法,又试了JTextField和JLabel,都不理想。搜了一溜够看到JTextPane的解决方法,其实真是有点儿大材小用了。JTextPane里可以设置style,对齐方式、字体字号、颜色等等,并且还可以设置style的作用范围,挺方便的。另外,想把文字区域背景设为透明的也是在API中找了一通,才发现敢情就是setOpacity,呃,对这个单词实在是太不敏感了。。。。

把在coderanch上搜的例子贴在这里吧,以防以后忘了:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class Styling
{
public Styling()
{
String text = "To refer to locations within the sequence, the " +
"coordinates used are the location between two " +
"characters./nAs the diagram below shows, a location " +
"in a text document can be referred to as a position, " +
"or an offset. This position is zero-based.";

SimpleAttributeSet aSet = new SimpleAttributeSet();
StyleConstants.setForeground(aSet, Color.blue);
StyleConstants.setBackground(aSet, Color.orange);
StyleConstants.setFontFamily(aSet, "lucida bright italic");
StyleConstants.setFontSize(aSet, 18);

SimpleAttributeSet bSet = new SimpleAttributeSet();
StyleConstants.setAlignment(bSet, StyleConstants.ALIGN_CENTER);
StyleConstants.setUnderline(bSet, true);
StyleConstants.setFontFamily(bSet, "lucida typewriter bold");
StyleConstants.setFontSize(bSet, 24);

JTextPane textPane = new JTextPane();
textPane.setText(text);
StyledDocument doc = textPane.getStyledDocument();
doc.setCharacterAttributes(105, doc.getLength()-105, aSet, false);
doc.setParagraphAttributes(0, 104, bSet, false);

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(textPane));
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}

public static void main(String[] args)
{
new Styling();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式