簡體   English   中英

帶有GridbagLayout問題的JPanel中帶有Gridbaglayout的Jpanels

[英]Jpanels with Gridbaglayout inside a JPanel with GridbagLayout problems

我正在嘗試使CardLayout正常工作。 -我的甲板上有第一張“卡片”,我將其稱為firstPanel(Gridbaglayout)。在該面板內,我需要其他具有相同布局(Gridbaglayout)的面板,其中包含一些組件。

作為此處下的示例-我展示了一個我希望在我的firstPanel(Jpanel)內部使用Gridbaglayout的JPanels,名為textFieldForPlayers。

我希望你明白我的意思。 如果沒有,我將嘗試更詳細地解釋。 :)

public void run() {
        deck.setLayout(cl);
        c = new GridBagConstraints();
        firstPanel.setLayout(new GridBagLayout());
        secondPanel.setLayout(new GridBagLayout());
        thirdPanel.setLayout(new GridBagLayout());
        GamePanel gamePanel = new GamePanel();
        c.gridx = 0;
        c.gridy = 0;
        firstPanel.add(textFieldForPlayers(humanPLayers), c);
        c.gridx = 0;
        c.gridy = 1;
        firstPanel.add(botPreferences(), c);
        c.gridx = 0;
        c.gridy = 2;
        firstPanel.add(next = new JButton("Next"), c);
        c.gridx = 0;
        c.gridy = 0;
        secondPanel.add(new JLabel("Bot preferences"), c);
        c.gridx = 0;
        c.gridy = 0;
        thirdPanel.add(gamePanel, c);
        deck.add(firstPanel, "1");
        deck.add(secondPanel, "2");
        deck.add(thirdPanel, "3");
        cl.show(deck, "1");
        events();
    }
private JPanel textFieldForPlayers(int hplayers) {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        c = new GridBagConstraints();
        JLabel text = new JLabel("Name of the human players");
        c.gridx = 0;
        c.gridy = 0;
        panel.add(text, c);
        boxes = new ArrayList<>();
        boxes.add(new JTextField());
        boxes.add(new JTextField());
        boxes.add(new JTextField());
        boxes.add(new JTextField());
        boxes.add(new JTextField());
        boxes.add(new JTextField());
        for (int i = 1; i <= hplayers; i++) {
            boxes.get(i).setPreferredSize(new Dimension(165, 18));
            c.gridx = 1;
            c.gridy = i;
            panel.add(boxes.get(i), c);
            c.gridx = 0;
            c.gridy = i;
            panel.add(new JLabel("Player " + i + ": "), c);
        }

        return panel;
    }

圖片3-這是現在的樣子

在此處輸入圖片說明

圖片4-應該是-頂部1,中間2,底部3。 他們都是中心人物。

在此處輸入圖片說明

您的CardLayout cl不能表現為真正的CardLayout,這表明未顯示的代碼有問題。 我自己嘗試將gui創建模塊化,包括使用單獨的實用程序方法來幫助創建gridbagconstraints(如果需要任何復雜的約束)。

例如:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.border.Border;

@SuppressWarnings("serial")
public class GridBagEg extends JPanel {
    public static final int PLAYER_COUNT = 5;
    private CardLayout cardLayout = new CardLayout();
    private JPanel deckPanel = new JPanel(cardLayout);
    private NextAction nextAction = new NextAction("Next");
    private PlayerPanel playerPanel = new PlayerPanel(PLAYER_COUNT);
    private BotDifficultyPanel botDifficultyPanel = new BotDifficultyPanel();

    public GridBagEg() {
        deckPanel.add(playerPanel, PlayerPanel.NAME);
        deckPanel.add(botDifficultyPanel, BotDifficultyPanel.NAME);

        JPanel nextBtnPanel = new JPanel();
        nextBtnPanel.add(new JButton(nextAction));

        setLayout(new BorderLayout());
        add(deckPanel, BorderLayout.CENTER);
        add(nextBtnPanel, BorderLayout.PAGE_END);
    }

    private class NextAction extends AbstractAction {
        public NextAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(0);
            putValue(MNEMONIC_KEY, mnemonic);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            cardLayout.next(deckPanel);
        }
    }

    private static void createAndShowGui() {
        GridBagEg mainPanel = new GridBagEg();

        JFrame frame = new JFrame("GridBagEg");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

@SuppressWarnings("serial")
class BotDifficultyPanel extends JPanel {
    public static final String NAME = "bot difficulty panel";
    public static final String[] LEVELS = {"Easy", "Mid-level", "Difficult", "Holy Mother of God Difficulty"};
    private JComboBox<String> difficultyCombo = new JComboBox<>(LEVELS);

    public BotDifficultyPanel() {
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;

        add(new JLabel("Bot Difficulty:"), gbc);
        gbc.gridx = 1;
        gbc.insets = new Insets(0, 10, 0, 0);
        add(difficultyCombo, gbc);
    }

    public String getSelectedDifficulty() {
        String selection = (String) difficultyCombo.getSelectedItem();
        return selection;
    }

}

@SuppressWarnings("serial")
class PlayerPanel extends JPanel {
    public static final String NAME = "player panel";
    private static final String TITLE = "Name of Human Players";
    private static final int EB_GAP = 10;
    private static final int FIELD_COLUMNS = 15;
    private static final int INS_GAP = 5;
    private int playerMaxCount = 0;
    private List<JTextField> playerFields = new ArrayList<>();

    public PlayerPanel(int playerMaxCount) {
        this.playerMaxCount = playerMaxCount;

        Border outsideBorder = BorderFactory.createTitledBorder(TITLE);
        Border insideBorder = BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP);
        setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));
        setLayout(new GridBagLayout());

        for (int i = 0; i < playerMaxCount; i++) {
            JTextField playerField = new JTextField(FIELD_COLUMNS);
            playerFields.add(playerField);

            add(new JLabel("Player " + i + ":"), createGbc(0, i));
            add(playerField, createGbc(1, i));
        }
    }

    public String getFieldName(int index) {
        if (index < 0 || index >= playerFields.size()) {
            String text = "for playerFields index of " + index;
            throw new IllegalArgumentException(text);
        } else {
            return playerFields.get(index).getText();
        }
    }

    public int getPlayerMaxCount() {
        return playerMaxCount;
    }

    private GridBagConstraints createGbc(int x, int y) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;

        // if x is 0, anchor to the left otherwise to the right
        gbc.anchor = x == 0 ? GridBagConstraints.WEST : GridBagConstraints.EAST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(INS_GAP, INS_GAP, INS_GAP, INS_GAP);
        if (x == 0) {
            gbc.insets.right = 4 * INS_GAP; // increase gap in between
        }
        return gbc;
    }

}

喜歡:

CardLayout cl = new CardLayout();

JPanel contentPane = new JPanel();
JPanel firstPanel = new JPanel();
JPanel secondPanel = new JPanel();
contentPane.setlayout(cl);


firstPanel.add(new JButton("1"));
secondPanel.add(new JButton("2"));

contentPane.add(firstPanel, "1");
contentPane.add(secondPanel, "2");

cl.show(contentPane, "1");

因此,現在contentPane內部包含2個JPanels。 現在我們將在firstPanel上看到所有內容,對不對? :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM