gpt4 book ai didi

java - MigLayout - 服务器浏览器

转载 作者:行者123 更新时间:2023-12-01 11:15:04 26 4
gpt4 key购买 nike

我有一个问题想问那些熟悉 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);
}

我花了很多时间想出一个解决方案来实现这样的服务器浏览器,但我还没有成功..我想要的是制作一个类似于下面的服务器浏览器 enter image description here

您将如何实现这一目标?

最佳答案

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

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 中通常有不止一种方法可以做到这一点。如果您在管理其他组件时仍然遇到问题,请告诉我

关于java - MigLayout - 服务器浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31934100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com