我想在文本区域中创建一个滚动条,但如果我将JPanel布局设置为null,则滚动条不会显示!
我尝试过:
JScrollPane scrollbar1 =
new JScrollPane(
ta1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
但是由于布局为空,它没有起作用。
这是我的当前代码:
import javax.swing.*;
import java.awt.*;
public class app extends JFrame {
public static void main(String[] args)
{
new app();
}
public app()
{
this.setSize(400,400);
this.setLocation(0,0);
this.setResizable(false);
this.setTitle("Application");
JPanel painel = new JPanel(null);
// Creating the Input
JTextField tf1 = new JTextField("Some random text", 15);
tf1.setBounds(5,5,this.getWidth()-120,20);
tf1.setColumns(10);
tf1.setText("Omg");
painel.add(tf1);
// Creating the button
JButton button1 = new JButton("Send");
button1.setBounds(290, 5, 100, 19);
painel.add(button1);
// Creating the TextArea
JTextArea ta1 = new JTextArea(15, 20);
JScrollPane scr = new JScrollPane();
ta1.setBounds(5, 35, 385, 330);
ta1.setLineWrap(true);
ta1.setWrapStyleWord(true);
painel.add(ta1);
this.add(painel);
this.setVisible(true);
}
}
我希望它能正常运作。如果有人可以帮忙,请在下面留言!
null
布局。无论如何,您需要将TextArea
放在滚动窗格中,并将滚动窗格放置在您希望组合出现的位置。 - kiheru