如何基于JOptionpane输入隐藏jLabel

时间:2018-12-02 01:47:16

标签: java swing jframe jlabel jtextfield

基本上,我单击一个JButton(单足),它弹出带有一个带有几个JTextField的JOptionPane。我想将这些JTextField的String输入和:

  1. 检查以确保字符串值在我具有的类型(posTasks.taskType)的HashMap中,
  2. 然后使用这些字符串作为参数创建一个新的UNIPEDALImpl对象
  3. 如果此HashMap的键在posTasks.completedTasks中用作键时返回true,则使用字符串作为另一个类型(jLabelsHM)的HashMap的键来隐藏JLabel。

我遇到几个错误,我不知道为什么。

unipedal.addActionListener(new java.awt.event.ActionListener() {

    @SuppressWarnings("null")
    @Override
    public void actionPerformed(java.awt.event.ActionEvent evt) {

        String robotName = null;
        String firstTask = null;
        String secondTask = null;
        String thirdTask = null;
        String fourthTask = null;
        String fifthTask = null;

        JPanel inputBox = new JPanel();
        inputBox.setLayout(new GridLayout(0, 2, 3, 4));

        JTextField name = new JTextField(15);
        JTextField task1 = new JTextField(15);
        JTextField task2 = new JTextField(15);
        JTextField task3 = new JTextField(15);
        JTextField task4 = new JTextField(15);
        JTextField task5 = new JTextField(15);

        inputBox.add(new JLabel("Robot's Name:"));
        inputBox.add(name);

        inputBox.add(new JLabel("Task 1:"));
        inputBox.add(task1);

        inputBox.add(new JLabel("Task 2:"));
        inputBox.add(task2);

        inputBox.add(new JLabel("Task 3:"));
        inputBox.add(task3);

        inputBox.add(new JLabel("Task 4:"));
        inputBox.add(task4);

        inputBox.add(new JLabel("Task 5:"));
        inputBox.add(task5);

        int option = JOptionPane.showConfirmDialog(middle,inputBox, 
       "Please fill all the fields", JOptionPane.OK_CANCEL_OPTION);

        if (option == JOptionPane.OK_OPTION) {

             robotName.equals(name.getText());
             firstTask.equals(task1.getText());
             secondTask.equals(task2.getText());
             thirdTask.equals(task3.getText());
             fourthTask.equals(task4.getText());
             fifthTask.equals(task5.getText());


            if (!posTasks.taskType.containsKey(firstTask)||!posTasks.taskType.containsKey(secondTask)||!posTasks.taskType.containsKey(thirdTask)||!posTasks.taskType.containsKey(fourthTask)||!posTasks.taskType.containsKey(fifthTask)) {
                throw new IllegalArgumentException("One or more of the tasks you have requested are invalid. Please choose tasks from the list provided and check your spelling!");
            }
            try {
                UNIPEDALImpl unipedal = new UNIPEDALImpl(firstTask, secondTask, thirdTask, fourthTask,
                        fifthTask);

                LinkedList<String>tasksList = new LinkedList <String>();
                tasksList.add(firstTask);
                tasksList.add(secondTask);
                tasksList.add(thirdTask);
                tasksList.add(fourthTask);
                tasksList.add(fifthTask);

                for (String task : tasksList) {

                    if (posTasks.completedTasks.get(task) == true) {
                        throw new IllegalArgumentException(task + "has already been completed. Please choose a different task or use 'refresh' if you want the task to be completed again");
                    }

                    unipedal.time += unipedal.taskTimes.get(task);
                    posTasks.completedTasks.put(task, true);
                    unipedal.tasksList.remove(task);
                    unipedal.taskCounter++;
                }

            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

            for (Entry<String, JLabel> entry : jLabelsHM.entrySet()) {
                if(posTasks.completedTasks.get(entry.getKey()) == true) {
                    jLabelsHM.get(entry.getKey()).setVisible(false);
                }
            }
        }
    }
});

1 个答案:

答案 0 :(得分:1)

首先,我们这里没有UNIPEDALImpl类,因此在黑盒视图中,我无法告诉您该部分是否存在错误。

关于代码中的问题。首先,似乎您想使用equals方法填充变量,这是错误的,您需要使用 =符号

相等方法用于比较两个未分配变量的对象。

robotName = name.getText();
firstTask = task1.getText();
secondTask = task2.getText();
thirdTask = task3.getText();
fourthTask = task4.getText();
fifthTask = task5.getText();

然后您从那里搬走。