【问题标题】:Adding jpanels created from the same class to a cardlayout frame将从同一类创建的 jpanel 添加到卡片布局框架
【发布时间】:2023-03-12 18:36:01
【问题描述】:

我有一个类可以读取 excelfile 并在 jpanel 内创建带有图形的框架。我通过 jmenuitem 中的 actionlistener 调用这个类。然后我有另一个 jmenuitem,它调用打开同一个文件的同一个类,但读取不同的 excel 表,并给出不同的图形(这是类中唯一更改的字符串)。具有这些 jmenitem 的 jmenubar 属于程序启动的 jframe。我想知道是否有可能每次单击创建要添加到新 jframe 卡片布局中的图形的 jmenuitems,以便我可以滚动它们。提前致谢

这是我当前用于在单击 jmenuitem 时在 jframe 中打开图形的代码:

public class startup extends JFrame  {   // creates a jframe with some stuff and the jmenubar

public void menu() {
...


                    menuItem.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent event2) {

                        new Thread(new Runnable() {
                                @Override
                                public void run() {
                          new ReadExcel();
                                    ReadExcel.excel(".xls", 0);  // this jmenuitem invokes the class to read the excelfile sheet 0
graphgen.main(null);
                                }
                            }).start();
                        }
                    });

                    subsubmenu1.add(menuItem);


menuItem.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent event2) {

                        new Thread(new Runnable() {
                                @Override
                                public void run() {
                          new ReadExcel();
                                    ReadExcel.excel(".xls", 1);  // this jmenuitem invokes the class to read the excelfile sheet 1

graphgen.main(null);
                                }
                            }).start();
                        }
                    });

                    subsubmenu1.add(menuItem);

....
}


          public static void main(String[] args)
            {


                GUIquery frame = new GUIquery();
                p.add(graphComponent, BorderLayout.CENTER);
                frame.setLayout(new BorderLayout());
                frame.add(p, BorderLayout.CENTER);
                frame.setJMenuBar(GUIquery.createMenuBar());
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(true);
                frame.setSize(1600, 1200);
                frame.setVisible(true);


            }



    }

readexcel 类只是读取一个 excelfile 的 excelsheet 并返回一些在 graphgen 类中处理的数组列表。

public class graphgen extends JFrame {

public graphgen() {

        super("Results");



        gen();

    }

    public void gen(){

//creates the graphcomponent

getContentPane().add(graphComponent);
    add(graphComponent);

}

    public static void main(String[] args)
    {


        graphgen frame = new graphgen();
        p2.add(graphComponent, BorderLayout.CENTER);

    frame.add(p2, BorderLayout.CENTER);
        frame.pack();
        frame.setResizable(true);
        frame.setSize(1600, 1200);
        frame.setVisible(true);

}

【问题讨论】:

  • “我想知道是否有可能每次单击创建要添加到新 jframe 卡片布局中的图形的 jmenuitems,..” 嗯..是的。如需更具体的答案,请提出更具体的问题,并发布SSCCE

标签: java swing jframe cardlayout


【解决方案1】:

使用Action封装目标组件、文件和工作表,如图herehere。添加一个方法以根据所选工作表更新类的状态。在卡片之间导航的示例见 herehere。另请参阅卡片布局操作,引用 here

【讨论】:

  • 对不起,我不明白。我对这件事有点迷茫,因为我搜索了这么多,却无法通过。 @trashgod
  • 似乎在这一行Examples of navigating among cards are seen here and here,两个链接都指的是同一个东西:-)
  • @nIcEcOw:很好的收获!已更新。
  • @user2598911:请更新您的 sscce 以显示您遇到的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多