首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GridBagLayout内部CardLayout

GridBagLayout内部CardLayout
EN

Stack Overflow用户
提问于 2014-01-18 06:54:57
回答 1查看 1.2K关注 0票数 0

如何将GridBagLayout和CardLayout结合使用?

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

代码语言:javascript
复制
        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);

我想做的是:

代码语言:javascript
复制
        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);

但是,它会恢复,就好像它在使用流布局。

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

完整

代码语言:javascript
复制
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(); 
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-19 07:56:54

您的代码非常混乱,但主要问题是:

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

我已经重构了您的代码,并修复了上面提到的这些问题。如果你能提供更多关于你想要达到的目标的细节,我们也许能给出一个更好的答案。

代码语言:javascript
复制
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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21201103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档