java - 定心 Swing 组件

标签 java swing layout components

如何使文本字段和按钮居中?

编辑:用 SSCE 更新了我的帖子。希望这有帮助。

顺便说一下,我希望左图看起来像右图。

package pkg;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;

public class Game {

private static JPanel panel = new JPanel();
public static JTextField username = new JTextField(20);
public static JPasswordField password = new JPasswordField(20);
JButton login = new JButton("Login");
JLabel status = new JLabel();
private static JPanel game = new JPanel();
private JButton logout = new JButton("Logout");
private static JFrame frame = new JFrame("RuneShadows");

public Game() {
    panel.add(username);
    panel.add(password);
    panel.add(login);
    panel.add(status);

    game.add(logout);
    frame.add(panel);
    frame.setSize(806, 553);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

public static void main(String[] args) {
    new Game();
}

public void loadGame() {
    frame.remove(panel);
    frame.revalidate();
    frame.add(game);
    frame.revalidate();

    logout.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.remove(game);
            frame.revalidate();
            frame.add(panel);
            frame.revalidate();
            try {
                Client.socketOut.writeUTF("logout");
                Client.socketOut.writeUTF(Client.username);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });
}
}

我希望它看起来像什么: enter image description here

我尝试过使用许多不同的布局样式,但似乎无法使其工作......

最佳答案

enter image description here

import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
    import javax.swing.*;
    import javax.swing.border.*;

    public class Game {

    private static JPanel panel = new JPanel();
    public static JTextField username = new JTextField(20);
    public static JPasswordField password = new JPasswordField(20);
    JButton login = new JButton("Login");
    JLabel status = new JLabel();
    private static JPanel game = new JPanel(new FlowLayout(FlowLayout.CENTER));
    private JButton logout = new JButton("Logout");
    private static JFrame frame = new JFrame("RuneShadows");

    public Game() {
        panel.setLayout(new GridLayout(0,1,15,15));
        panel.setBorder(new EmptyBorder(50,100,50,100));
        panel.add(username);
        panel.add(password);
        panel.add(status);

        JPanel logoutConstrain = new JPanel(new FlowLayout(FlowLayout.CENTER));
        logoutConstrain.add(logout);
        panel.add(logoutConstrain);
        frame.setLayout(new GridBagLayout());
        frame.add(panel);
        //frame.setSize(806, 553);  // forget this nonsense, instead..
        frame.pack();  // best!
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Game();
    }

    public void loadGame() {
        frame.remove(panel);
        frame.revalidate();
        frame.add(game);
        frame.revalidate();

        logout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.remove(game);
                frame.revalidate();
                frame.add(panel);
                frame.revalidate();
            }
        });
    }
    }

关于java - 定心 Swing 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20826493/

相关文章:

java - 在 android 中与发件人一起列出 SMS 时性能缓慢

java - 释放按键时停止玩家移动

ios - UICollectionView自定义单元格布局

java - 如何在Gradle useTestNG中使用testng参数

java - 使用循环和通用 vfs 无法从 .iso 获取文件系统。 (JAVA)

Java Swing 阿拉伯语文本反转方向

java - 如何让 jButton.setVisible() 立即工作?

css - 保持图像水平对齐无间隙的最简单方法?

css - 带边距的全屏 div

java - 正则表达式获取“符号”之间的文本