我想将文本添加到JTextArea,并在垂直方向上有一个自动滚动条.
但是当在地理位置打字时,我想要一个自动新行,当行中没有空格时.如果我只使用JTextArea就可以,但是当我把它放在JScrollPane中时,它在需要时不会换行.
我怎样才能做到这一点?
谢谢!
我有一个JTextPane,我想添加行,根据他们的内容,他们有不同的格式.
目前我有这个
StyleContext context = new StyleContext();
StyledDocument document = new DefaultStyledDocument(context);
Style styleBold = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setBold(styleBold, true);
StyleConstants.setFontSize(styleBold, 18);
Style styleNorm = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setFontSize(styleNorm, 15);
for (int i = 0; i < temp.size(); i++) {
String tmp = temp.get(i);
if (tmp.substring(0, 2).equals(COMMENT_PREFIX)) {
String addThis = " - " + tmp.substring(2);
try {
document.insertString(document.getLength(), addThis,
styleNorm);
} //CATCH
} else if (tmp.substring(0, 2).equals(VERSION_PREFIX)) {
Date d = new Date(System.currentTimeMillis());
String addThis = "Version: " + tmp.substring(2) + " - …Run Code Online (Sandbox Code Playgroud) 我有以下代码块:
private void saveAs()
{
CDocument currentDocument=this.panelMain().openedDocuments().get(this.panelMain().openedDocuments().size()-1);
StyledDocument contents=currentDocument.getStyledDocument();
DefaultEditorKit kit=new DefaultEditorKit();
JFileChooser chooserSaveAs=new JFileChooser();
chooserSaveAs.setDialogTitle("Save as ...");
if(chooserSaveAs.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
{
String strNewFilename=chooserSaveAs.getSelectedFile().getName();
BufferedOutputStream out;
try
{
out=new BufferedOutputStream(new FileOutputStream(strNewFilename));
kit.write(out,
contents,
contents.getStartPosition().getOffset(),
contents.getLength());
out.close();
}
catch(IOException | BadLocationException ex)
{
Logger.getLogger(CFrameMain.class.getName()).log(Level.SEVERE,
null,
ex);
}
}
}
Run Code Online (Sandbox Code Playgroud)
一旦执行,此代码不会生成任何异常,但我无法在任何地方找到磁盘上保存的文件(我使用Total Commander搜索本地磁盘).为什么没有生成文件?我目前正在使用Windows 7 Ultimate,我试图保存到已登录用户的桌面(因为可能存在访问冲突问题......)?
我知道这个问题已经问过几次了。我试图从另一个类刷新我的JTextPane,当我从主类中刷新它时,它可以工作,但其他任何类都不能。我听说您需要使用线程来执行此操作,但是我尚未真正看到有人实现它。我在下面尽可能简单地提供了我的代码。有3个班级。谢谢!
以下是其外观的输出:http : //imgur.com/fQVDFPk
______________________________________________________
public class frame extends JPanel implements Runnable {
public TextBox textbox = new TextBox();
Thread t1 = new Thread(new TextBox());
public void run(){
}
public frame(){
t1.start();
textField();
}
public void textField(){
add(textbox.sp);
}
}
___________________________________________________
//bunch of imports
public class TextBox implements Runnable {
JTextPane field = new JTextPane();
JScrollPane sp = new JScrollPane(field);
String name ="This is your message!!\n\n Message \n\n Message";
public void run(){
try{
Thread.sleep(2000);
System.out.println("hello thread");
}catch(Exception e){}
} …Run Code Online (Sandbox Code Playgroud) 我想根据面板的大小设置 JTextPane 的大小,以便当我添加其他面板时,它会相应地改变。但它只是在中心提供一个小的文本窗格,当我添加一些文本时,它的大小会相应地改变。
JPanel panel = new JPanel();
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane();
pane.add(txt);
panel.add(pane,BorderLayout.CENTER);
add(pane);
Run Code Online (Sandbox Code Playgroud)
现在 jtextpane 就像一个小盒子一样出现在屏幕的中央。我希望它根据面板的大小出现
我目前有一个JLabel嵌入式JTextPane使用这个:
import javax.swing.*;
import javax.swing.text.*;
public class MainFrame
{
JFrame mainFrame = new JFrame("Main Frame");
JTextPane textPane = new JTextPane();
public MainFrame()
{
String[] components = {"Title", "\n"};
String[] styles = {"LABEL_ALIGN", "LEFT_ALIGN"};
StyledDocument sd = textPane.getStyledDocument();
Style DEFAULT_STYLE = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style LEFT_STYLE = sd.addStyle("LEFT_ALIGN", DEFAULT_STYLE);
StyleConstants.setAlignment(LEFT_STYLE, StyleConstants.ALIGN_LEFT);
Style CENTER_STYLE = sd.addStyle("CENTER_ALIGN", DEFAULT_STYLE);
StyleConstants.setAlignment(CENTER_STYLE, StyleConstants.ALIGN_CENTER);
JLabel titleLbl = new JLabel("Title");
Style LABEL_STYLE = sd.addStyle("LABEL_ALIGN", DEFAULT_STYLE);
StyleConstants.setAlignment(LABEL_STYLE, StyleConstants.ALIGN_CENTER);
StyleConstants.setComponent(LABEL_STYLE, titleLbl);
for(int i = 0; i < components.length; …Run Code Online (Sandbox Code Playgroud) 我的程序用各种消息填充 JTextPane,有些将 JTextPane 扩展到 2 行以适合文本,有些则仅一行。我希望窗格始终是两行。有没有办法返回 JTextPane 中的文本当前占用的行数?
使用定制颜色后:
Color bg = new Color(0f,0f,0f,0.5f);
Run Code Online (Sandbox Code Playgroud)
对于JTextPane背景我可以看到背景JTextPane显示的背景部分Jlabel包括JTextPane在其中.
发生了什么:
所以JTextPane背景的底部部分是可以的,但是文本背后的顶部部分有一些问题.
我该如何解决?我是否因使用自定义颜色的简单透明背景而犯了错误JTextPane?
该部分计划的代码:
t = new JTextPane();
SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setAlignment(style , StyleConstants.ALIGN_CENTER);
StyleConstants.setForeground(style , Color.white);
StyleConstants.setFontFamily(style, "Times new Roman");
StyleConstants.setFontSize(style, 20);
StyleConstants.setBold(style, true);
t.setParagraphAttributes(style,true);
t.setText(" " + text.getT1().get(0).toUpperCase());
t.setOpaque(true);
Color bg = new Color(0f,0f,0f,0.5f);
t.setBackground(bg);
t.setEditable(false);
t.setBounds(250, 400, 300, 50);
animation.add(t);
Run Code Online (Sandbox Code Playgroud)