当我向我的textarea添加滚动时,textarea在应用程序中不可见:
代码:
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 );
答案 0 :(得分:3)
以下是如何使用文本区域,布局和填充的大小提示来执行代码似乎要执行的操作。调整需要的数字。
参见代码中的进一步评论:
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);
}
}