下面是我的代码,它向框架添加了一些 swing 组件。我使用两个文本窗格并为两者设置一些文本。但是文本很大,当我运行代码时只有文本窗格可见。所以我尝试将滚动窗格添加到文本窗格 ta2但随后也没有任何反应。文本窗格 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();
}
}
您需要将
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=0
和y=0
开始,而不是x=1
和y=1
。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();
}
}