【问题标题】:MigLayout - Server browserMigLayout - 服务器浏览器
【发布时间】: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


【解决方案1】:

根据需要使用尽可能多的 JPanel。我会在这里至少使用 2 或 3 个。第一个用于标签和标签窗格,第二个(和第三个)用于其余部分。 下面是如何管理 first 的示例:

import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;

public class App {

    public static void launchView(){
        JFrame frame = new JFrame("Game");
        JLabel servers = new JLabel("Servers");
        Object [][] data = {{"A","B","C","D"}, {"E","F","G","H"}};  //add more components to see the effect
        String [] columnNames = {"Servers", "Game", "Players", "Map"};
        JTable table = new JTable(data, columnNames);    //or proper JTabbedPane

        frame.setLayout(new MigLayout());
        frame.add(servers, "pos 0% 5% 30% 10%");
        frame.add(new JScrollPane(table), "pos 0% 20% 100% 100%");

        frame.setSize(new Dimension(500, 400));
        frame.setVisible(true);
    }

    public static void main(String [] args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                launchView();
            }
        });
    }
}

但是,当然,在 Mig 中通常有不止一种方法来做这件事。 如果您在管理其他组件时仍有问题,请告诉我

【讨论】:

  • 您的意思是示例代码?如果是,你得到什么编译器错误?另外,我相信逻辑是好的,实际上您可以使用这种方法和许多 JPanel 来完成您的所有视图(这并不意味着这将是最有效的)。
猜你喜欢
  • 1970-01-01
  • 2015-06-27
  • 2017-11-09
  • 1970-01-01
  • 2017-12-21
  • 2011-06-11
  • 2014-08-06
  • 2013-01-22
  • 2011-01-11
相关资源
最近更新 更多