JScrollBar和JScrollPane有什么区别?
我正在尝试将JScrollPane添加到我的大型JTextArea中,但每当我包含JScrollPane代码时,我的整个JTextArea都会消失.
public myGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1174, 656);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setVisible(true);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBounds(203, 5, 869, 440);
textArea_1.setEditable(true);
textArea_1.setVisible(true);
contentPane.add(textArea_1);
JScrollPane scroll = new JScrollPane (textArea_1);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
contentPane.add(scroll);
Run Code Online (Sandbox Code Playgroud)
}
我的出发点是这样的:

标签下面的矩形是JScrollPane.
根据变量的值,我必须将JLabel和JCheckBox添加到滚动窗格.
我读了其他帖子,但我不能做我想要的.你能帮助我吗?
我没有代码,因为我认为我可以使用NetBeans设计器来完成它.我唯一能做的就是
JLabel label = new JLabel("New Label");
JCheckBox cb = new JCheckBox("New CheckBox");
jScrollPane1.add(label);
jScrollPane1.add(cb);
Run Code Online (Sandbox Code Playgroud)
但显然它不起作用.感谢Robby现在我可以添加组件.我需要知道如何对齐它们.例如,jLabel1下的一列标签,jLabel2下的一列复选框等都具有相同的高度和宽度.
可能重复:
Java Textarea ScrollPane
我有一个滚动条的代码出现在textarea但它并没有真正起作用.有什么建议?
final JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBounds(10, 152, 456, 255);
textArea.setBorder(border);
textArea.setLineWrap(true);
sbrText = new JScrollPane(textArea);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel_1.add(textArea);
Run Code Online (Sandbox Code Playgroud) 我正在编写一些代码。JFrame包含JPanel具有特定尺寸的。这是我的代码:
import javax.swing.*;
import java.awt.*;
public class ScrollPane extends JFrame {
public ScrollPane() {
super("Title");
setLayout(new BorderLayout());
setSize(320,240);
JScrollPane scroller = new JScrollPane(new DrawingPane());
scroller.setPreferredSize(new Dimension(320,240));
add(scroller);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private class DrawingPane extends JPanel {
public DrawingPane(){
super();
setSize(640,480);
setMinimumSize(new Dimension(320,240));
}
}
public static void main(String[] args) {
new ScrollPane();
}
}
Run Code Online (Sandbox Code Playgroud)
即使设置了最小尺寸JPanel,卷轴也不会出现。