自定义秋千JTextComponent [英] Custom swing JTextComponent

查看:135
本文介绍了自定义秋千JTextComponent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个看起来像JTextArea的JTextArea,像JTextArea的行为,像JTextArea的响应,像JTextArea那样讲话,像JTextArea那样移动等等,但是不是JTextArea.

I want to create a JTextArea which looks like JTextArea, acts like JTextArea, responds like JTextArea, speaks like JTextArea, moves like JTextArea, etc, but is not JTextArea.

为了简短起见,我想基于JTextArea创建一个自定义的swing组件.一旦这样做,我将能够更改JTextArea的其他不同硬编码属性,并创建自己的自定义JTextArea.没有按照我需要的方式设计预定义的swing组件,但是JTextArea最接近,这就是我选择它的原因.

To make it short, I'd like to create a custom swing component based 100% on JTextArea. Once I do that, I'll be able to change different otherwise hard-coded properties of a JTextArea and make my own customised JTextArea. There are no predefined swing components that are designed the way I need them to be, but JTextArea is the closest, that why I choose it.

我想更改JTextArea的行之间的间距.不,我不想使用JtextPane,我试过了,它不适用于我的程序,它计算出的位置不同,看起来也不同,应用JtextArea边框只会使事情变得更糟.

I'd like to change the spacing inbetweem the rows of a JTextArea. And no, I don't want to use JtextPane, I've tried, it doesn't work with my program, it calcualtes it position diferently, it look diferently, and applying the JtextArea border just messes thing up further.

我不尝试扩展JTextArea,而是尝试创建自定义JTextArea,如

I'm not trying to extend the JTextArea, I'm trying to create a custom JTextArea, as in custom swing component, with changed hard-coded properties that are not configurable trought JTextAreas methods.

但是,我不知道该怎么做.我一直在Internet上查找它,但是只有大量的指导可以从stracth构建自己的组件...

However, I have no idea how to do it. I've been looking it up on the internet, but there is only an extensive guide about building your own component from stracth...

弄清楚这将花费很多时间,并不能真正解决我的问题.

Figuring that out will take a lot of time and will not really solve my problem.

我要做的只是创建一个类(或几个类),其中将包含构建JTextArea的所有内容.从JTextComponent级别开始,然后复制在创建JTextArea时使用的所有较低级别的类.我还要指出的是,我使用的是Nibus外观,我认为可能需要包含一些类才能使自定义JTextArea在该LAF下正常运行.

Only thing I have to do is create a class (or several classes) that will contain everyting that builds JTextArea. Start from JTextComponent level and copy all the lower level classes that are used in creating JTextArea. I'd also like to note that I use Nibus look and feel, I think that there may be some classes that would need to be included for the custom JTextArea to function properly under that LAF.

我研究了swing源代码,其中充满了一切.鉴于我对核心挥杆结构和力学一无所知,弄清楚在创建JTextArea中使用了哪些类或其部件将是一个耗时的噩梦.

I've looked into the swing source code, and it's full of everyting. Figuring out what classes or their parts are used in creating a JTextArea would be a time consuming nightmare, given that I have no knowledge about core swing structure and mechanics.

这就是为什么我要问一个有知识的人至少列出我复制JTextArea所需的类,然后再找出如何编写它们.

That's why I'm asking somebody who has the knowledge to at least list the classes that I need to replicate the JTextArea, and I'll then figure out how to compose them.

因为,如果我现在开始学习摆动核心力学,则需要花费数天和数周的时间才能弄清一切,但是对于一个知道的人,只需几分钟就可以列出我需要的所有课程吸引我的注意力.

Because, if I start learning now about swing core mechanics, it'll take days and weeks before I figure it all out, but for someone who knows, it would take only a couple of minutes to list all classes that I need to focus my attention onto.

我正在尝试在这里提供一个捷径.我不想完全理解秋千,我只是想让这个东西工作.默认间距是一个像素太低,我要做的就是将其提高一个像素.我不想知道画家是如何在屏幕上绘制组件的,我只是想知道它是从哪里调用的以及它本身是什么...

I'm trying to take a shortcut here. I don't want to fully understand swing, I just want this thing to work. Default spacing is one pixel too low, and all I want to do is just make it that pixel higher. I don't want to know how the painter paints component onto screen, I just want to know where is it called from and what does it call itself...

感谢所有花时间的人.

推荐答案

我想更改JTextArea行之间的间距

I'd like to change the spacing inbetweem the rows of a JTextArea

我的第一个想法是,覆盖javax.swing.JTextArea#getRowHeight就足够了. Javadoc明确指出

My first thought was that overriding javax.swing.JTextArea#getRowHeight would be sufficient. The javadoc clearly states

定义行高的含义.默认为字体的高度.

Defines the meaning of the height of a row. This defaults to the height of the font.

因此,我希望通过重写此方法,可以调整定义,并在行之间获得更大的间距. Bummer,没用.快速搜索JDK中该方法的用法后发现相同.它主要用于计算某些大小,但是在组件内部绘制文本时肯定不会使用.

So I was hoping that by overriding this method, you would adjust the definition and you would get more spacing between the rows. Bummer, didn't work. A quick search on the usages of that method in the JDK revealed the same. It is mainly used to calculate some sizes, but certainly not used when painting text inside the component.

通过查看javax.swing.text.PlainView#paint方法的源代码,我看到使用了FontMetrics,并且可以轻松地在JTextArea中覆盖它们.所以第二种方法是扩展JTextArea(哇,扩展了Swing组件,但这是概念验证)

By looking at the source code of the javax.swing.text.PlainView#paint method, I saw that the FontMetrics are used, and those you can easily override in the JTextArea. So second approach was to extend the JTextArea (bwah, extending Swing components but it is for a proof-of-concept)

  private static class JTextAreaWithExtendedRowHeight extends JTextArea{
    private JTextAreaWithExtendedRowHeight( int rows, int columns ) {
      super( rows, columns );
    }

    @Override
    public FontMetrics getFontMetrics( Font font ) {
      FontMetrics fontMetrics = super.getFontMetrics( font );
      return new FontMetricsWrapper( font, fontMetrics );
    }
  }

除了getHeight方法外,FontMetricsWrapper类基本上委派了所有内容.在那种方法中,我在委托结果中添加了10

The FontMetricsWrapper class basically delegates everything, except the getHeight method. In that method I added 10 to the result of the delegate

@Override
public int getHeight() {
  //use +10 to make the difference obvious
  return delegate.getHeight() + 10;
}

这会导致更多的行间距(插入符太长,但可能可以调整).

And this results in more row spacing (and a caret which is way too long, but that can probably be adjusted).

一些截图说明了这一点(虽然不如其他一些好,但是它表明这种方法可能有效):

A little screenshot to illustrate this (not as nice as some of the other ones, but it shows that this approach might work):

小的免责声明:这感觉像是丑陋的骇客,可能会导致意想不到的问题.我希望有人能提供更好的解决方案.

Small disclaimer: this feels like an ugly hack and might result in unexpected issues. I do hope somebody comes with a better solution.

这篇关于自定义秋千JTextComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