java swing scrollpane设置为textpane

wz8daaqr  于 2023-04-19  发布在  Java
关注(0)|答案(2)|浏览(101)

下面是我的代码,添加一些swing组件到框架.我使用两个textpane和设置一些文本都.但文本是大的,只有textpane是可见的,当我运行code.so我试图添加scrollpane textpane ta2,但也没有发生.scrollpane不出现textpane ta2周围.什么是错误的

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;

public class Test1 {

    public Test1() {
        String a="vdnogregnroei dfnoj";
         JFrame frm = new JFrame("frontend");
         JLabel l1= new JLabel("Enter name of text file");
         JLabel l2= new JLabel("Enter name of text file");
         final JTextField t1=new JTextField(15);
         final JTextField t2=new JTextField(15);
         JTextPane ta2=new JTextPane();
         JLabel l3=new JLabel("SIMILARITY");
         JLabel  l4=new JLabel("DIFFERENCES");
          JTextPane ta1=new JTextPane();
          JScrollPane sp2=new JScrollPane(ta2);
          frm.getContentPane().add(sp2);
         JButton b1=new JButton("COMPARE");
         frm.setLayout(new GridBagLayout());
         Container cont=frm.getContentPane();
         GridBagConstraints cnt=new GridBagConstraints();
         cnt.fill=GridBagConstraints.HORIZONTAL;
         cnt.insets=new Insets(10,10,10,10);
         cnt.gridx=1;
         cnt.gridy=1;
         cont.add(l1,cnt);
         cnt.gridx=2;
         cnt.gridy=1;
         cont.add(t1,cnt);
         cnt.gridx=1;
         cnt.gridy=2;
         cont.add(l2,cnt);
         cnt.gridx=2;
         cnt.gridy=2;
         cont.add(t2,cnt);
         cnt.gridx=1;
         cnt.gridy=3;
         cont.add(l3,cnt);
         cnt.gridx=2;
         cnt.gridy=3;
         cont.add(ta1,cnt);
         cnt.gridx=1;
         cnt.gridy=4;
         cont.add(l4,cnt);
         cnt.gridx=2;
         cnt.gridy=4;
         cont.add(ta2,cnt);
         cnt.gridx=1;
         cnt.gridy=5;
         cont.add(b1,cnt);
         ta1.setContentType("text/html");
         ta1.setText("sbdiu sdjj<b>bjksd</b>"+a+"<br/>dnsaod<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>vsdnono");

         ta2.setContentType("text/html");
         ta2.setText("sbdiu sdjj<b>bjksd</b>"+a+"<br/>dnsaod<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>vsdnono");
         frm.pack();
         frm.setVisible(true);
    }
    public static void main(String[] args)
    {
        Test1 obj=new Test1();
    }

}
dbf7pr2w

dbf7pr2w1#

您需要将JTextPane添加到JScrollPane,然后将JScrollPane添加到JFrame(或其内容窗格):

public class Test1 {

    public Test1() {

        JFrame frm = new JFrame("frontend");

        JLabel l1 = new JLabel("Enter name of text file");
        JLabel l2 = new JLabel("Enter name of text file");
        JLabel l3 = new JLabel("SIMILARITY");
        JLabel l4 = new JLabel("DIFFERENCES");

        JTextField t1 = new JTextField(15);
        JTextField t2 = new JTextField(15);

        JTextPane ta1 = new JTextPane();
        JTextPane ta2 = new JTextPane();

        ta1.setContentType("text/html");
        ta2.setContentType("text/html");
        String a = "vdnogregnroei dfnoj";
        ta1.setText(a);
        ta2.setText(a);

        JScrollPane sp1 = new JScrollPane(ta1);
        JScrollPane sp2 = new JScrollPane(ta2);
        JButton b1 = new JButton("COMPARE");

        Container cont = frm.getContentPane();
        cont.setLayout(new GridBagLayout());
        GridBagConstraints cnt = new GridBagConstraints();

        cnt.fill = GridBagConstraints.HORIZONTAL;
        cnt.insets = new Insets(10, 10, 10, 10);
        cnt.gridx = 0;
        cnt.gridy = 0;
        cont.add(l1, cnt);

        cnt.gridx = 1;
        cnt.gridy = 0;
        cont.add(t1, cnt);

        cnt.gridx = 0;
        cnt.gridy = 1;
        cont.add(l2, cnt);

        cnt.gridx = 1;
        cnt.gridy = 1;
        cont.add(t2, cnt);

        cnt.gridx = 0;
        cnt.gridy = 2;
        cont.add(l3, cnt);

        cnt.gridx = 1;
        cnt.gridy = 2;
//      cnt.weightx = 1;
//      cnt.weighty = 1;
        cont.add(sp1, cnt);

        cnt.gridx = 0;
        cnt.gridy = 3;
//      cnt.weightx = 0;
//      cnt.weighty = 0;
        cont.add(l4, cnt);

        cnt.gridx = 1;
        cnt.gridy = 3;
        cont.add(sp2, cnt);

        cnt.gridx = 0;
        cnt.gridy = 4;
        cont.add(b1, cnt);

        frm.pack();
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }

    public static void main(String[] args) {

        new Test1();
    }
}

我建议你仔细看看这段代码和你的代码之间的区别,注意我在哪里设置了布局,添加了什么组件。

备注:

  • 我注解掉了一些提示您调整大小行为的行。
  • 您可能想在框架上调用setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  • 网格从x=0y=0开始,而不是x=1y=1
gr8qqesn

gr8qqesn2#

import javax.swing.*;
import javax.swing.text.*;

public class JTextPaneDemo extends JFrame {
    private JTextPane textPane;

    public JTextPaneDemo() {
        setTitle("JTextPane Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);

        // Create a new JTextPane object
        textPane = new JTextPane();

        // Add some content to the JTextPane
        String text = "Hello World!\nThis is a JTextPane example.";
        textPane.setText(text);

        // Apply some styles to the text
        StyledDocument doc = textPane.getStyledDocument();
        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        doc.setParagraphAttributes(0, doc.getLength(), center, false);

        // Add the JTextPane to the JFrame
        add(new JScrollPane(textPane));

        setVisible(true);
    }

    public static void main(String[] args) {
        new JTextPaneDemo();
    }
}

相关问题