gpt4 book ai didi

How to create a Scrolling Pane for a Chat System?(如何为聊天系统创建滚动窗格?)

转载 作者:bug小助手 更新时间:2023-10-24 21:26:26 27 4
gpt4 key购买 nike



I've already created a working scrolling pane using JScrollPane, however, the current code works as follows: I type something into the chat input, press enter, it creates a JPanel and adds it to the JScrollPane. However, the JScrollPane scroll bar is fixed. Is there a property where I can change it's canvas size so I can actually "scroll" the scrolling bar. Or is there a simpler method to achieve what I'm trying to do?

我已经使用JScrollPane创建了一个工作滚动窗格,然而,当前代码的工作方式如下:我在聊天输入中键入内容,按Enter键,它创建一个JPanel并将其添加到JScrollPane中。但是,JScrollPane滚动条是固定的。有没有一个属性可以让我改变画布的大小,这样我就可以真正“滚动”滚动条了。或者,有没有更简单的方法来实现我想要做的事情?


ChatUI.java

ChatUI.java


import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.Font;
import java.awt.Image;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

public class ChatUI {
static JTextField chat_input = new JTextField(8);
static JPanel inner_background = new JPanel();
static JScrollPane scrollPane = new JScrollPane(inner_background);

public static String getTextFieldText() {
return chat_input.getText();
}

public static void createScrollingList(JLayeredPane layeredPane) {
// Set the layout manager for the inner background panel
inner_background.setLayout(new BoxLayout(inner_background, BoxLayout.Y_AXIS));

// Set the preferred size of inner_background to be larger than the visible area
inner_background.setPreferredSize(new Dimension(300, 1000));

// Other settings for the inner background panel
inner_background.setBackground(Color.decode("#111214"));
inner_background.setBorder(new LineBorder(Color.decode("#111214"), 2, true));

layeredPane.add(inner_background, Integer.valueOf(0));

// Configure the JScrollPane
scrollPane.setBounds(20, 200, 330, 450);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.getViewport().setBackground(Color.decode("#111214"));


scrollPane.setPreferredSize(new Dimension(400, 1000)); // Set a larger preferred size
// scrollPane.setLayout(new BoxLayout(inner_background, BoxLayout.Y_AXIS)); // Use BoxLayout for vertical layout

// Add some components to the content panel
for (int i = 0; i < 20; i++) {
JButton button = new JButton("Button " + i);
scrollPane.add(button);
}
// Add the JScrollPane to the layeredPane
layeredPane.add(scrollPane, Integer.valueOf(2));
}


static void UIVisuals(JFrame frame, JLayeredPane layeredPane) {
// Left frame
JPanel topBackground = new JPanel();
topBackground.setBackground(Color.decode("#62D0F6"));
topBackground.setBounds(0, 0, 500, 80);

// Right frame
JPanel rightFrame = new JPanel();
rightFrame.setBackground(Color.decode("#9B90B2"));
rightFrame.setBounds(20, 40, 20, 20);

// Add frames to layeredPane
layeredPane.add(rightFrame, Integer.valueOf(1));
layeredPane.add(topBackground, Integer.valueOf(1));

// User_Icon
ImageIcon userIcon = new ImageIcon("C:/Users/pokec/OneDrive/Pictures/user_java_icon.png");
JLabel userLabel = new JLabel(userIcon);
userLabel.setBounds(0, 30, userIcon.getIconWidth(), userIcon.getIconHeight()); // Adjust the bounds as needed
layeredPane.add(userLabel, Integer.valueOf(2));

// Emoji Button
ImageIcon imageIcon = new ImageIcon("C:\\Users\\pokec\\OneDrive\\Pictures\\gift_icon.png");

// Create a JButton and set the image icon
JButton gift_button = new JButton(imageIcon);
gift_button.setBounds(320, 670, 40, 40);
layeredPane.add(gift_button, Integer.valueOf(2));

// Make button transparent
gift_button.setOpaque(false);
gift_button.setContentAreaFilled(false);
gift_button.setBorderPainted(false);

// Texting Panel
chat_input.setBackground(Color.decode("#383A40"));
chat_input.setForeground(Color.WHITE);
chat_input.setBorder(new LineBorder(Color.decode("#4B4D54"), 1, true));

// Font setup
Font f3 = new Font(Font.DIALOG, Font.BOLD, 15);
chat_input.setFont(f3);

// Adjust the position and size of the chat input
chat_input.setBounds(20, 670, 280, 40);
layeredPane.add(chat_input, Integer.valueOf(1));

JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(null); // Set null layout for precise positioning
bottomPanel.add(chat_input);

// Add components to layeredPane
layeredPane.add(chat_input, Integer.valueOf(1));
layeredPane.add(bottomPanel, BorderLayout.SOUTH);

// Event Listeners
EventListeners eventListeners = new EventListeners(gift_button, chat_input);
chat_input.addActionListener(eventListeners);
gift_button.addActionListener(eventListeners);

createScrollingList(layeredPane);




}
}


