尽管使用了:setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS),但JScrollPane滚动条未显示的java奇怪问题;
因此,在下面的代码中,我在左侧有一个JTextArea。右上侧的JScrollPane看起来不错。使用相同的代码,我还在右下角添加了一个JScrollPane,但尽管代码相同,但保存了首选大小和绝对位置,垂直滚动条似乎没有显示出来
我将在代码之后添加GUI的屏幕截图。提前感谢您为解决此问题提供的任何帮助
frame = new JFrame("Title");
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(width, height));
frame.pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
frame.setResizable(false);
frame.addKeyListener(this);
//scroll and text area
textArea = new JTextArea();
textArea.setText("Static Text\n");
textArea.setFont(new Font("Consolas", 0, 12));
textArea.setColumns(50);
textArea.setLineWrap(true);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(width/2, height * 4 / 5));
scrollPane.setBounds(width/2, 0, width/2, height * 4 / 5);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scrollPane);
inputTextArea = new JTextArea();
inputTextArea.setText(">");
inputTextArea.setFont(new Font("Consolas", 0, 12));
inputTextArea.setColumns(50);
inputTextArea.setLineWrap(true);
inputScrollPane = new JScrollPane(inputTextArea);
inputScrollPane.setPreferredSize(new Dimension(width/2, height / 5));
inputScrollPane.setBounds(width/2, height * 4 / 5, width, height);
inputScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(inputScrollPane);
//map
mapView = new JTextArea();
mapView.setFont(new Font("Consolas", 0, 8));
mapView.setEditable(false);
mapView.setPreferredSize(new Dimension(width/2, height));
mapView.setText(state.getCurrentMap().toString());
mapView.addKeyListener(this);
mapView.setBounds(0, 0, width/2, height);
frame.add(mapView);
frame.pack();
frame.setVisible(true);
# 1 楼答案
该代码有几个重要问题,包括
setBounds()
可能会像创建复杂GUI的最简单和最好的方式一样吸引新手,但您创建的Swing GUI越多,在使用它们时会遇到越严重的困难。当GUI调整大小时,它们不会调整您的组件的大小,它们是一个需要增强或维护的皇家女巫,它们在滚动窗格中完全失败,在所有平台或屏幕分辨率与原始分辨率不同的情况下查看时,它们看起来非常糟糕。相信我,这会让你的调试工作变得更加困难。因此,学习和使用布局管理器要好得多。你可以在这里找到布局管理器教程:Layout Manager Tutorial,你可以在这里找到Swing教程和其他Swing资源的链接:Swing Info李>例如,下面的代码显示了如何使用简单的布局、文本区域列和行属性,以及使用键绑定来捕获用户按enter键的情况,如果需要的话: