为什么 JScrollPane 出现但没有显示它的滚动条?
Why does the JScrollPane show up but not its scroll bar?
这是我的代码。 JScrollpanel 出现了,但现在是滚动条。如果文本低于该区域,我也无法使用滚轮。
JTextArea textArea = new JTextArea("Enter text here");
textArea.setPreferredSize(new Dimension(100,100));
textArea.setLineWrap(true);
textArea.setEditable(true);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//frame
this.setTitle("GUI Practice");
this.setLayout(new BorderLayout());
this.setResizable(true);
this.setSize(720,480);
this.setLocationRelativeTo ( null );
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(scroll, BorderLayout.SOUTH);
this.setVisible(true);
从不,从不,从不这样做:
textArea.setPreferredSize(new Dimension(100,100));
您人为地限制了 JTextArea 的大小,您您自己导致 JScrollPane 失败。相反,设置 JTextArea 的列和行属性。
例如,
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
@SuppressWarnings("serial")
public class GuiPracticePanel extends JPanel {
private static final int ROWS = 10;
private static final int COLS = 80;
private static final String INIT_TEXT = "Enter text here";
private JTextArea textArea = new JTextArea(INIT_TEXT, ROWS, COLS);
public GuiPracticePanel() {
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setLayout(new BorderLayout());
add(Box.createRigidArea(new Dimension(400, 300)));
add(scrollPane, BorderLayout.PAGE_END);
textArea.selectAll();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
GuiPracticePanel mainPanel = new GuiPracticePanel();
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
这是我的代码。 JScrollpanel 出现了,但现在是滚动条。如果文本低于该区域,我也无法使用滚轮。
JTextArea textArea = new JTextArea("Enter text here");
textArea.setPreferredSize(new Dimension(100,100));
textArea.setLineWrap(true);
textArea.setEditable(true);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//frame
this.setTitle("GUI Practice");
this.setLayout(new BorderLayout());
this.setResizable(true);
this.setSize(720,480);
this.setLocationRelativeTo ( null );
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(scroll, BorderLayout.SOUTH);
this.setVisible(true);
从不,从不,从不这样做:
textArea.setPreferredSize(new Dimension(100,100));
您人为地限制了 JTextArea 的大小,您您自己导致 JScrollPane 失败。相反,设置 JTextArea 的列和行属性。
例如,
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
@SuppressWarnings("serial")
public class GuiPracticePanel extends JPanel {
private static final int ROWS = 10;
private static final int COLS = 80;
private static final String INIT_TEXT = "Enter text here";
private JTextArea textArea = new JTextArea(INIT_TEXT, ROWS, COLS);
public GuiPracticePanel() {
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setLayout(new BorderLayout());
add(Box.createRigidArea(new Dimension(400, 300)));
add(scrollPane, BorderLayout.PAGE_END);
textArea.selectAll();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
GuiPracticePanel mainPanel = new GuiPracticePanel();
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}