EventListeners.java

EventListeners.java


  import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JViewport;

public class EventListeners implements ActionListener {
private final JButton button;
private final JTextField textField;

public EventListeners(JButton gift_button, JTextField textField) {
this.button = gift_button;
this.textField = textField;
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
//Gift Button
System.out.println("Button clicked");

} else if (e.getSource() == textField) {
//Enter
String input = textField.getText();
if (!input.isEmpty()) {
System.out.println("Enter key pressed: " + input);
textField.setText("");

// Create a JPanel to add to the JScrollPane
JPanel chatPanel = new JPanel();
chatPanel.setPreferredSize(new Dimension(20, 10)); // Set the preferred size
chatPanel.setBackground(Color.YELLOW); // Set the background color or customize as needed

// Add the chatPanel to the JScrollPane's viewport
JViewport viewport = ChatUI.scrollPane.getViewport();
viewport.add(chatPanel);

// Repaint the viewport to reflect changes
viewport.repaint();
}
}
}
}


I've attempted changing the size of the panel that gets added into the ScrollingPane and it doesn't seem to change in size and rather covers the entire scrolling Pane. Again, not sure if there is a proprety I'm not aware of about JScrollPane that can change its canvas size resulting in the scroll bar to actually be scrollable.

我尝试更改添加到ScrollingPane中的面板的大小,它似乎没有改变大小,反而覆盖了整个滚动Pane。再说一次,我不确定JScrollPane是否有一个我不知道的特性,可以改变它的画布大小,导致滚动条实际上是可滚动的。


https://i.stack.imgur.com/aTTg0.png


更多回答

"JScrollPane scroll bar is fixed" then you've done it wrong

“JScrollPane滚动条已修复”,说明您做错了

Don't do viewport.add(chatPanel); this isn't how scroll panes work. Instead, create a "conversation pane" onto which your CharPanels can be added. The JScrollPane should wrap the "conversation pane" (see things like the JScrollPane constructor or JScrollPane#setViewportView. In fact you might actually just want to look at How to Use Scroll Panes)

不要执行viewport.add(Chat Panel);这不是滚动窗格的工作方式。相反,你可以创建一个“对话面板”,你可以在上面添加你的CharPanels。JScrollPane应该包装“对话面板”(参见JScrollPane构造函数或JScrollPane#setViewportView之类的内容。实际上,您可能只想了解如何使用Scroll Panes)

static JScrollPane scrollPane = new JScrollPane(inner_background); is a bad idea - static is not your friend in this context. This is ignoring principles like encapsulation and single responsibility, is exposing portions or your code to manipulation in ways which weren't intended (see single responsibility principle) and would be better handled using things like dependency injection and/or observer patterns

静态JScrollPane scllPane=new JScrollPane(INTERNAL_BACKGROUND);不是一个好主意--在这种情况下,静态不是您的朋友。这忽略了封装和单一责任等原则,将部分或您的代码暴露在无意的操作方式下(请参阅单一责任原则),使用依赖项注入和/或观察者模式等处理会更好

Get rid of all the setPreferredSize() statements. It is the job of the layout manager to determine the preferred size of the component. Setting the preferred size of the component added to the viewport of the scrollpane will prevent scrolling from working properly. Also, I see no need to use a layered pane.

删除所有的setPferredSize()语句。确定组件的首选大小是布局管理器的工作。设置添加到滚动窗格的视区的组件的首选大小将阻止滚动正常工作。此外,我认为没有必要使用分层的窗格。

The use of JLayeredPane in this way seems naive to me. JLayeredPane isn't the simplest component to deal with and you're adding the additional overhead of having to (in a naive way) manage the layout. Instead, you should be taking the time to learn and understand the available layout managers and making use of "compound" layouts to help solve complex UIs. Maybe start with Laying Out Components Within a Container

在我看来,以这种方式使用JLayeredPane似乎很天真。JLayeredPane不是要处理的最简单的组件,而且您增加了必须(以一种幼稚的方式)管理布局的额外开销。相反,您应该花时间学习和理解可用的布局管理器,并利用“复合”布局来帮助解决复杂的UI。也许可以从在容器中布局组件开始

优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com