简体   繁体   English

单击独立于其余jPanel的按钮时,创建一个新的相同jPanel

[英]Creating a new identical jPanel when clicking a button that is independent from the rest of the jPanels

I'm trying to create an task-list type of applet with a jPanel within a jFrame. 我正在尝试在jFrame中使用jPanel创建小程序的任务列表类型。 The jPanel has items such as an Text Input block, a slider, a "Complete Task" button, and a "Clear Task" button. jPanel具有诸如文本输入块,滑块,“完成任务”按钮和“清除任务”按钮之类的项目。 I also have an "Add Task" button that will ask a user the name of the new task and then create an identical jPanel with the same configuration. 我还有一个“添加任务”按钮,它将询问用户新任务的名称,然后使用相同的配置创建相同的jPanel。 My question is, how do I create the new jPanel with the same buttons/sliders/input box but these have a different variable name every time the "Add Task" button is clicked? 我的问题是,我如何使用相同的按钮/滑块/输入框创建新的jPanel,但是每次单击“添加任务”按钮时它们具有不同的变量名? I attempted to do this by creating a separate method that takes a random number, converts it into a string, and then use that string value as the name of the new jPanel...it gives me an error saying "double cannot be dereferenced". 我试图通过创建一个采用随机数的单独方法,将其转换为字符串,然后使用该字符串值作为新jPanel的名称来执行此操作,这给我一个错误,指出“无法取消引用双精度” 。

public void createNewTask(){
  double panelTask = Math.random();
  Double.toString(panelTask);

  javax.swing.GroupLayout panelTaskLayout = new javax.swing.GroupLayout(panelTask);
  panelTask.setLayout(panelTaskLayout);
  panelTaskLayout.setHorizontalGroup(
        panelTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(panelTaskLayout.createSequentialGroup()
            .addContainerGap()
                .addGroup(panelTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelTaskLayout.createSequentialGroup()
                    .addComponent(completeTask, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                    .addGap(18, 18, 18)
                    .addComponent(clearTask, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(inputText)
                .addComponent(sliderExample, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap())
    );
    panelTaskLayout.setVerticalGroup(
        panelTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelTaskLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(inputText, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(sliderExample, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(10, 10, 10)
            .addGroup(panelTaskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(completeTask, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                .addComponent(clearTask, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
}

I'm not sure why exactly you want to create a whole new panel with the exact same components, but I do see you've pretty much just copy and pasted the auto-generated code from the GUI Builder tool and tried to paste it into a method, to recreate the panel. 我不确定为什么您要创建一个具有完全相同组件的全新面板,但我确实看到您几乎只是复制并粘贴了来自GUI Builder工具的自动生成的代码,然后尝试将其粘贴到一种重新创建面板的方法。 I've never tried it myself, but it just looks wrong. 我自己从未尝试过,但是看起来很不对劲。

Without clarification of your requirements, I'd just suggest you use a CardLayout to switch between views. 在不明确您的要求的情况下,我只建议您使用CardLayout在视图之间切换。 You could create a JPanel form with the builder tool and use the same form twice, once for each card. 您可以使用构建器工具创建一个JPanel表单,并使用同一表单两次,每张卡一次。 You can see more at How to Use CardLayout with NetBeans GUI Builder and see How to Use CardLayout Oracle tutorial for the basics of what CardLayout has to offer. 您可以在如何与NetBeans GUI Builder 一起使用CardLayout以及在如何使用CardLayout Oracle教程中了解更多有关CardLayout必须提供的基础知识。

Or perhaps, reuse a JDialog to get user input for a Task . 或者,也许可以重用JDialog来获取Task用户输入。

Or maybe instead, a better option would be to have model objects that hold the state for each (Task, or whatever, I'm not sure). 或者,也许相反,更好的选择是让模型对象保存每个对象的状态(任务或其他不确定的对象)。 Say class Task that holds the state for fields and slider values. 说出类Task ,其中包含字段和滑块值的状态。 Or just use a JTable to hold the state for each task. 或者只是使用JTable来保存每个任务的状态。

But really, I don't see the point of two identical panels. 但实际上,我看不到两个相同的面板的意义。 So please offer some more detail as to why you want two identical panels, and what different functionality do they provide, that you would need two. 因此,请提供更多详细信息,说明为什么需要两个相同的面板,以及它们需要提供两个不同的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM