Swing - 如何混合 JTextField 和 JTextAreas 并具有相同的视觉外观?

发布于 2024-08-29 04:17:36 字数 1736 浏览 5 评论 0原文

我正在使用 miglayout 创建一个表单,其中有 JTextFields(短输入答案)和 JTextAreas(较长答案)。问题是双重的。

  1. 滚动窗格环绕文本区域周围的边框与文本字段的边框不匹配。
  2. 文本区域/文本字段的宽度和位置不同,导致它们无法正确对齐。

alt text http://grab.by/3O0V

从右/左更改为右/填充后: 替代文本 http://grab.by/3RMk 您可以看到边界已对齐,但仍然存在间隙。我尝试设置 novisualpadding 但这并没有解决它。

源代码:

package test2;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;

public class Test extends JPanel {

private static final int NUM_CHARACTERS_WIDTH = 20;
private static final int NUM_ROWS = 5;
public Test() {

    setLayout(new MigLayout(
            "wrap 2",
            // Align text labels on the so their right edge meets left edge of the text fields
            "[right][left]"
            ));

    add(new JLabel("Text field:"));
    add(new JTextField(NUM_CHARACTERS_WIDTH));

    add(new JLabel("No scrollpane text area:"));
    add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));

    add(new JLabel("Scrollpane text area:"));
    add(new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH)));

    add(new JLabel("Text field:"));
    add(new JTextField(NUM_CHARACTERS_WIDTH));

}


public static void main(String[] args) {
    JFrame frame = new JFrame("");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new Test();
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

}

在保持视觉和谐的同时混合和匹配 jtextfield 和 jtextareas 的首选方法是什么?我现在注意到,当焦点位于文本字段中时,文本字段周围有一个蓝色突出显示,而不是文本区域......视觉不连续的另一个来源。

I am using miglayout to create a form in which there are JTextFields (short input answers) as well as JTextAreas (Longer answers). The problem is twofold.

  1. The border placed around a Scrollpane wrapped text area does not match that of a Text Field.
  2. The width and placement of the textarea/textfield differ, causing them not to line up correctly.

alt text http://grab.by/3O0V

After changing from right/left to right/fill:
alt text http://grab.by/3RMk
You can see that the bounds line up, but that there are still gaps. I tried setting novisualpadding but this did not fix it.

Source code:

package test2;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;

public class Test extends JPanel {

private static final int NUM_CHARACTERS_WIDTH = 20;
private static final int NUM_ROWS = 5;
public Test() {

    setLayout(new MigLayout(
            "wrap 2",
            // Align text labels on the so their right edge meets left edge of the text fields
            "[right][left]"
            ));

    add(new JLabel("Text field:"));
    add(new JTextField(NUM_CHARACTERS_WIDTH));

    add(new JLabel("No scrollpane text area:"));
    add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));

    add(new JLabel("Scrollpane text area:"));
    add(new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH)));

    add(new JLabel("Text field:"));
    add(new JTextField(NUM_CHARACTERS_WIDTH));

}


public static void main(String[] args) {
    JFrame frame = new JFrame("");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new Test();
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

}

What's the preferred way to mix and match jtextfield and jtextareas, while still maintaining visual harmony? I notice now that the text field has a blue highlight around it when focus is in it, as opposed to the text area... another source of visual discontinuity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

祁梦 2024-09-05 04:17:36

我知道这个问题已经很老了,但是要让 TextArea 的边框与 TextField 的边框相匹配:

(myTextArea).setBorder(new JTextField().getBorder());

这应该为您的 TextArea 提供边框 code> 就像 TextField 周围的代码一样。

I know this question is pretty old, but to get the border for a TextArea to match that of a TextField:

(myTextArea).setBorder(new JTextField().getBorder());

That should give a border to your TextArea like the one around a TextField.

一指流沙 2024-09-05 04:17:36

不知道如何解决边框问题,但要解决布局情况,我只会使用 springlayout。 Springlayout 只是一种在 JPanel 中更好地布局元素的方法。您可以了解更多相关信息 Java Sun 教程

具体来说,您可以通过设置每个元素的北、南、西和东边界来使用它。为此,您必须首先将标签调用从添加中取出,以便可以命名每个标签。因此,不要:

add(new JLabel("Text field:"));

Do:

JLabel myLabelName = new JLabel("Text field:");
add(myLabelName);

对于每个元素(JLabels、JTextAreas 和 JTextField)。完成此操作后,您可以轻松设置布局。

Springlayout layout = new SpringLayout();
setLayout(layout);

然后,对于每个元素,您必须设置所需的任何边框。它们必须按照南、北、西、东的特定顺序。不过,如果您不愿意,则不必使用所有四个边框。以下是如何设置第一个文本区域(顶部的文本区域)的示例。

