我输入任何不适合的文本,而JScrollPane不起作用。通常,它不显示文本区域。
textArea = new JTextArea("asdfsdfsdfsdfsdfasdfsdfsdfsdfsdfasdfsdfsdfsdfsdfasdfsdfsdfsdfs");
textArea.setBounds(20, 400, 130, 30);
textArea.setFont(new Font("SansSerif", Font.BOLD, 20));
panelHome.add(new JScrollPane(textArea));
答案 0 :(得分:2)
如果你想在jscroll中使用texarea,你应该使用类似的解决方案:
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
答案 1 :(得分:2)
你的问题在这里:
textArea.setBounds(20, 400, 130, 30);
你将JTextArea的大小限制为130乘30,无论它有什么文本,这样做会阻止JScrollPane正常工作。您永远不想设置Swing组件的大小(或使用空布局),而是使用JTextArea来设置其行和列。如果需要,这将允许JTextArea扩展。
编辑:按照用户1722245的建议--- 1+给他答案。
请注意,我通常也会在JTextAreas上调用setLineWrap(true)
和setWrapStyleWord(true)
,以便在需要时将文本换行到下一行。
另一个重要的因素是容器的布局管理器,这里是panelHome,你要添加JScrollPane,因为这会影响它显示JScrollPane及其内容的方式。
例如,在下面的代码中,我在其构造函数中设置了JTextArea的列和行属性,我设置了文本区域自动换行策略,我将它添加到JScrollPane的视口视图中,方法是将它传递给JScrollPane的构造函数,然后我将JScrollPane添加到BorderLayout - 使用CENTER位置的JPanel。现在,当文本附加到JTextArea时,它会显示包装的文本并显示滚动条,但仅在需要时显示。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class TextAreaEg extends JPanel {
private static final int TEXTAREA_ROWS = 20;
private static final int TEXTAREA_COLUMNS = 40;
private static final String NONSENSE_TEXT = "Lorem ipsum dolor sit amet, "
+ "consectetur adipiscing elit, sed do eiusmod tempor "
+ "incididunt ut labore et dolore magna aliqua. Ut enim ad "
+ "minim veniam, quis nostrud exercitation ullamco laboris "
+ "nisi ut aliquip ex ea commodo consequat. Duis aute irure "
+ "dolor in reprehenderit in voluptate velit esse cillum "
+ "dolore eu fugiat nulla pariatur. Excepteur sint occaecat "
+ "cupidatat non proident, sunt in culpa qui officia "
+ "deserunt mollit anim id est laborum.";
private JTextArea textArea = new JTextArea(TEXTAREA_ROWS, TEXTAREA_COLUMNS);
public TextAreaEg() {
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(textArea);
JButton addTextButton = new JButton(new AddTextAction("Add Text", KeyEvent.VK_A));
JPanel buttonPanel = new JPanel();
buttonPanel.add(addTextButton);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
}
private class AddTextAction extends AbstractAction {
public AddTextAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 5; i++) {
textArea.append(NONSENSE_TEXT + "\n");
}
}
}
private static void createAndShowGui() {
TextAreaEg mainPanel = new TextAreaEg();
JFrame frame = new JFrame("JTextArea Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}