Boxlayout 拉伸我的 Jpanels
嘿(:这是我第一次在这个论坛上寻求帮助,所以我希望我已经正确完成了所有事情
好吧,我的问题是我用红色标记的内容。我的 JPanel 使用 Boxlayout 和 BoxLayout.Y_AXIS 方向。我想这不是正确的 JSeperator 和我的其他 JPanel(管理员)彼此分开,我希望它们直接堆叠在彼此下面。
我不希望我的 JPanel(在线用户)、我的 href="http://dl.dropbox.com/u/10049103/notcool.png" rel="nofollow">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;
}
我希望有人可以学习如何正确使用该胶水或告诉我执行此操作的好方法(:
Hey (: This is my first time asking help on this forum so I hope I have done everything correctly
Okay my problem is what I have marked with red. My JPanel uses a Boxlayout with BoxLayout.Y_AXIS orientation. I guess that is not the right choise. I don't want my JPanel(online users), my JSeperator and my other JPanel(admin) to be apart of eachother. I want them to stack directly under eachother.
Pic of GUI: http://dl.dropbox.com/u/10049103/notcool.png
I have heard that I should be able to use some kind of "glue" but could not get that to work as intended. I also heard that I had to use GridBagLayout but that seems very complicated and I would really like if there is an easier way.
Here is the code for the marked JPanel (don't know if needed):
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;
}
I hope someone can learn me how to use that glue properly or tell me the good way to do this (:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:更改为答案:
面板位于彼此之上。问题是容纳两个 JPanel 的容器正在使用什么样的布局?您还打算显示很多用户吗?您需要 JList 或 JTable 吗?您能否展示一张您想要的 gui 外观的图片,并在有典型用户数量时展示它。另请考虑创建并发布 SSCCE。这需要您做一些工作,但非常值得。
编辑1:
在进一步检查您的图像时,我建议您在此处使用 JTable,其中“在线用户”和“静音”作为列标题,并将当前在下面显示的内容、管理和复选框作为 JTable 的一行。将其放入 JScrollPane 中并在当前显示的位置显示它。
Edit: changed to an answer:
The panels are right on top of each other. The question is what kind of layout is being used by the container holding the two JPanels? Also are you doing to display many users? Do you need a JList or JTable there? Can you show a picture of what you want the gui to look like, and show it when there are a typical number of users. Also consider creating and posting an SSCCE. This would take a bit of work on your part, but would be well worth it.
Edit 1:
On further review of your image, I suggest that you use a JTable here with "Online Users" and "Mute" as your column headings and with the stuff you currently show below, the Admin and the checkbox as a row of the JTable. Put it in a JScrollPane and display it where you're currently displaying it.
将两个面板添加到框布局后,执行
此操作会将这两个面板缩小到其首选尺寸,并将空间放在它们下方。
after adding the two panels to the boxlayout do
this will shrink those two panels to their preferred sizes, and put the space below them.