layout.putConstraint(SpringLayout.NORTH, FirstTextAreaName, 10, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, FirstTextAreaName, this.getWidth()/2, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.EAST, FirstTextAreaName, -10, SpringLayout.EAST, this);

此示例未设置文本区域的南边,但如果您确实想要,则必须将其设置为第一个。第一行将文本区域的北侧设置为距顶部 10 像素。设置其他区域时,您可以使用前一个(上方)区域的名称来代替此区域,并说它距前一个区域的南边 10 像素:

layout.putConstraint(SpringLayout.NORTH, SecondTextAreaName, 10, SpringLayout.SOUTH, FirstTextAreaName);

上例中的第二行将文本区域的东侧设置为从中间开始通过您的主面板。最后第三行将文本区域的东侧设置为距离主面板东侧 10 个像素。

Not sure how you can fix your border problem but to fix your layout situation I would just use springlayout. Springlayout is just a way to better layout your elements within the JPanel. You can find out more about it Java Sun Tutorial

Specifically you use it by setting where you want your North, South, West and East borders of each element. To do this you would have to first take your label calls out of the add so each one can be named. So instead of:

add(new JLabel("Text field:"));

Do:

JLabel myLabelName = new JLabel("Text field:");
add(myLabelName);

For each of your elements (JLabels, JTextAreas and JTextField). Once this is done you can easily set the layout.

Springlayout layout = new SpringLayout();
setLayout(layout);

Then for each of the elements you have to set any of the borders you want. They have to be in the specific order South, the North, West then East. Though you don't have to use all four borders if you don't want to. Here is an example on how to set your first text area, the one on the top.

layout.putConstraint(SpringLayout.NORTH, FirstTextAreaName, 10, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, FirstTextAreaName, this.getWidth()/2, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.EAST, FirstTextAreaName, -10, SpringLayout.EAST, this);

This example doesn't set the south of the text area but if you did want to it would have to be first. The first line sets the north side of the text area to be 10 pixels away from the top. When setting the other areas you but the previous (above) areas name instead of this and say it is 10 pixels away from the south of the previous one:

layout.putConstraint(SpringLayout.NORTH, SecondTextAreaName, 10, SpringLayout.SOUTH, FirstTextAreaName);

The second line in the above example sets the east side of the text area to start halfway through your main panel. The last, third, line sets the east side of the text area to be 10 pixels from the east side of your main panel.

够钟 2024-09-05 04:17:36

我知道 MiGLAyout(顺便说一句,我喜欢它)能够对视觉对齐与严格的像素对齐进行特殊处理。您可能会遇到这个问题...“al”单元标识符用于此目的,但我没有必要使用它,因此无法提供示例。可能值得下载 MiG 示例项目,看看它们是否有相同的对齐问题(我确信他们有与您类似的面板)。

无论如何,我们经常在同一面板中混合文本字段和区域,但不会遇到这种情况...我们确实必须将滚动窗格的边框设置为与文本字段的边框由 Noel Ang 建议。

另外,我们通常在添加每个组件时指定它们,而不是在布局构造函数中指定约束 - 不确定这是否有区别......

I know that MiGLAyout (which I love, BTW) has the ability to do special handling for visual alignment vs strict pixel alignment. You may be running into this... The 'al' unit identifier is used for this, but I haven't had to use it so can't provide examples. It would probably be worth downloading the MiG sample project and see if they have the same alignment issue (I'm sure they have panels similar to yours).

For what it's worth, we mix text fields and areas in the same panel quite frequently and don't run into this... We do have to set the border of the scroll pane to be the same as the border of the text field as suggested by Noel Ang.

Also, instead of specifying constraints in the layout constructor, we generally specify them as we add each component - not sure if that makes a difference or not...

一个人的旅程 2024-09-05 04:17:36

对于布局问题,请尝试使用 columnConstraints 值为 [right][fill] 而不是 [right][left]

对于另一个问题,这似乎是外观和感觉上的不一致。我在 Windows 中运行了你的代码,差异也存在,但不那么明显。我的建议是为文本字段和文本区域明确设置相同的边框。

setLayout(new MigLayout(
        "wrap 2",
        "[right][fill]"
        ));

JTextField textField;
JScrollPane scrollPane;

add(new JLabel("Text field:"));
textField = new JTextField(NUM_CHARACTERS_WIDTH);
textField.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
add(textField);

add(new JLabel("No scrollpane text area:"));
add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));

add(new JLabel("Scrollpane text area:"));
scrollPane = new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));
scrollPane.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
add(scrollPane);

add(new JLabel("Text field:"));
textField = new JTextField(NUM_CHARACTERS_WIDTH);
textField.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
add(textField);

相同边框

