嘿(:这是我第一次在这个论坛上寻求帮助,所以我希望我做的一切都是正确的
好的,我的问题是我用红色标记的。我的 JPanel 使用 BoxLayout.Y_AXIS 方向的 Boxlayout。我想这不是正确的选择。我不希望我的 JPanel(在线用户)、我的 JSeperator 和我的其他 JPanel(管理员)彼此分开。我希望它们直接堆叠在彼此下方。
图形用户界面图片:http: //dl.dropbox.com/u/10049103/notcool.png
我听说我应该能够使用某种“胶水”,但无法按预期工作。我还听说我必须使用 GridBagLayout 但这似乎很复杂,如果有更简单的方法,我真的很想。
这是标记的JPanel的代码(不知道是否需要):
package Client;
import Server.User;
import java.awt.BorderLayout;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
/**
*
* @author Sune
*/
public class OnlineUsersPanel extends JPanel {
private UserWindow owner; public void setOwner(UserWindow uw) { owner = uw; }
private HashMap<String, ShowUserPanel> userPanels = new HashMap<String, ShowUserPanel>();
public OnlineUsersPanel() {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(addStart());
this.add(new JSeparator());
System.out.println("OnlineUsersPanel con called");
}
void addUser(User user) {
ShowUserPanel newPanel = new ShowUserPanel(owner, user.getUserName());
userPanels.put(user.getUserName(), newPanel);
this.add(newPanel);
owner.updateGUI();
}
void deleteUser(User user) {
ShowUserPanel toRemove;
for (Iterator i = userPanels.keySet().iterator(); i.hasNext();) {
String tempUserName = (String) i.next();
if (tempUserName.equals(user.getUserName())) {
toRemove = userPanels.remove(tempUserName);
this.remove(toRemove);
System.out.println("Fundet og fjernet");
break;
}
}
}
private JPanel addStart() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Online Users:"), BorderLayout.LINE_START);
p.add(new JLabel("mute:"), BorderLayout.LINE_END);
return p;
}
我希望有人可以教我如何正确使用这种胶水或告诉我这样做的好方法(: