gpt4 book ai didi

Java-BoxLayout

转载 作者:行者123 更新时间:2023-12-01 22:29:19 26 4
gpt4 key购买 nike

我正在使用 BoxLayout 来定位服务器 GUI 的 GUI 元素。我的 JLabels 发生了一件奇怪的事情。我想知道为什么当我将字符串附加到 JTextArea 时,JLabels 会从其原始位置移动。所以第一个图是位于正确位置的 JLabels。第二张图片是 JLabels 倾斜的

图1

enter image description here

图2

enter image description here

下面是服务器 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/

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