gpt4 book ai didi

java - 添加滚动java时文本区域不可见

转载 作者:行者123 更新时间:2023-12-01 22:05:16 25 4
gpt4 key购买 nike

当我向文本区域添加滚动时,文本区域在应用程序中不可见:

代码:

    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);
}
}

关于java - 添加滚动java时文本区域不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32922711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com