-2
スクロール可能な結果を表示するには、textAreaが必要です。 VERTICAL_SCROLLBAR_ALWAYSに設定してもスクロールバーが表示されない 何が間違っていますか?Java - Swing - TextArea - scrollbar
void addPlayerPanel(JFrame gameFrame) {
JPanel playerPanel = new JPanel();
// automatically added to contentPane with gameFrame.add()
gameFrame.add(playerPanel, BorderLayout.CENTER);
playerPanel.setBorder(new TitledBorder(new EtchedBorder(), "Registered players"));
// text are to show registered players
JTextArea display = new JTextArea(5, 40);
display.setEditable(true); // set textArea to editable
display.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
JScrollPane scroll = new JScrollPane(display);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
// Add Text area
playerPanel.add(scroll);
playerPanel.add(display);
}
これはうまくいきました。私はdisplay.setLayoutも削除しました。 borderlayoutを維持する – user3162260
@ user3162260:BorderLayoutとは何ですか?上記のコードでは、BorderLayoutでコンポーネントのレイアウトを設定しません。 –
gameFrame.add(playerPanel、BorderLayout.CENTER);これは何もしない?それは自動的に中心に置かれるからですか? – user3162260