Java 滑块没有显示在我的JScrollPane上,使用了我从堆栈溢出中获得的代码
我查看了这个堆栈溢出的解决方案,但它对我不起作用。我的代码:Java 滑块没有显示在我的JScrollPane上,使用了我从堆栈溢出中获得的代码,java,Java,我查看了这个堆栈溢出的解决方案,但它对我不起作用。我的代码: public cTextWin( String name, int id, String body) throws Exception { super(name,id); JPanel containerPanel = new JPanel(new BorderLayout() ); textArea = new JTextArea(body); scrollPane = new JScrollP
public cTextWin( String name, int id, String body) throws Exception
{
super(name,id);
JPanel containerPanel = new JPanel(new BorderLayout() );
textArea = new JTextArea(body);
scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);
containerPanel.add(scrollPane);
containerPanel.add(textArea,BorderLayout.CENTER);
add(containerPanel);
}
是的,如果复制并粘贴一块GUI代码,它将无法正常运行。您应该研究一下获得GUI设置的最低要求。您的代码不清楚您是否从swing线程、初始化变量、添加和显示JFrame运行。。。等等 除非设置滚动策略或插入足够的文本来强制滚动,否则就不清楚是否有JScrollPane。您必须使用布局管理器和组件的大小
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
public static void createGUI() {
JFrame jf = new JFrame();
addComp(jf.getContentPane());
jf.setVisible(true);
jf.pack();
}
public static void addComp(Container pane) {
JPanel containerPanel = new JPanel(new BorderLayout() );
JTextArea textArea = new JTextArea("stuff");
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);
containerPanel.add(scrollPane);
containerPanel.add(textArea, BorderLayout.CENTER);
pane.add(containerPanel);
}
}
请分享您从中获得代码的答案