对于那些熟悉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);
}
我花了数小时的时间来寻找解决方案,以解决如何实现这样的服务器浏览器,但是我还没有成功。.我想要做的是使服务器浏览器类似于下面的服务器浏览器
您将如何实现?
最佳答案
根据需要使用尽可能多的JPanels。我将在这里至少使用2或3。首先使用label和tabbedpane,其余使用第二(和第三)。
这是如何管理第一个示例:
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),通常有不止一种方法来做事情。
如果您在管理其他组件方面仍然遇到问题,请告诉我