我想在textarea中創建一個滾動條,但是如果我將JPanel佈局設置爲null,滾動條將不會顯示!JTextArea中的滾動條
我試圖
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
@Kiheru我說我試圖做到這一點,但沒有奏效。 –
它確實有效,但是您需要正確設置滾動窗格的邊界,因爲您嘗試手動創建佈局(是的,我試過了)。 – kiheru