滚动窗格不起作用
我对 ScrollPane 和 TextArea 有问题,我在那里放置了一个滚动条,当我尝试在 TextArea 中输入内容时,它不会滚动,它会变得更宽。这里你得到了它的代码:
package interface_Components;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class chatComponent extends JFrame {
private JTextField chatInput;
private JTextArea chatOutput;
private JScrollPane chatScroll;
private JButton sendButton;
private JButton newRoomButton;
private JButton joinRoomButton;
private JButton inviteButton;
private JList roomsList;
private JList usersList;
public chatComponent() {
JFrame loggedInWindow = new JFrame("Yikes!");
chatInput = new JTextField("Type here");
chatOutput = new JTextArea("Type here and press enter many times scroll doesnt work I dont know why");
chatScroll = new JScrollPane(chatOutput);
sendButton = new JButton("Send");
newRoomButton = new JButton("New Room");
joinRoomButton = new JButton("Join Room");
inviteButton = new JButton("Invite");
roomsList = new JList();
usersList = new JList();
chatInput.selectAll();
chatScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
chatScroll.setAutoscrolls(true);
chatOutput.setRows(6);
chatOutput.setLineWrap(true);
chatOutput.setAutoscrolls(true);
loggedInWindow.setLayout(new BorderLayout(5, 5));
JPanel centerPanel = new JPanel(new BorderLayout(5, 5));
JPanel eastPanel = new JPanel(new BorderLayout(5, 5));
JPanel centerInternal_1 = new JPanel(new BorderLayout(5, 5));
JPanel centerInternal_2 = new JPanel(new BorderLayout(5, 5));
JPanel centerInternal_3 = new JPanel(new BorderLayout(5, 5));
JPanel eastInternal_1 = new JPanel(new BorderLayout(5, 5));
JPanel eastInternal_2 = new JPanel(new GridLayout(3, 0, 5, 5));
centerInternal_3.add(chatInput, BorderLayout.CENTER);
centerInternal_3.add(sendButton, BorderLayout.EAST);
centerInternal_2.add(chatOutput, BorderLayout.CENTER);
centerInternal_2.add(chatScroll, BorderLayout.EAST);
centerInternal_2.add(centerInternal_3, BorderLayout.SOUTH);
centerInternal_1.add(centerInternal_2, BorderLayout.SOUTH);
centerInternal_1.add(roomsList, BorderLayout.CENTER);
centerPanel.add(centerInternal_1, BorderLayout.CENTER);
eastInternal_1.add(usersList, BorderLayout.CENTER);
eastInternal_2.add(newRoomButton);
eastInternal_2.add(joinRoomButton);
eastInternal_2.add(inviteButton);
eastPanel.add(eastInternal_1, BorderLayout.CENTER);
eastPanel.add(eastInternal_2, BorderLayout.SOUTH);
loggedInWindow.add(eastPanel, BorderLayout.EAST);
loggedInWindow.add(centerPanel, BorderLayout.CENTER);
loggedInWindow.setVisible(true);
loggedInWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loggedInWindow.setSize(800, 600);
}
public static void main(String[] args) {
chatComponent cc = new chatComponent();
}
}
I have a problem with ScrollPane and TextArea I put a scroll there and when I try to type in the TextArea it doesn't scroll it gets wider. Here you got the code for it:
package interface_Components;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class chatComponent extends JFrame {
private JTextField chatInput;
private JTextArea chatOutput;
private JScrollPane chatScroll;
private JButton sendButton;
private JButton newRoomButton;
private JButton joinRoomButton;
private JButton inviteButton;
private JList roomsList;
private JList usersList;
public chatComponent() {
JFrame loggedInWindow = new JFrame("Yikes!");
chatInput = new JTextField("Type here");
chatOutput = new JTextArea("Type here and press enter many times scroll doesnt work I dont know why");
chatScroll = new JScrollPane(chatOutput);
sendButton = new JButton("Send");
newRoomButton = new JButton("New Room");
joinRoomButton = new JButton("Join Room");
inviteButton = new JButton("Invite");
roomsList = new JList();
usersList = new JList();
chatInput.selectAll();
chatScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
chatScroll.setAutoscrolls(true);
chatOutput.setRows(6);
chatOutput.setLineWrap(true);
chatOutput.setAutoscrolls(true);
loggedInWindow.setLayout(new BorderLayout(5, 5));
JPanel centerPanel = new JPanel(new BorderLayout(5, 5));
JPanel eastPanel = new JPanel(new BorderLayout(5, 5));
JPanel centerInternal_1 = new JPanel(new BorderLayout(5, 5));
JPanel centerInternal_2 = new JPanel(new BorderLayout(5, 5));
JPanel centerInternal_3 = new JPanel(new BorderLayout(5, 5));
JPanel eastInternal_1 = new JPanel(new BorderLayout(5, 5));
JPanel eastInternal_2 = new JPanel(new GridLayout(3, 0, 5, 5));
centerInternal_3.add(chatInput, BorderLayout.CENTER);
centerInternal_3.add(sendButton, BorderLayout.EAST);
centerInternal_2.add(chatOutput, BorderLayout.CENTER);
centerInternal_2.add(chatScroll, BorderLayout.EAST);
centerInternal_2.add(centerInternal_3, BorderLayout.SOUTH);
centerInternal_1.add(centerInternal_2, BorderLayout.SOUTH);
centerInternal_1.add(roomsList, BorderLayout.CENTER);
centerPanel.add(centerInternal_1, BorderLayout.CENTER);
eastInternal_1.add(usersList, BorderLayout.CENTER);
eastInternal_2.add(newRoomButton);
eastInternal_2.add(joinRoomButton);
eastInternal_2.add(inviteButton);
eastPanel.add(eastInternal_1, BorderLayout.CENTER);
eastPanel.add(eastInternal_2, BorderLayout.SOUTH);
loggedInWindow.add(eastPanel, BorderLayout.EAST);
loggedInWindow.add(centerPanel, BorderLayout.CENTER);
loggedInWindow.setVisible(true);
loggedInWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loggedInWindow.setSize(800, 600);
}
public static void main(String[] args) {
chatComponent cc = new chatComponent();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您将
chatOutput
添加到面板的中心,并将chatScroll
添加到东部。我认为这不是你想要的。您需要将
chatScroll
添加到 CENTER,如下所示:Problem is you are adding
chatOutput
to the CENTER of the panel andchatScroll
to the EAST. I don't think this is what you want.You need to add
chatScroll
to the CENTER like this: