我正在学习 Java,最近想制作一个模仿 iMessage 之类的文本消息系统。我现在的主要问题是,当涉及到带有我创建的标签和文本标签的图层优先级系统时,我不明白或不知道该使用什么。我当前的代码将所有内容放在固定位置,我根本无法更改大小/位置。实现图层系统同时又让我可以自由调整标签的大小/位置的好方法是什么?
import javax.swing.*;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.PriorityQueue;
import javax.swing.*;
import javax.swing.border.*;
import javax.accessibility.*;
public class ChatSystem {
static JFrame frame = new JFrame("ChatBot");
static JTextField chat_input = new JTextField(8);
static JFrame chatPanel = new JFrame();
static JTextArea ca = new JTextArea();
static JLayeredPane layoutPriority = new JLayeredPane();
public static String getTextFieldText() {
return chat_input.getText();
}
static void createScrollingFrame() {
JPanel layeredPane = new JPanel();
layeredPane.setOpaque(false); // Make the JPanel transparent
frame.setContentPane(layeredPane);
layeredPane.add(chat_input, JLayeredPane.DEFAULT_LAYER);
// Set the preferred size for the JPanel
layeredPane.setPreferredSize(new Dimension(1200, 800)); // Adjust the size as needed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
JPanel leftFrame = new JPanel();
leftFrame.setBackground(Color.decode("#7289da"));
leftFrame.setBounds(220, 20, 350, 550);
JPanel rightFrame = new JPanel();
rightFrame.setBackground(Color.decode("#9B90B2"));
rightFrame.setBounds(720, 20, 350, 550);
layeredPane.add(rightFrame, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(leftFrame, JLayeredPane.DEFAULT_LAYER);
// UI Labels
JLabel client = new JLabel();
Font f3 = new Font(Font.DIALOG, Font.BOLD, 35);
client.setFont(f3);
client.setText("Amel!a");
client.setForeground(Color.BLACK);
client.setBounds(125, 0, 350, 200);
layeredPane.add(client, JLayeredPane.PALETTE_LAYER);
// Chat input position (adjust as needed)
chat_input.setBounds(820, 950, 950, 40);
layeredPane.add(chat_input, JLayeredPane.DEFAULT_LAYER);
}
static void chatHandler() {
//handles create the "chat bubbles" with the text
//take input and add to scrolling frame
}
public static void main(String[] args) {
frame.add(layoutPriority);
frame.setLayout(null); // Set the layout manager for the frame
frame.setSize(12000, 1200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.decode("#1e2124")); // set background color
frame.setVisible(true);
//text input
chat_input.setBackground(Color.decode("#7289da"));
chat_input.setForeground(Color.WHITE);
// 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(220, 650, 850, 40);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(null); // Set null layout for precise positioning
bottomPanel.add(chat_input);
frame.add(bottomPanel, BorderLayout.SOUTH);
//create chat box // connect action
createScrollingFrame();
EventListeners eventListeners = new EventListeners();
chat_input.addActionListener((ActionListener) eventListeners);
}
}
我尝试过使用不同的布局管理器,但我总是面临优先级系统有效但无法更改大小/位置的问题,或者我可以更改大小/位置但项目不可见的问题。作为参考,我在游戏编程方面更有经验,因此创建一个用于分层 UI 的系统是我从未遇到过的问题。
“...我现在的主要问题是,当涉及到具有我创建的标签和文本标签的图层优先级系统时,我不明白或不知道该使用什么。...实现图层的好方法是什么系统,还让我可以自由调整标签的尺寸/位置?...”
您所指的通常称为“z-order”;如轴 x、y 和 z。
Container类中有一个方法。
setComponentZOrder
... 将指定组件移动到容器中指定的 z 顺序索引。 z 顺序决定了组件绘制的顺序; z 顺序最高的组件首先绘制,z 顺序最低的组件最后绘制。当组件重叠时,具有较低 z 顺序的组件将绘制在具有较高 z 顺序的组件之上。 ...
更方便的是,您可以使用 JLayeredPane 组件。
这里是 Java 教程。
如何使用分层窗格(Java™ 教程 > 使用 Swing 创建 GUI > 使用 Swing 组件)。
Java 教程代码示例 – LayeredPaneDemo.java.
最后一点,在您的代码中,您应该在 frame.setVisible(true) 之前调用 createScrollingFrame。