1
當我註釋掉frame.add(隱藏)時,它只顯示文本區域。當我不評論它時,它只會顯示一個灰色的灰色框,並帶有灰色的滾動條。爲什麼JScrollPane無法正常工作?
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Panlindrome{
public Panlindrome(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Panlindrome?");
frame.setSize(240,320);
//frame.setLayout(new GridLayout(3,1));
JTextArea inputText = new JTextArea(30,1);
inputText.setLineWrap(true);
JScrollPane hidden = new JScrollPane(inputText);
hidden.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(inputText);
//frame.add(hidden);
frame.setVisible(true);
}
public static void main(String[] args){
Panlindrome check = new Panlindrome();
}
}
自從我使用Java以來已經有一段時間了。我認爲這是因爲您只能將Swing小部件添加到一個父項;因此,當您直接將其添加到框架時,將會從滾動窗格中刪除「inputText」。 – iBelieve
'frame.add(inputText)'將從滾動窗格中刪除文本區域,因此在此調用之後添加滾動窗格將導致一個空的滾動窗格(並且'inputText'已從框架中移除,因爲這就是' BorderLayout'作品)... – MadProgrammer
@mspencer我會說你需要做出答案;) – MadProgrammer