【问题标题】:GridBagLayout inside CardLayoutCardLayout 内的 GridBagLayout
【发布时间】:2014-01-18 06:54:57
【问题描述】:

GridBagLayout 和 CardLayout 怎么搭配使用?

我正在尝试将这些按钮添加到面板中,但它破坏了 GridBagLayout,这是文档中的内容;

        button = new JButton("Button 1");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    pane.add(button, c);
 
    button = new JButton("Button 2");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 0;
    pane.add(button, c);

我正在努力:

        JPanel m = new JPanel();
 
    button = new JButton("Button 1");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    m.add(button, c);
 
    button = new JButton("Button 2");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 0;
    m.add(button, c);
    
    
    windows = new JPanel(new CardLayout());
        windows.add(m, "test");
        
    pane.add(windows, c);

但是它会像使用 flowlayout 一样恢复。

我错过了什么?非常感谢任何帮助。

完整

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public final class GUI extends JPanel implements ActionListener {
    
    // buttons
    private JButton newButton, findButton;
    JPanel panels;

    public void addComponentToPane (Container pane) {
        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        
        c.fill = GridBagConstraints.HORIZONTAL;
        
        JPanel mainMenu = new JPanel();

        // add
            mainMenu.add(newButton = new JButton("Add"), c);
                newButton.setActionCommand("new");
                newButton.addActionListener(this);
        
        // find
            mainMenu.add(findButton = new JButton("Find"), c);
                findButton.setActionCommand("find");  
                findButton.addActionListener(this);

        panels = new JPanel(new CardLayout());
            panels.add(mainMenu, "mainWindow");
            
        pane.add(panels, c);
    }

    public void actionPerformed(ActionEvent e) {
        
        CardLayout allpanels = (CardLayout)(panels.getLayout());
        
        switch (e.getActionCommand()) {
            
            case "new":
                allpanels.show(panels, "add");
            break;
            
            case "find":
                System.out.println("find");
            break;
        }
    }

    private static void createGUI () {

        JFrame frame = new JFrame("Check");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GUI contentPane = new GUI();
        contentPane.addComponentToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        createGUI(); 
    }
}

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖MCVE
  • 您将网格包约束添加到您的 mainMenu 但您尚未为此 mainMenu JPanel 定义 GridBagLayout。

标签: java swing layout-manager gridbaglayout cardlayout


【解决方案1】:

你的代码很纠结,但主要问题是:

  • 您正在将 Buttons 添加到 JPanel,并使用 GridBagConstraints 实例作为“约束”参数,但您尚未在此面板上设置 GridBagLayout
  • 您正在尝试切换到 CardLayout 面板中尚未添加(“添加”)的卡片
  • 您说它看起来像 FlowLayout,但您没有设置任何 GridBagConstraints 使其看起来与此不同。

我对您的代码进行了一些重构,并修复了上面提到的这些问题。如果您能提供更多关于您尝试实现的目标的详细信息,我们或许可以给出更好的答案。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public final class GUI extends JPanel implements ActionListener {

    // buttons
    private JButton newButton, findButton;
    JPanel panels;

    public void addComponentToPane(Container pane) {
        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL;

        createPanels();

        pane.add(panels, c);
    }

    private void createPanels() {
        JPanel mainMenu = createMainMenu();

        panels = new JPanel(new CardLayout());
        panels.add(mainMenu, "mainWindow");
        panels.add(new JLabel("Add card"), "add");
        panels.add(new JLabel("Find card"), "find");
    }

    private JPanel createMainMenu() {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.HORIZONTAL;

        JPanel mainMenu = new JPanel(new GridBagLayout());

        // add
        mainMenu.add(newButton = new JButton("Add"), c);
        newButton.setActionCommand("new");
        newButton.addActionListener(this);

        // find
        mainMenu.add(findButton = new JButton("Find"), c);
        findButton.setActionCommand("find");
        findButton.addActionListener(this);
        return mainMenu;
    }

    public void actionPerformed(ActionEvent e) {
        CardLayout allpanels = (CardLayout) (panels.getLayout());

        switch (e.getActionCommand()) {
        case "new":
            allpanels.show(panels, "add");
            break;

        case "find":
            allpanels.show(panels, "find");
            break;
        }
    }

    private static void createGUI() {

        JFrame frame = new JFrame("Check");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GUI contentPane = new GUI();
        contentPane.addComponentToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        createGUI();
    }
}

【讨论】:

  • 非常感谢您的上述内容,我恢复了另一个解决方案,但是您的帖子对我有很大帮助,将来会为我节省很多时间。再次感谢
猜你喜欢
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多