精华内容
下载资源
问答
  • JAVA TextPane用法

    2021-03-13 12:26:55
    StyledDocument styledDoc = new DefaultStyledDocument();//文档模型JTextPane textPane = new ...//创建具有指定文档模型的TextPane//或者JTextPane textPane = new JTextPane();//styledDoc = textPane.getStyle...

    StyledDocument styledDoc = new DefaultStyledDocument();//文档模型

    JTextPane textPane = new JTextPane(styledDoc);//创建具有指定文档模型的TextPane

    //或者JTextPane textPane = new JTextPane();

    //styledDoc = textPane.getStyledDocument(); // 获得textPane的Document

    textPane.setEditable(false);//设置不可编辑

    JScrollPane scrollPane = new JScrollPane(textPane);//可以给文字面板textPane添加滚条

    文档模型插入文字:

    styledDoc.insertString(int styledDoc.getLength(),String content, AttributeSet attrSet);

    //content为你要插入的内容

    //AttributeSet为属性集,包括字体样式(粗体斜体)、字号、前景颜色、背景颜色等

    SimpleAttributeSet attrSet = new SimpleAttributeSet();

    //属性集设置

    StyleConstants.setFontSize(attrSet,size); // 大小

    StyleConstants.setBold(attrSet,true); // 粗体

    StyleConstants.setItalic(attrSet, true); // 斜体

    StyleConstants.setUnderline(attrSet,true); // 下划线

    StyleConstants.setForeground(attrSet,color); // 前景颜色

    StyleConstants.setBackground(attrSet, backColor);//背景颜色

    //Color color; Color backColor;

    StyleConstants.setFontFamily(attrSet,fontName); //设置字体

    //String fontName 如“宋体”

    textPane.setText(null);//清空面板

    //插入文字用下述结构

    try {

    styledDoc.insertString(styledDoc.getLength(),content, attrSet);

    } catch (BadLocationException e) {

    e.printStackTrace();

    }

    展开全文
  • on the TextPane where the answer will show, it doesn't put more numbers in it just changes to the specified number. When I press 1 it shows 1 but when I press 2 it doesn't show 12, it shows 2. Is ...

    I'm trying to make a calculator. http://i.imgur.com/exQLj4m.png

    The user will press the number they would like to calculate followed by the operator all in one line e.g

    '1+1-2+5'

    then Java would convert that into something it can understand and get the answer.

    But when I try you use setText() on the TextPane where the answer will show, it doesn't put more numbers in it just changes to the specified number. When I press 1 it shows 1 but when I press 2 it doesn't show 12, it shows 2. Is there like an addText() method?

    Here's my code for the number 1 button.

    JButton btnNewButton = new JButton("1");

    btnNewButton.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent arg0) {

    answer.setText("1");

    }});

    解决方案

    Here's my code for the number 1 button.

    Don't create custom ActionListeners for every button. Use a generic listener. Something like:

    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.*;

    import javax.swing.border.*;

    public class CalculatorPanel extends JPanel

    {

    private JTextField display;

    public CalculatorPanel()

    {

    Action numberAction = new AbstractAction()

    {

    @Override

    public void actionPerformed(ActionEvent e)

    {

    display.setCaretPosition( display.getDocument().getLength() );

    display.replaceSelection(e.getActionCommand());

    }

    };

    setLayout( new BorderLayout() );

    display = new JTextField();

    display.setEditable( false );

    display.setHorizontalAlignment(JTextField.RIGHT);

    add(display, BorderLayout.NORTH);

    JPanel buttonPanel = new JPanel();

    buttonPanel.setLayout( new GridLayout(0, 5) );

    add(buttonPanel, BorderLayout.CENTER);

    for (int i = 0; i < 10; i++)

    {

    String text = String.valueOf(i);

    JButton button = new JButton( text );

    button.addActionListener( numberAction );

    button.setBorder( new LineBorder(Color.BLACK) );

    button.setPreferredSize( new Dimension(50, 50) );

    buttonPanel.add( button );

    KeyStroke pressed = KeyStroke.getKeyStroke(text);

    InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

    inputMap.put(pressed, text);

    button.getActionMap().put(text, numberAction);

    }

    }

    private static void createAndShowUI()

    {

    // UIManager.put("Button.margin", new Insets(10, 10, 10, 10) );

    JFrame frame = new JFrame("Calculator Panel");

    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    frame.add( new CalculatorPanel() );

    frame.pack();

    frame.setLocationRelativeTo( null );

    frame.setVisible(true);

    }

    public static void main(String[] args)

    {

    EventQueue.invokeLater(new Runnable()

    {

    public void run()

    {

    createAndShowUI();

    }

    });

    }

    }

    The above code also shows how you can append the text to a text component.

    展开全文
  • 普通文本框和文本域设置不了字体颜色,需要用TextPane 文本窗格继承自编辑器窗格 public class JTextPane extends JEditorPane 样例: MutableAttributeSet attrRed = new SimpleAttributeSet(); StyleConstants....

     

    普通文本框和文本域设置不了字体颜色,需要用TextPane

    文本窗格继承自编辑器窗格

    public class JTextPane extends JEditorPane

     

    样例:

    MutableAttributeSet attrRed = new SimpleAttributeSet();
    StyleConstants.setForeground(attrRed, Color.RED);
    this.jTextPane1.getDocument().insertString(this.jTextPane1.getDocument().getEndPosition().getOffset()-1,"\n正在比较……\n",attrRed);

     

    参考:

    Swing对JTextPane中字体颜色的设置

    http://www.blogjava.net/Swing/archive/2007/07/09/128965.html

     

    展开全文
  • import javax.swing.JTextPane; import javax.swing.text.AbstractDocument; import javax.swing.text.BoxView; import javax.swing.text.ComponentView; import javax.swing.text.Element;...import javax.swing.te....
    
    import javax.swing.JTextPane;
    import javax.swing.text.AbstractDocument;
    import javax.swing.text.BoxView;
    import javax.swing.text.ComponentView;
    import javax.swing.text.Element;
    import javax.swing.text.IconView;
    import javax.swing.text.LabelView;
    import javax.swing.text.ParagraphView;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledEditorKit;
    import javax.swing.text.View;
    import javax.swing.text.ViewFactory;
    
    /**
     * 针对Bug  JTextPane中当输入较长的英文无空格时,会认为这是一个单词,并且自动换行失效,导致JTextPane的实际宽度变大出现滚动条
     * 重写JTextPane的方法,来控制这个Bug
     */
    public class MyTextPane extends JTextPane {
    
    	private static final long serialVersionUID = 1L;
    
    	public MyTextPane() {
    		super();
    		this.setEditorKit(new WarpEditorKit());
    	}
    	
    	//内部类---以下内部类全都用于实现自动强制折行
    	@SuppressWarnings("serial")
    	private class WarpEditorKit extends StyledEditorKit {
     
    		private ViewFactory defaultFactory = new WarpColumnFactory();
     
    		@Override
    		public ViewFactory getViewFactory() {
    			return defaultFactory;
    		}
    	}
    	
    	//内部类
    	private class WarpColumnFactory implements ViewFactory {
    		public View create(Element elem) {
    			String kind = elem.getName();
    			if (kind != null) {
    				if (kind.equals(AbstractDocument.ContentElementName)) {
    					return new WarpLabelView(elem);
    				} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
    					return new ParagraphView(elem);
    				} else if (kind.equals(AbstractDocument.SectionElementName)) {
    					return new BoxView(elem, View.Y_AXIS);
    				} else if (kind.equals(StyleConstants.ComponentElementName)) {
    					return new ComponentView(elem);
    				} else if (kind.equals(StyleConstants.IconElementName)) {
    					return new IconView(elem);
    				}
    			}
    			return new LabelView(elem);
    		}
    	}
     
    	private class WarpLabelView extends LabelView {
    		public WarpLabelView(Element elem) {
    			super(elem);
    		}
    		@Override
    		public float getMinimumSpan(int axis) {
    			switch (axis) {
    				case View.X_AXIS:
    					return 0;
    				case View.Y_AXIS:
    					return super.getMinimumSpan(axis);
    				default:
    					throw new IllegalArgumentException("Invalid axis: " + axis);
    			}
    		}
    	}
    }

     

    展开全文
  • //: c13:TextPane.java// From 'Thinking in Java, 2nd ed.' by Bruce Eckel// ...
  • 设置文本窗格自动滚到到最后,初始化时设置一次即可。 //设置文本窗格自动滚到到最后 DefaultCaret caret = (DefaultCaret)this.jTextPane1.getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);...
  • SqlTextPane.java

    2021-03-23 08:05:54
    import javax.swing.*;import javax.swing.text.BadLocationException;import javax.swing.text.DefaultStyledDocument;import javax.swing.text.Element;import javax.swing.text.MutableAttributeSet;...
  • 使用了JEditorPane组件 **/ public class WebBrowser extends JFrame implements HyperlinkListener, PropertyChangeListener { /**下面是使用的Swing组件**/ // 显示HTML的面板 JEditorPane textPane; // 最底下的...
  • import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.text.*; //简单的文本编辑器 public class Editor extends ... public JTextPane textPane...
  • textPane.setText("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\n"); JScrollPane scrollPane = new JScrollPane(textPane); frame.add(scrollPane, BorderLayout.CENTER);// Highlight some text ...
  • //简单的文本编辑器62616964757a686964616fe4b893e5b19e31333332643234 public class EditorDemo extends JFrame { JTextPane textPane = new JTextPane(); //文本窗格,编辑窗口 JLabel statusBar = new JLabel();...
  • JTextPane背景色

    2021-07-17 03:08:07
    I am making a text-based game where the user inputs text to solve the ... I decided to use java swing to display the text, and I want to have the background of the textPane to be black. I have tried...
  • JTextPane使用笔记

    2021-03-06 02:42:19
    JTextPane textPane = newJTextPane(); left_top_Pane.add(newJScrollPane(textPane), BorderLayout.CENTER); 2.插入图片 JTextPane提供insertIcon(Icon icon)方法,可直接调用,由于ImageIcon实现了Icon接口,...
  • 这将打印出两种不同颜色的“BLAH BLEG”.public class Main {public static void main(String[] args) {JTextPane textPane = new JTextPane();StyledDocument doc = textPane.getStyledDocument();Style style = ...
  • private JTextPane textPane = new JTextPane(); private boolean debug = false; private File currentFile; /** Listener for the edits on the current document. */ protected UndoableEditListener ...
  • import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JP...
  • 监听textarea换行

    2021-12-25 19:54:28
    监听
  • I have a JTextPane (or JEditorPane, I can use either no problem). How can I change the font of a selected area to a specific font?textpane.getSelectedText().setFont() won't work. (Even with font-fa...
  • 此组件使用 EditorKit 的实现来完成其操作。对于给予它的各种内容,它能有效地将其形态变换为适当的文本编辑器种类。该编辑器在任意给定时间的内容类型都由当前已安装的 EditorKit 确定。如果将内容设置为新的 URL,...
  • 1、智能家庭项目 智能家庭项目: 智能家庭包括各种设备,闹钟、咖啡机、电视机、窗帘 等 主人要看电视时,各个设备可以协同工作,自动完成看电视的准备工作,比如流程为: 闹铃响起 --> 咖啡机开始做咖啡 -->...
  • import java。awt。BorderLayout;import javax。swing。JPanel;import javax。swing。JFrame;import javax。swing。JTextField;...public class Test extends JFrame {private JPanel jContentPane ...
  • 用Java实现文本编辑器

    万次阅读 多人点赞 2018-02-14 10:25:06
    源码里有注释:import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.text.*; ...//简单的文本编辑器 ...public class Editor ... public JTextPane textPane = new...
  • 关于java文本框中添加滚动条

    千次阅读 2018-03-21 23:05:11
    JScrollPane jsp=new JScrollPane(textPane); jsp.setBounds(14, 13, 309, 405); textPane.setEditable(false); contentPane.add(jsp); 这里JTextPane类是Component的曾孙子类,所以可以作为构造函数的参数 ...
  • getDocument

    2021-03-08 09:00:05
    textArea.getDocument().addUndoableEditListener疑问textArea.getDocument().addUndoableEditListener疑问classTestextendsJFrame{finalUndoManagerundo=newUndoManager();UndoableEditListenerundoHandler=...
  • 设计模式练习(15)——中介者模式

    千次阅读 2017-01-20 15:59:16
    中介者模式 一、题目:某软件公司需要开发一套图形用户界面类库,该类库包含若干个预定义的窗格对象,例如TextPane,ListPane,GraphicPane,窗口之间不允许直接调用,具有该类库的应用由一个包含一组窗格的窗口...
  • 我怀疑这是附加文本的建议方法。这意味着每次您更改一些文本,您需要重新解析整个文档。...一个简单的例子可能是:JTextPane textPane = new JTextPane();textPane.setText( "original text" );StyledDocument ...
  • JTextPane设置居中对其的方法

    千次阅读 2017-08-22 17:13:56
    StyledDocument doc = textPane.getStyledDocument(); doc.setCharacterAttributes(105, doc.getLength()-105, aSet, false); doc.setParagraphAttributes(0, 104, bSet, false); JFrame f = new JFrame();...
  • 前言 在swing界面开发的时候,为了给我们的界面改变的更加美观,所以要给界面添加背景图片,这里我总结了一个demo进行操作这些元素,包括背景图片的添加,JtextPane添加背景图片不覆盖文字,还有Swing界面启动的时候...
  • 展开全部这是一个java计算器案例 在最下面的倒数第七行有这个从右边显示的方法e69da5e887aa62616964757a686964616f31333236373839import javax.swing.*;import javax.swing.event.*;import java.awt.*;...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 636
精华内容 254
关键字:

textPane