想要从另一个类/对象数组将值传递给CardLayout

时间:2015-03-25 17:30:53

标签: java swing cardlayout

这是MessageBox类。这使用CardLayout格式显示消息集。它是容器类。与消息一起,我们发送标签上显示的分钟和秒的相应值。

//Reference - SynforgeTutorials
public class MessageBox extends JFrame{
    JPanel p=new JPanel();
    CardLayout cards=new CardLayout();
    int numQs;


    int cardnumber;

    CL1 messages[]={

            new CL1("Hi! How are you doing ?",1,30,this),
            new CL1("I am good thanks",2,0,this),
            new CL1("Its a great dayoutside",0,30,this),
            new CL1("I am going to go hiking",1,0,this),
            new CL1("Maybe I will join you too",1,0,this)

    };


    public static void main(String[] args) {

        new MessageBox();
    } 

    public MessageBox(){        
        super("Message Box");
        setResizable(true);


        //setSize(500,400);
        setBounds(100, 100, 450, 226);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        p.setLayout(cards);

        numQs=messages.length;
        for(int i=0;i<numQs;i++){
            p.add(messages[i],"q"+i);
        }

        cardnumber = 0;

        cards.show(p,"q"+ cardnumber);


        add(p);
        setVisible(true);
    }

这是构建卡片的类。它有3个面板 - 显示消息的顶部面板,标签上显示分钟和秒的中间面板,以及存在OK按钮的底部面板。点击&#39;确定&#39;按钮,显示下一张卡片。

public class CL1 extends JPanel implements ActionListener {
    MessageBox mb;


    //Message
    JPanel qPanel=new JPanel();

    //Timer
    JPanel tPanel=new JPanel();
    JLabel timeLbl = new JLabel("New label");
    JButton btnPause=new JButton("Pause");
    JButton btnResume=new JButton("Resume");

    //bottom
    JPanel botPanel=new JPanel();
    JButton OK=new JButton("OK");



    public CL1(String q, int userMinutes, int userSeconds, MessageBox mb){

        this.mb=mb;

        setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));


        //Message
        qPanel.add(new JLabel(q));
        add(qPanel);


        //Timer

        tPanel.add(timeLbl);
        tPanel.add(btnPause);
        tPanel.add(btnResume);

        add(tPanel);


        //bottom

        //OK.setBounds(323, 139, 97, 25);
        OK.addActionListener(this);

        botPanel.add(OK);

        add(botPanel);
    }
    ////////////////////////////////////////////////////////////////////////////////
    public void actionPerformed(ActionEvent e){
        Object src=e.getSource();
        //OK button
        if(src.equals(OK)){

            mb.OK();}

    }

}

我希望能够从另一个类发送消息,分钟和秒值,并能够多次调用MessageBox类,如下所示。 此类发送消息列表,每个相应消息的分钟和秒。 我想用不同的消息集多次调用MessageBox类。

Calling class
{

public static void main(String[] args) {

        String[] displaymsg1 = {"Hi! How are you doing ?","I am good thanks","Its a great dayoutside","I am going to go hiking","Maybe I will join you too"};
        int[] mins1 = {1,2,0,1,1};
        int[] secs1 = {30,0,30,0,0};
        new MessageBox(displaymsg1 ,mins1,secs1);


        String[] displaymsg2 = {"Monday","Tuesday","Wednesday","Thursday","Friday"};
        int[] mins2 = {0,1,2,3,2};
        int[] secs2 = {0,25,0,10,20};
        new MessageBox(displaymsg2,mins2,secs2);

    } 

}

我该如何解决这个问题?我更改了MessageBox的构造函数以接受参数并创建了for循环。但是,这并不能识别一系列对象和消息[i]&#39;。

public MessageBox(String displaymsg[],int mins[],int secs[]){


        super("Step Message");
        setResizable(true);

        CL1 messages[];

        for(int i=0;i<messages.length;i++)
        {
        messages[i]={

                    new CL1(displaymsg[i],mins[i],secs[i],this)

            };


        }

任何建议将不胜感激。感谢。

1 个答案:

答案 0 :(得分:2)

您的错误/问题与Swing无关,似乎与如何动态创建数组有关。我认为您要做的是将消息变量初始化为长度为displaymsg.length的新CL1数组,然后在for循环中为每个数组创建项目:

public MessageBox(String displaymsg[], int mins[], int secs[]) {
  super("Step Message");
  setResizable(true);

  messages = new CL1[displaymsg.length];

  for (int i = 0; i < messages.length; i++) {
     messages[i] = new CL1(displaymsg[i], mins[i], secs[i], this);
  }
}

话虽如此,我可能会做一些不同的事情。如果您正在做的大部分内容是交换消息,那么您可能甚至不需要CardLayout,而是可以简单地交换单个JLabel中保存的文本。如果不了解更多关于实际需求的话,很难说。