米格布局不良行为

时间:2016-01-23 21:49:05

标签: java layout jcomponent miglayout

如果这是一个愚蠢的问题,我很抱歉,但我找不到答案。我正在尝试为我的java程序创建简单的登录页面。它包含JLayeredPane设置和登录。还有一个JSplitPane,它包含两个JSrollpane控制台面板和changelogpanel。所以问题是我添加了JSplitPane和Settings layeredpane,但是当我添加login layeredpane时,它与settingspanel的行相同,但它也在splitpane旁边,所以它看起来像这样:

[----]
[ - ] ... [ - ]

它应该是:

[----]
[ - ] [ - ]

Here is screenshot

Main.java

package Main;

import javax.swing.JFrame;

import Development.Version;
import GameEngine.GameEngine;

public class Main {

     private static String title = "2D SquareWorld 0.";

     private static JFrame window;

     public static void main(String[] args) {

          GameEngine game = new GameEngine();

          window = new JFrame();

          window.setTitle("2D SquareWorld 0." + Version.newVersion());
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          window.setResizable(false);
          window.add(game);
          window.add(new GUI());
          window.pack();
          window.setSize(1000, 720);
          window.setLocationRelativeTo(null);
          window.setVisible(true);

    }

}

GUI.java

package Main;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import net.miginfocom.swing.MigLayout;

public class GUI extends JPanel {

    private static final long serialVersionUID = 1L;

    private JSplitPane Splitpanel;
    private JScrollPane consolepanel, changelogpanel;
    private JPasswordField password;
    private JLabel usernametext, passwordtext;
    private JButton update, register, login;
    private JCheckBox keepLogged;
    private JTextField username, server;
    private JTextPane console, changelog;
    private JLayeredPane loginlayer, settingslayer;

    public GUI() {

        setLayout(new MigLayout());

        settingslayer = new JLayeredPane();
        settingslayer.setBorder(BorderFactory.createTitledBorder(""));

        loginlayer = new JLayeredPane();
        loginlayer.setBorder(BorderFactory.createTitledBorder(""));

        username = new JTextField();

        password = new JPasswordField();

        usernametext = new JLabel("Username:");

        passwordtext = new JLabel("Password:");

        update = new JButton("Update");

        register = new JButton("Register");

        login = new JButton("Login");

        keepLogged = new JCheckBox("Keep me logged in");

        server = new JTextField();
        server.setEditable(false);
        server.setText("jdbc:mysql://sql4.freemysqlhosting.net");

        loginlayer.add(server);
        loginlayer.add(keepLogged);
        loginlayer.add(login);
        loginlayer.add(update);
        loginlayer.add(register);
        loginlayer.add(usernametext);
        loginlayer.add(passwordtext);
        loginlayer.add(username);
        loginlayer.add(password);

        console = new JTextPane();
        console.setContentType("text/html");
        console.setEditable(false);
        console.setText("<center><h1><u>Console:</u></h1></center>");

        changelog = new JTextPane();
        changelog.setContentType("text/html");
        changelog.setEditable(false);
        changelog.setText("<center><h1><u>Changelog:</u></h1></center>");

        consolepanel = new JScrollPane(console);
        consolepanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        consolepanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        changelogpanel = new JScrollPane(changelog);
        changelogpanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        changelogpanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        Splitpanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, consolepanel, changelogpanel);
        Splitpanel.setOneTouchExpandable(true);
        Splitpanel.setDividerLocation(480);

        add(Splitpanel, "w 100%, h 80%, wrap");
        add(settingslayer, "w 50%, h 20%");
        add(loginlayer, "w 50%, h 20%");

    }

}

我很抱歉我的英语不好。这是我在stackoverflow中的第一个问题所以请告诉我,如果我做错了什么。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

add(Splitpanel, "w 100%, h 80%, wrap");

应该是

add(Splitpanel, "w 100%, h 80%, spanx, wrap");