![在此处输入图像说明] [1]我有以下结构
JFrame
JPanel
JScrollPane
class that extends JTextPane
添加长文本后,JScrollPane会隐藏JTextPane。
我正在使用Gridbag布局。
我在clss中有DefaultStyledDocument,它扩展了JTextPane。
我已使用
为JTextPane设置了最大大小text.setMaximumSize(new Dimension(100,50));
package com.example;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
public class Test extends JFrame{
IPanel p;
public Test() {
p = new IPanel();
p.init();
setTitle("Test");
setBounds(50, 50, 200, 200);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(p);
setVisible(true);
}
public static void main(String[] args) {
Test t = new Test();
}
}
class IPanel extends JPanel{
public void init() {
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gridbag = new GridBagLayout();
JLabel subjectLabel = new JLabel("Text");
gbc.weighty = 0.0;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.anchor = GridBagConstraints.NORTH;
gridbag.setConstraints(subjectLabel, gbc);
add(subjectLabel);
TextPane Text = new TextPane(255);
Text.setMaximumSize(new Dimension(100,20));
Text.setPreferredSize(new Dimension(100,20));
gridbag.setConstraints(Text, gbc);
JScrollPane subScrollPane = new JScrollPane(Text);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.weighty = 0.0;
gbc.insets = new Insets(0, 0, 5, 0);
gridbag.setConstraints(subScrollPane, gbc);
subScrollPane.setWheelScrollingEnabled(false);
add(subScrollPane);
}
}
class TextPane extends JTextPane{
private Doc doc = new Doc();
public TextPane(int lenght) {
doc.setmaxLength(lenght);
}
}
class Doc extends DefaultStyledDocument{
private int maxLength = 0;
public void setmaxLength(int lenght) {
maxLength = lenght;
}
}
这是代码示例
答案 0 :(得分:3)
只需使用EmptyBorder。看一下这个例子,看看你可以做些什么呢?
import java.awt.*;
import javax.swing.*;
public class ScrollPaneExample
{
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("JScrollPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
JTextPane textPane = new JTextPane();
textPane.setText("IF this is a very large text" +
" still it can manage that" +
" without any difficulty since" +
" it knows how to handle itself.");
textPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JScrollPane scroller = new JScrollPane();
scroller.setViewportView(textPane);
contentPane.add(scroller);
frame.setContentPane(contentPane);
frame.setSize(100, 200);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ScrollPaneExample().createAndDisplayGUI();
}
});
}
}
答案 1 :(得分:2)
以下是一个完全相同的层次结构,相同的布局以及没有问题的所有内容的示例:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
public class Test3 {
protected static void initUI() {
JFrame frame = new JFrame("Test text pane");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane text = new JTextPane();
text.setText(LONG_TEXT);
JScrollPane scrollPane = new JScrollPane(text);
JPanel panel = new JPanel(new GridBagLayout());
panel.add(scrollPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
0, 0, 0, 0), 0, 0));
frame.add(panel);
frame.setSize(600, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initUI();
}
});
}
private static final String LONG_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In molestie purus vel ipsum commodo id bibendum est porta. Proin leo nisl, facilisis eu euismod eget, fermentum a lacus. Nulla pulvinar ante at elit tristique condimentum. Suspendisse ac erat eu lectus vulputate sagittis sed a urna. In diam ligula, blandit id scelerisque sit amet, porttitor in libero. Curabitur in rhoncus tellus. Aenean dignissim dui semper ante pharetra interdum scelerisque vel tortor. Vestibulum id arcu mauris. Integer ut magna purus. Mauris orci massa, porta sollicitudin rutrum eu, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sem justo, dapibus in euismod et, gravida sit amet eros.\r\n"
+ "\r\n"
+ "Suspendisse id ligula sit amet neque euismod commodo in at massa. Sed pretium lobortis vulputate. Aliquam non eros odio, non accumsan nisi. Phasellus vel nisl tellus, quis tincidunt risus. Donec ut nulla felis. Morbi sit amet enim arcu. Integer iaculis mi id sem vulputate non lacinia dui dictum.\r\n"
+ "\r\n"
+ "Praesent luctus neque in purus convallis vel aliquet dui rutrum. In lobortis nisl eu nisi tincidunt adipiscing sodales sapien sodales. Morbi augue massa, dictum vel hendrerit vel, tincidunt id lectus. Proin vulputate nunc placerat ante fringilla vitae ullamcorper mauris fringilla. Duis et massa sapien. Integer porta elementum enim in lobortis. Morbi eget mi nunc. Quisque nec arcu eget orci elementum consectetur ut in metus.\r\n"
+ "\r\n"
+ "Donec iaculis felis vel felis rutrum id congue tellus aliquet. Ut vel sapien mauris, quis sodales odio. Aliquam cursus urna a leo lacinia eget porttitor ante mattis. Aenean dictum, tellus in volutpat luctus, magna eros feugiat tortor, nec lobortis diam justo vel est. Praesent sit amet nisl sed eros semper ultrices. Nunc lobortis scelerisque sapien, ac sodales nibh viverra ac. Etiam auctor, odio ut venenatis hendrerit, odio ipsum sollicitudin libero, non adipiscing nisi eros at nulla. Nulla consequat arcu in libero pellentesque sit amet aliquet arcu elementum. Nulla facilisi. Proin sodales neque quis turpis gravida ac mollis ipsum ultricies. Mauris lorem magna, ultricies a dignissim sit amet, egestas et neque. Phasellus iaculis pellentesque ultrices. Phasellus a consectetur nisl. Proin ac quam id lectus dignissim lacinia. Praesent ligula purus, mattis eu pellentesque nec, convallis id lorem.\r\n"
+ "\r\n"
+ "Cras at sapien vitae felis fermentum consectetur sed eu justo. Mauris in felis mi. Vestibulum felis neque, varius at aliquet non, ornare at eros. In hac habitasse platea dictumst. Vivamus placerat, tellus laoreet pulvinar tempor, velit elit aliquet diam, eu ullamcorper purus enim non lectus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Etiam et enim magna. Ut varius, lectus at luctus laoreet, urna mauris euismod velit, vitae sodales nulla nulla non sem. Phasellus eget viverra erat. Phasellus quam ante, vehicula sed tristique vestibulum, ullamcorper et nunc. Phasellus placerat congue tellus, condimentum volutpat ligula convallis fringilla. Curabitur quis nibh lorem, ut feugiat velit. Aliquam vehicula pellentesque aliquet. ";
}
编辑:
这两行代码存在问题:
Text.setMaximumSize(new Dimension(100, 20));
Text.setPreferredSize(new Dimension(100, 20));
您正在强制JTextPane的首选大小和最大大小。您提供非常严格的约束。通常,滚动条的宽度/高度约为20px,这意味着当水平滚动条出现时,它将占据组件的大部分高度。
还要注意代码中的以下错误:
init()
方法中声明了GridBagLayout,但未在面板上设置它(使用setLayout
),这意味着它是实际执行布局的FlowLayout
。 我不知道你想要达到的目标。对我来说,它看起来像一个简单的JTextField
就够了,但我没有足够的背景知道。
EDIT2:
gridbag
。为滚动窗格约束添加以下约束提示:
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1.0;