【问题标题】:I am using Card Layout to switch between panels but when I try to make a specific panel appear after pressing a button nothing happens我正在使用卡片布局在面板之间切换,但是当我尝试在按下按钮后显示特定面板时没有任何反应
【发布时间】:2016-07-26 13:02:20
【问题描述】:

我正在尝试获取它,以便在按下下一个按钮时它会转到客户页面,但是当我单击该按钮时没有任何反应。

这是我的代码 包javaapplication1;

import javax.swing.*;
import java.lang.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;

public class Test extends javax.swing.JPanel implements  ActionListener{

JButton firstButton, lastButton, nextButton, previousButton;
JPanel cardPanel;
String cardNames[] = new String[3];
int cardCounter = 0;

public JPanel createContentPane (){

    JPanel totalGUI = new JPanel();

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));

    buttonPanel.add(Box.createRigidArea(new Dimension(10,0)));

    previousButton = new JButton("<- Previous");
    previousButton.addActionListener(this);
    buttonPanel.add(previousButton);
    buttonPanel.add(Box.createHorizontalGlue());        

    firstButton = new JButton("First");
    firstButton.addActionListener(this);
    buttonPanel.add(firstButton);
    buttonPanel.add(Box.createHorizontalGlue());

    lastButton =  new JButton("Last");
    lastButton.addActionListener(this);
    buttonPanel.add(lastButton);
    buttonPanel.add(Box.createHorizontalGlue());

    nextButton = new JButton("Next ->");
    nextButton.addActionListener(this);
    buttonPanel.add(nextButton);
    buttonPanel.add(Box.createRigidArea(new Dimension(10,0)));

    JPanel Shop = new JPanel();
    Shop panel1 = new Shop(); 
    Shop.add(panel1);

    JPanel Items = new JPanel();
    Items panel2 = new Items(); 
    Items.add(panel2); 

    JPanel Customers = new JPanel();
    Customers panel3 = new Customers(); 
    Customers.add(panel3); 

    cardPanel = new JPanel(new CardLayout(150, 50));

    cardNames[0] = "Component.CENTER";
    cardNames[1] = "Component.BOTTOM_ALIGNMENT";
    cardNames[2] = "Component.TOP_ALIGNMENT";

    cardPanel.add(Shop, cardNames[0]);
    cardPanel.add(Items, cardNames[1]);
    cardPanel.add(Customers, cardNames[2]);

    JPanel bottomPanel = new JPanel();
    bottomPanel.setLayout(new BorderLayout());

    bottomPanel.add(cardPanel, BorderLayout.CENTER);
    bottomPanel.add(buttonPanel, BorderLayout.PAGE_START);

    totalGUI.add(bottomPanel);
    return totalGUI;
}

public void actionPerformed(ActionEvent e) {


    if(e.getSource() == nextButton)
    {
        CardLayout cl = (CardLayout)(cardPanel.getLayout());

        cl.show(cardPanel, "Customers");
    }
    /*
    if(e.getSource() == firstButton)
    {
        cl.first(cardPanel);
        cardCounter = 0;
    }
    else if(e.getSource() == lastButton)
    {
        cl.last(cardPanel);
        cardCounter = 2;
    }
    else 
    }
    else if(e.getSource() == previousButton)
    {
        cl.previous(cardPanel);
        if(cardCounter > 0)
        {
            cardCounter--;
        }
        else
        {
            cardCounter = 2;
        }*/
    }


protected static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("");

    Test demo = new Test();
    frame.setContentPane(demo.createContentPane());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

}

为什么会这样?我错过了什么吗? 我只是使用 Cardlayout 的新手,不知道它是如何工作的

【问题讨论】:

    标签: java cardlayout


    【解决方案1】:

    如有疑问,请查看documentation

    来自CardLayout.addLayoutComponent

    constraints 指定的对象必须是字符串。卡片布局将此字符串存储为键值对,可用于随机访问特定卡片。通过调用show方法,应用程序可以显示指定名称的组件。

    换句话说,您传递给 CardLayout.show 的字符串 必须与您在向 CardLayout 添加组件时作为约束提供的字符串之一相同。

    您使用的约束是:

    • “组件.CENTER”
    • “Component.BOTTOM_ALIGNMENT”
    • “Component.TOP_ALIGNMENT”

    这些都不符合您传递给 CardLayout.show 的内容:

    cl.show(cardPanel, "Customers");
    

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 2012-10-05
      • 2016-04-04
      • 1970-01-01
      • 2014-08-09
      • 2022-11-01
      相关资源
      最近更新 更多