标签:java界面编辑 jtextpane setborder
本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020
JComponent有一个setBorder()的方法,它允许为任何可视组件设置各种边框。下面的例子使用showBorder()方法演示了一些可用的边框。此方法先创建了一个JPanel,然后设置相应的边框,此外并将边框的名称显示在JLabel中。
public class Borders extends JFrame { private static JPanel showBorder(Border b){ JPanel jp = new JPanel(); jp.setLayout(new BorderLayout()); String nm = b.getClass().toString(); nm = nm.substring(nm.lastIndexOf('.')+1); jp.add(new JLabel(nm,JLabel.CENTER),BorderLayout.CENTER); jp.setBorder(b); return jp; } public Borders() { // TODO Auto-generated constructor stub setLayout(new GridLayout(2,4)); setSize(500,300); setVisible(true); add(showBorder(new TitledBorder("Title"))); add(showBorder(new EtchedBorder())); add(showBorder(new LineBorder(Color.BLUE))); add(showBorder(new MatteBorder(5, 5, 30, 30, Color.GREEN))); add(showBorder(new BevelBorder(BevelBorder.RAISED))); add(showBorder(new SoftBevelBorder(BevelBorder.LOWERED))); add(showBorder(new CompoundBorder(new EtchedBorder(), new LineBorder(Color.RED)))); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new Borders(); } }运行结果:
这里的编辑器指的是JTextPanel组件,它可以毫不费事地支持许多编辑操作,下面的例子是对这个组件的简单应用,单击按钮,将定义好的文本显示在JTextPanel中。
public class TextPane extends JFrame { private JButton b = new JButton("Add Text"); private JTextPane tp = new JTextPane(); public TextPane() { // TODO Auto-generated constructor stub setVisible(true); setSize(500,300); add(new JScrollPane(tp)); add(BorderLayout.SOUTH,b); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub for(int i = 0;i < 10;i++){ tp.setText(tp.getText() + "this is test string"+i+"\n"); } } }); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new TextPane(); } }运行结果:
另外,JTextPane还有诸如自动换行这样的内置功能以及其他的功能,可以参考JDK文档。
标签:java界面编辑 jtextpane setborder
原文地址:http://blog.csdn.net/jesson20121020/article/details/41152103