gpt4 book ai didi

java - 右对齐光标在 JTextPane 组件中未显示

转载 作者:行者123 更新时间:2023-11-30 04:32:21 30 4
gpt4 key购买 nike

我有一个 JTextPane 夹在 2 个 JLabels 之间 - 如果我将光标放在文本 Pane 的最左侧而不是右侧,是否有已知原因导致光标显示出来?

这是更好地演示我的意思的代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class Testing {
/**
* @param args
*/
public static void main(String[] args) {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel cp = new JPanel(new BorderLayout());
f.setContentPane(cp);

final SubPanel subPanel = new SubPanel();

cp.add(subPanel, BorderLayout.CENTER);

JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(new JLabel("Align"));
final JComboBox alignCB = new JComboBox(new String[] {"left", "centre", "right"});
alignCB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
subPanel.align((String) alignCB.getSelectedItem());
}
});
buttonPanel.add(alignCB);
buttonPanel.add(new JLabel("Justify"));
final JComboBox justifyCB = new JComboBox(new String[] {"left", "centre", "right"});
justifyCB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
subPanel.justify((String) justifyCB.getSelectedItem());
}
});
buttonPanel.add(justifyCB);

JTextField tf = new JTextField("TF");
tf.setBorder(null);
buttonPanel.add(tf);

cp.add(buttonPanel, BorderLayout.NORTH);

f.pack();
f.setSize(new Dimension(300,300));
f.setLocation(300, 300);
f.setVisible(true);

}

public static class SubPanel extends JPanel {
JPanel innerPanel = new JPanel(new GridBagLayout());
TextPaneWidget[] tps = new TextPaneWidget[3];

public SubPanel() {
// setBorder(BorderFactory.createLineBorder(Color.RED));
setBorder(null);
// innerPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
innerPanel.setBorder(null);

for (int i = 0; i < tps.length; i++) {
tps[i] = new TextPaneWidget();
}

int gridy = 0;
for (TextPaneWidget tp : tps) {
innerPanel.add(tp, new GridBagConstraints(0,gridy, 1,1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
gridy++;
}

setLayout(new GridBagLayout());

add(innerPanel, new GridBagConstraints(0,0, 1,1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
}

public void align(String alignment) {
System.out.println("Align: " + alignment);

int anchor = GridBagConstraints.CENTER;
if ("right".equals(alignment)) {
anchor = GridBagConstraints.EAST;
} else if ("left".equals(alignment)) {
anchor = GridBagConstraints.WEST;
}

GridBagLayout gbl = (GridBagLayout) getLayout();
gbl.setConstraints(innerPanel, new GridBagConstraints(0,0, 1,1, 1.0, 0.0, anchor, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));

revalidate();
repaint();
}

public void justify(String justification) {
System.out.println("Justify: " + justification);

for (TextPaneWidget tp : tps) {
tp.justify(justification);
}
}
}

public static class MyDocument extends DefaultStyledDocument {
@Override
public void insertString(int offset, String text, AttributeSet attributeSet) throws BadLocationException {
SimpleAttributeSet attrs = new SimpleAttributeSet(attributeSet);
StyleConstants.setForeground(attrs, Color.WHITE);
StyleConstants.setBackground(attrs, Color.RED);
super.insertString(offset, text, attrs);
}
}

public static class TextPaneWidget extends JPanel {
JTextPane tp = new JTextPane();
JLabel lSpace = new JLabel(" ");
JLabel rSpace = new JLabel(" ");

public TextPaneWidget() {
// setBorder(BorderFactory.createLineBorder(Color.GREEN));
setBorder(null);

Font font = new Font("monospaced", Font.BOLD, 13);
tp.setBorder(null);
tp.setDocument(new MyDocument());
tp.setFont(font);
tp.setText("Text");
tp.setOpaque(true);

setLayout(new GridBagLayout());

lSpace.setBackground(Color.MAGENTA);
lSpace.setOpaque(true);
lSpace.setBorder(null);
add(lSpace, new GridBagConstraints(0,0, 1,1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.VERTICAL, new Insets(0,0,0,0), 0, 0));

add(tp, new GridBagConstraints(1,0, 1,1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));

rSpace.setBackground(Color.MAGENTA);
rSpace.setOpaque(true);
rSpace.setBorder(null);
add(rSpace, new GridBagConstraints(2,0, 1,1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(0,0,0,0), 0, 0));

addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
tp.setCaretPosition((e.getX() < tp.getX()) ? 0 : tp.getText().length());
tp.requestFocusInWindow();
}
});

lSpace.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
tp.setCaretPosition(0);
tp.requestFocusInWindow();
}
});

rSpace.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
tp.setCaretPosition(tp.getText().length());
tp.requestFocusInWindow();
}
});

}

public void justify(String justification) {
double leftWeight = 0.5;
double rightWeight = 0.5;
if ("right".equals(justification)) {
leftWeight = 1.0;
rightWeight = 0.0;
} else if ("left".equals(justification)) {
leftWeight = 0.0;
rightWeight = 1.0;
}

GridBagLayout gbl = (GridBagLayout) getLayout();
gbl.setConstraints(lSpace, new GridBagConstraints(0,0, 1,1, leftWeight, 0.0, GridBagConstraints.EAST, GridBagConstraints.VERTICAL, new Insets(0,0,0,0), 0, 0));
gbl.setConstraints(rSpace, new GridBagConstraints(2,0, 1,1, rightWeight, 0.0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(0,0,0,0), 0, 0));
revalidate();
repaint();
}
}
}

最佳答案

我想我明白发生了什么。感谢您提供代码。

定义 JTextPane 时,默认边框是 3 像素的空边框。当光标位于最右侧位置时,此空边框为文本 Pane 光标提供了显示位置。光标位于最右边的位置,以便可以在一行字符的末尾输入字符。

当您定义空边框(与 0 像素空边框相同)时,当文本 Pane 光标位于最右侧位置时,就没有地方可以绘制它。

为了在最右边的位置看到光标,您必须定义一个至少有 1 个右像素的空边框。如果您希望它在视觉上更具吸引力,请包含 1 个左侧像素。

tp.setBorder(BorderFactory.createEmptyBorder(0,1,0,1));

您必须定义一个空边框,因为空边框是唯一不绘制的Border。绘制的 Border 将在最右侧位置的文本 Pane 光标上绘制。

因此,您需要使用具有至少一个右侧像素的空边框,以便 JTextPane 能够显示最右侧的光标。

编辑添加:

当您使用 GridBagLayout 时,像这样的方法可以减少添加组件时必须处理的参数数量。

protected void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight,
Insets insets, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}

关于java - 右对齐光标在 JTextPane 组件中未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14340851/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com