- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 BoxLayout 来定位服务器 GUI 的 GUI 元素。我的 JLabels 发生了一件奇怪的事情。我想知道为什么当我将字符串附加到 JTextArea 时,JLabels 会从其原始位置移动。所以第一个图是位于正确位置的 JLabels。第二张图片是 JLabels 倾斜的
图1
图2
下面是服务器 GUI 的代码
GUI.java
package serverGUI;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class GUI extends JFrame{
public JTextArea serverInfo;
public JButton serverRunningState;
public JLabel serverConnectionStatus = new JLabel(GUIConfig.DEFAULT_SERVER_STATUS);
public JLabel serverInfoLabel = new JLabel(GUIConfig.DEFAULT_SERVER_EVENT_LABEL);
public GUI(){
super("Yahtzee Game Server");
//set the server online/off-line status to default color
serverConnectionStatus.setForeground(GUIConfig.DEFAULT_SERVER_STATUS_COLOR);
//create the server events text area. It will hold all info
//about server events including client connections/disconnections
serverInfo = new JTextArea();
serverInfo.setEditable(false);
serverInfo.setPreferredSize(new Dimension(350, 450));
//create the connect/disconnect button. Will be connect when server
//is not connected and disconnect when server is connected
serverRunningState = new JButton();
serverRunningState.setText("Run Server");
//Create JPanel with box layout
JPanel guiPanel = new JPanel();
guiPanel.setLayout(new BoxLayout(guiPanel, BoxLayout.PAGE_AXIS));
//add components to panel
guiPanel.add(serverInfoLabel);
guiPanel.add(Box.createRigidArea(new Dimension(0,5)));
guiPanel.add(serverInfo);
guiPanel.add(Box.createRigidArea(new Dimension(0,5)));
guiPanel.add(serverConnectionStatus);
//create a bit of space around components so they get away from edges
guiPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
//add panel to frame
add(guiPanel, BorderLayout.CENTER);
//create new panel for buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.PAGE_AXIS));
//add connection button
serverRunningState.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonPanel.add(serverRunningState);
buttonPanel.add(Box.createRigidArea(new Dimension(0,5)));
//add panel to frame
add(buttonPanel, BorderLayout.SOUTH);
//set size, do not allow resize and show
setSize(GUIConfig.DEFAULT_FRAME_WIDTH, GUIConfig.DEFUALT_FRAME_HEIGHT);
setResizable(false);
setVisible(true);
//set it to terminate frame on exit
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]) {
GUI g = new GUI();
}
}
ActionListener 代码 (ServerController.java):
package serverController;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import serverGUI.GUI;
import serverModel.AppServer;
import serverModel.Config;
public class ServerController {
public AppServer server;
public GUI serverGUI;
public ServerController() {
super();
//create the view and add an action listener to know when run server
//button is pressed
serverGUI = new GUI();
//add action listener on run server button
serverGUI.serverRunningState.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
//start server and disable run server button. change its text to server running
server = new AppServer(Config.DEFAULT_PORT);
if(server.isRunning() == true){
serverGUI.serverRunningState.setEnabled(false);
serverGUI.serverRunningState.setText("Server Running");
serverGUI.serverInfo.append("Server started!\nWaiting for new clients...\n\n");
serverGUI.serverInfo.setLineWrap(true);
//change JLabel to "Server Online" from "Server Off-line"
serverGUI.serverConnectionStatus.setText("Server Online");
serverGUI.serverConnectionStatus.setForeground(Color.GREEN);
}
}
});
}
public void updateServerEventLog() {}
private void checkServerStatus(AppServer s) {
}
public static void main(String args[]) {
ServerController sController = new ServerController();
}
}
最佳答案
出现此行为的原因是,一旦向 JTextArea
添加内容,整个 JPanel
就必须重新绘制。而且由于您没有明确设置对齐方式,Swing 会将其对齐到中心,从而弄乱您的 UI。
您需要使用 BoxLayout
显式指定 JPanel
内所有组件的对齐方式:
//add components to panel
serverInfoLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
guiPanel.add(serverInfoLabel);
guiPanel.add(Box.createRigidArea(new Dimension(0,5)));
serverInfo.setAlignmentX(Component.LEFT_ALIGNMENT);
guiPanel.add(serverInfo);
guiPanel.add(Box.createRigidArea(new Dimension(0,5)));
serverConnectionStatus.setAlignmentX(Component.LEFT_ALIGNMENT);
guiPanel.add(serverConnectionStatus);
添加后应该可以工作。
关于Java-BoxLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28142195/
以下用于布局组件的代码返回错误 public class MainPanel extends JPanel { private JTextField txt1, txt2; priva
我试图将 4 个水平盒子布局以垂直方式堆叠在 BoxLayout 中。 我的 KV 文件: : BoxLayout: size: root.size pos:
我知道这里有很多 BoxLayout 问题,但是我找不到可以解决我的问题的问题。我需要我的 ScoreDescPanel 来在彼此正下方显示每个标签(如列表),但是我在使用 BoxLayout 时遇到
我正在使用 BoxLayout 来定位服务器 GUI 的 GUI 元素。我的 JLabels 发生了一件奇怪的事情。我想知道为什么当我将字符串附加到 JTextArea 时,JLabels 会从其原始
我正在尝试制作一个简单的计算器,以熟悉我刚刚在计算机科学入门类(class)中学到的 GUI 的创建。我在按照我想要的方式排列 GUI 时遇到问题。我认为 BoxLayout 是完美的,但每当我尝试使
我有一个非常简单的自定义组件。它的首选最大尺寸为 100x100,并且只是一个红色矩形。我将其添加到使用框布局的 JPanel 中。我预计自定义组件的大小为 100x100,但实际上它是 50x100
我刚刚开始摆弄 BoxLayout 管理器。 我制作了两个相邻的按钮,第三个按钮应该转到下一行(前两个按钮下方),前两个按钮应该位于框架的顶部。 我怎样才能做到这一点? 这是我当前的代码 Bo
我的这段代码似乎抛出了 IllegalComponentException,但我不确定为什么。归结为以下几行代码: JRadioButton setRed = new JRadioButton(“Re
查看了一些以前与我的主题相关的帖子,但也无济于事。 尝试使用 BoxLayout 对齐组件但我无法让它发挥作用。我已经修改了一段时间,得到了不同的结果,但我无法弄清楚。我用的是默认的FlowLayou
我有两个 JPanel,我想将它们排列在一个更大的 JPanel 内部,一个叠在另一个之上。 “panel_controls”覆盖.getPreferredSize()。代码: public fina
你好,我正在尝试以与声明 BorderLayout 相同的方式声明 BoxLayout 声明borderlayout的方式 JPanel textFiled = new JPanel(new Bord
如所附屏幕截图所示 - 黄线来自底层 BoxLayout 面向的 JPanel。更改为 BorderLayout 删除黄线: 代码示例如下: import java.awt.BorderLayout;
是的,我用谷歌搜索了大约 30 分钟。是的,stackoverflow 中有 2 个关于该主题的不同帖子,但这些帖子并没有为我的问题提供任何解决方案。 我正在使用相当多的带有 BoxLayout 的面
我正在尝试在我的程序中制作 BoxLayout 布局。我使用 Eclipse,所以当我输入这些行时, JTextArea fntxt = new JTextArea(1, 20); JTextArea
我正在创建一个具有多个聊天的消息传递程序。在聊天窗口的一侧有一个 JPanel,其中包含用于切换聊天的所有按钮的列表数组。我无法使侧面板中的所有按钮具有相同的宽度,无论它们包含什么。但无论我尝试什么似
我似乎无法在网上找到解决方案来解释为什么我在尝试运行时遇到此错误 我正在为不同的程序制作一个简单的测试系统,当按下按钮时将在文本框中产生值。我希望它们位于不同的行以使其更清晰,所以我研究了布局。我认为
我试图弄清楚如何在运行时将项目插入到 BoxLayout...或使用不同的小部件/面板/布局实现类似的效果。 应用程序窗口的设计是垂直堆叠多个面板(BoxLayout.Y_AXIS)。然后,用户可以插
我正在尝试使用 BoxLayout 而不是我的 GridLayout 代码: setLayout (new GridLayout (5, 2)); 除了我注意到,当你做 BoxLayout 时,你需要
谁能帮帮我。为什么标签“当前”没有在面板/框架中左对齐? public static void main(String[] args) { JFrame TFrame =
我正在尝试沿面板中心的垂直轴对齐多个元素,而 BoxLayout 似乎正是我所需要的。但是,当添加的所有元素都具有奇数宽度时,它似乎会做一些奇怪的事情。 这是演示这种古怪行为的 SSCCE: impo
我是一名优秀的程序员,十分优秀!