如果您无法让 MigLayout 对齐您的组件,请考虑使用 java.awt.GridBagLayout:

import static java.awt.GridBagConstraints.*;

setLayout( new GridBagLayout() );

GridBagConstraints leftCons = new GridBagConstraints();
leftCons.anchor = NORTHEAST;
leftCons.fill = NONE;
leftCons.weightx = 1.0;
leftCons.gridy = RELATIVE;
leftCons.gridx = 0;
leftCons.insets = new Insets( 4, 8, 4, 8 );

GridBagConstraints rightCons = new GridBagConstraints();
rightCons.anchor = NORTHWEST;
rightCons.fill = HORIZONTAL;
rightCons.weightx = 1.0;
rightCons.gridy = RELATIVE;
rightCons.gridx = 1;
rightCons.insets = leftCons.insets;

add(new JLabel("Text field:"), leftCons);
add(new JTextField(NUM_CHARACTERS_WIDTH), rightCons);

add(new JLabel("No scrollpane text area:"), leftCons);
add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH), rightCons);

add(new JLabel("Scrollpane text area:"), leftCons);
add(new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH)), rightCons);

add(new JLabel("Text field:"), leftCons);
add(new JTextField(NUM_CHARACTERS_WIDTH), rightCons);

For the layout problem, try a columnConstraints value of [right][fill] instead of [right][left].

For the other issue, this appears to be a look-and-feel inconsistency. I ran your code in Windows, and the differences are there too, but less flagrant. My suggestion would be to set identifical borders explicitly for text fields and text areas.

setLayout(new MigLayout(
        "wrap 2",
        "[right][fill]"
        ));

JTextField textField;
JScrollPane scrollPane;

add(new JLabel("Text field:"));
textField = new JTextField(NUM_CHARACTERS_WIDTH);
textField.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
add(textField);

add(new JLabel("No scrollpane text area:"));
add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));

add(new JLabel("Scrollpane text area:"));
scrollPane = new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));
scrollPane.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
add(scrollPane);

add(new JLabel("Text field:"));
textField = new JTextField(NUM_CHARACTERS_WIDTH);
textField.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
add(textField);

Identicial borders

If you can't get MigLayout to align your components, considering using java.awt.GridBagLayout:

import static java.awt.GridBagConstraints.*;

setLayout( new GridBagLayout() );

GridBagConstraints leftCons = new GridBagConstraints();
leftCons.anchor = NORTHEAST;
leftCons.fill = NONE;
leftCons.weightx = 1.0;
leftCons.gridy = RELATIVE;
leftCons.gridx = 0;
leftCons.insets = new Insets( 4, 8, 4, 8 );

GridBagConstraints rightCons = new GridBagConstraints();
rightCons.anchor = NORTHWEST;
rightCons.fill = HORIZONTAL;
rightCons.weightx = 1.0;
rightCons.gridy = RELATIVE;
rightCons.gridx = 1;
rightCons.insets = leftCons.insets;

add(new JLabel("Text field:"), leftCons);
add(new JTextField(NUM_CHARACTERS_WIDTH), rightCons);

add(new JLabel("No scrollpane text area:"), leftCons);
add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH), rightCons);

add(new JLabel("Scrollpane text area:"), leftCons);
add(new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH)), rightCons);

add(new JLabel("Text field:"), leftCons);
add(new JTextField(NUM_CHARACTERS_WIDTH), rightCons);
ゞ记忆︶ㄣ 2024-09-05 04:17:36

首先为屏幕截图+1。
既然您使用的是Mac,您尝试过Quaqua Look And Feel吗?它正确呈现文本框/区域。

First off +1 for screen shots.
Since you are using Mac, did you try Quaqua Look And Feel? It renders the textboxes/areas properly.

极致的悲 2024-09-05 04:17:36

答案是 MiG Layout 人员正在致力于下一个版本的修复

你好,

苹果有一个讨厌的习惯,就是默认补偿而不让开发者决定。在这种情况下,他们添加了边框,使其在视觉上更像 OS X。这应该是布局管理器的选择...

MigLayout 可以补偿这样的视觉边界,但它仅适用于 Windows XP 中的 JTabbedPane。但我不确定它在 OS X 中是否可以 100% 好。我得检查一下。我们不希望文本字段超出边界。

我已将其添加到下一版本的待办事项列表中。

The answer is that MiG Layout folks are working on a fix for their next version.

Hello,

Apple has a nasty habbit of compensating by default and not let the developer decide. This is such a case where they have added a border to make it more visually like OS X. This should be the choice of the layout manager...

MigLayout can compensate for visual bounds like this but it is only done for JTabbedPane in Windows XP. I'm not sure it can be done 100% good in OS X though. I'll have to check. We don't want the text field to just grow into the bounds.

I have added this to the todo list for the next version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文