0
如果這是愚蠢的問題,我很抱歉,但我找不到答案。我正在嘗試使簡單的登錄頁面到我的Java程序。它包含JLayeredPane設置和登錄。還有一個JSplitPane包含兩個JSrollpane consolepanel和changelogpanel。所以問題是我已經添加了JSplitPane和設置分層面板,但是當我添加登錄分層面板時,它與設置面板屬於同一行,但它也緊鄰分割面板,因此它看起來像這樣:米格佈局不良行爲
[ - ]
[ - ] ... [ - ]
,它應該是:
[----]
[ - ] [ - ]
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中的第一個問題,所以只是告訴我,如果我做錯了什麼。感謝幫助!
提示:在Java中,你不大寫字母開頭的變量名,並使用駱駝情況。 「Splitpanel」應該是「splitPanel」。 –
謝謝你對我而言是新的:) – TheProgrammer