当我添加一个滚动到我的文本区域,然后文本区域在应用程序中不可见:
代码: JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
final JTextArea textArea = new JTextArea();
textArea.setBounds(15, 112, 689, 310);
JScrollPane scrollPane = new JScrollPane( textArea );
panel.add( scrollPane );
这是如何做代码似乎试图做的,使用文本区域,布局和填充的大小提示。根据需要调整数字
参见代码中的进一步注释:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ScrollingTextArea {
private JComponent panel = null;
ScrollingTextArea() {
initUI();
}
public void initUI() {
if (panel != null) {
return;
}
panel = new JPanel(new BorderLayout());
// adjust numbers to need
panel.setBorder(new EmptyBorder(32, 15, 32, 15));
// adjust rows & cols to need
final JTextArea textArea = new JTextArea(20,80);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane);
}
public JComponent getUI() {
return panel;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ScrollingTextArea o = new ScrollingTextArea();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}