问题
private JPanel contentPane;
public Driver() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 867, 502);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
final JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBounds(10, 11, 831, 393);
JScrollPane scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textArea.setText("dfgf");
contentPane.add(scroll);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Driver frame = new Driver();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Why this doesn't not show the textArea
with a scroll bar?
my problem is that I don't see even the textArea. but if I do contentPane.add(scroll);
instead, I can see textArea but no scroll.
回答1:
"why this doesn not show the textArea with a scroll bar?"
Because the viewport of the scrollpane has its own layout manager and is changing the use & position in the text area to suit its needs
The viewport will use The text area's preferred size property to determine how to lay it out. You can effect this value by adding text to the text area and/or setting the row/column properties according to your needs.
Updated with example
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestScrollPane03 {
public static void main(String[] args) {
new TestScrollPane03();
}
public TestScrollPane03() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JTextArea textArea = new JTextArea(100, 50);
JScrollPane scrollPane = new JScrollPane(textArea);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(scrollPane);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Updated based on changes to question
As camickr has already pointed out, the problem you are having is related to the how you are laying out your components.
This contentPane.setLayout(null);
is just one of your many problems. Stop using null
layouts and start using appropriate layout managers.
回答2:
A scrollbar will appear automatically when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane. You need to use layout managers for this to happen automatically
final JTextArea textArea = new JTextArea();
textArea.setBounds(10, 11, 831, 393);
JScrollPane scroll = new JScrollPane(textArea);
You should NOT be using setBounds(). Instead you do something like:
final JTextArea textArea = new JTextArea(5, 30);
JScrollPane scroll = new JScrollPane(textArea);
Now when you load more then 5 lines of text, the scrollbar will appear. Of course all this assumes you are using an appropriate layout maanager.
回答3:
why this doesn not show the textArea with a scroll bar
The scrollbar will in fact appear but without the "thumb grip" until you have appended enough text to your JTextArea
to as to expand its size beyond the JScrollPane
viewport size.
You're using null
layout for your contentPane
JPanel
. This is never a good idea. Here your JScrollPane
has a default size of 0 x 0
so never appears. There are plenty of layout managers available for various sizing & positioning requirements.
来源:https://stackoverflow.com/questions/15454442/jtextarea-no-scroll-bar