【发布时间】:2015-08-11 19:31:53
【问题描述】:
对于那些熟悉 MigLayout 或基本上 Swing 用法的人,我有一个问题。
我正在尝试为游戏创建服务器浏览器。
布局最终将如下所示。另外,老实说,我不知道该使用什么布局管理器。
+---------+---------+---------+---------+---------+---------+---------+-------------------+---+-----+
| tab1 | tab2 | tab3 | tab4 | tab5 | tab6 | tab7 | | X | |
+---------+---------+---------+---------+---------+---------+---------+ +---+ |
| |
+-----------+--------------------+---------+-----------+ |
| Country | Servers | Players | Game mode | |
+-----------+--------------------+---------+-----------+ |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+-----------+--------------------+---------+-----------+ |
| |
| |
| +---------------+ |
| | Username | |
| +---------------+ |
| |
| +-------------+ +-------------+ |
| | Spectate | | Play | |
| +-------------+ +-------------+ |
| |
| |
+---------------------------------------------------------------------------------------------------+
调整窗口大小时,所有组件都应填充 x 轴。就像一个带有动态 TabbedPane 的动态面板。
到目前为止,这是我的代码:
/**
* A class handling the server browser.
* @author Jamie
*/
public class ServerBrowser extends JPanel {
/**
* Handle the server browser from client.
* @param c The client reference in case it was used in-game.
* @param m The main reference in case it was used in-game.
* TODO @return reference
*/
public Client c;
public Main m;
private int width = 400;
private int height = 500;
private JTabbedPane tabbedPane;
public ServerBrowser(Main m, Client c) {
this.m = m;
this.c = c;
this.setLayout(new BorderLayout());
this.setBorder(new LineBorder(Color.GRAY, 1, true));
// Creating the object and template
tabbedPane = new JTabbedPane();
this.add(tabbedPane);
/**
* Handle the tabs from the tabbed pane.
*/
JPanel internetTab = new JPanel();
JPanel customTab = new JPanel();
JPanel favoritesTab = new JPanel();
JPanel historyTab = new JPanel();
JPanel spectateTab = new JPanel();
JPanel lanTab = new JPanel();
JPanel friendsTab = new JPanel();
// Creates the label to go in each of the tabs
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
JLabel label4 = new JLabel();
JLabel label5 = new JLabel();
JLabel label6 = new JLabel();
JLabel label7 = new JLabel();
//Set the text of each of the labels in the tabbed panes.
label1.setText("This tab will display a list of all the servers.");
label2.setText("What should this tab display?");
label3.setText("What should this tab display?");
label4.setText("What should this tab display?");
label5.setText("What should this tab display?");
label6.setText("What should this tab display?");
label7.setText("What should this tab display?");
//Add the labels to the specific tabs
internetTab.add(label1);
customTab.add(label2);
favoritesTab.add(label3);
historyTab.add(label4);
spectateTab.add(label5);
lanTab.add(label6);
friendsTab.add(label7);
// Create test JTextArea and place at certain x,y coordinate
JTextArea unText1 = new JTextArea();
internetTab.add(unText1);
unText1.setBounds(200,200,150,50);
// Name the tabs and add them into the Tabbed Pane object
tabbedPane.addTab(" Internet ", internetTab);
tabbedPane.addTab(" Custom ", customTab);
tabbedPane.addTab(" Favorites ", favoritesTab);
tabbedPane.addTab(" History ", historyTab);
tabbedPane.addTab(" Spectate ", spectateTab);
tabbedPane.addTab(" Lan ", lanTab);
tabbedPane.addTab(" Friends ", friendsTab);
// Buttons
JButton test = new JButton("Press");
customTab.add(test);
// Action listener
ButtonHandler phandler = new ButtonHandler();
test.addActionListener(phandler);
this.setVisible(true);
public void handleResize() {
// Center
//int x = (m.getWidth() - width) / 2;
int x = m.getWidth() - width - 25;
int y = (m.getHeight() - height - m.mb.getHeight()) / 3;
this.setBounds(x,y,width,height);
}
我花了好几个小时想出一个解决方案来解决如何获得像这样的服务器浏览器,但我还没有成功。我想要的是制作一个类似于下面的服务器浏览器
您将如何实现这一目标?
【问题讨论】:
-
如果你使用eclipse你可以选择并安装这个插件:eclipse.org/windowbuilder。这是在编码过程中配置和查看 GUI 的完美工具。它也支持 Mig-Layout。
-
我正在使用 Eclipse。就我个人而言,我不喜欢使用 WindowBuilder 插件,主要是因为我认为它不像人们想象的那样用户友好。
-
我支持你的意见。但是我在上一个项目中使用了这个构建器(基于一个复杂的 GUI),我的 expreinec 非常适合这个工具。
-
如果你能给我一些关于如何正确使用插件以实现我想要的东西的体面参考;我很乐意试一试。我花了几个小时和几天试图弄清楚这个案例。老实说,我不知道从哪里开始。
-
我会远离 Mig 布局,因为管理起来很痛苦。
标签: java swing jtabbedpane miglayout