Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为每种不适用于表达式类型的表达式求解a?_Java_Foreach - Fatal编程技术网

Java 如何为每种不适用于表达式类型的表达式求解a?

Java 如何为每种不适用于表达式类型的表达式求解a?,java,foreach,Java,Foreach,removePanel()用于使用getComponent()和增强循环获取main面板中的组件。然后检查哪些组件是可见的&在该组件上调用removeLayoutComponent() 这是REMOVEJPANEL方法 mainPanel使用卡布局,然后将panel阵列中的jpanel添加到mainPanel上 这是我创建主面板并将JPanel添加到其中的代码 public TicTacToeFrame () { if (badge.num == 0) { playerX = JOpti

removePanel()
用于使用
getComponent()
和增强循环获取
main面板中的组件。然后检查哪些组件是可见的&在该组件上调用
removeLayoutComponent()

这是REMOVEJPANEL方法

mainPanel
使用卡布局,然后将
panel
阵列中的jpanel添加到
mainPanel

这是我创建主面板并将JPanel添加到其中的代码

public TicTacToeFrame () {
 if (badge.num == 0) {
   playerX = JOptionPane.showInputDialog ("Enter Player X name: ");
   playerO = JOptionPane.showInputDialog ("Enter Player O name: ");
   badge.setLabel (playerX);
 }
   mainPanel = new JPanel ();//THIS IS WHERE I CREATE MAINPANEL
   mainPanel.setLayout (card);//THIS IS WHERE CARDLAYOUT IS SET AS LAYOUT MANAGER
   panels [0] = new JPanel (new GridLayout (3, 3, 0, 0));
   panels [1] = new JPanel (new GridLayout (3, 3, 0, 0));
   panels [2] = new JPanel (new GridLayout (3, 3, 0, 0));
   JPanelArray ();

   panels [0].setBorder (new LineBorder (Color.red, 1));
   panels [1].setBorder (new LineBorder (Color.red, 1));
   panels [2].setBorder (new LineBorder (Color.red, 1));

   mainPanel.add (panels [0], "one");//JPANELS FROM THE PANEL ARRAY ARE ADDED TO THE MAINPANEL
   mainPanel.add (panels [1], "two");
   mainPanel.add (panels [2], "three");

   badge.jlblStatus.setBorder (new LineBorder (Color.yellow, 1));

   add (mainPanel, BorderLayout.CENTER);
   add (badge.jlblStatus, BorderLayout.SOUTH);
}
预期的结果是播放器单击JDialog按钮并执行
removePanel()
。相反,发生的是当前面板没有被删除&出现了一个不适用于表达式类型的
编译错误

这就是调用REMOVEJPANEL()的地方

编译错误


我在这个网站上找到了这个,但这并不能满足我的需要,因为我还是java的初学者,所以我很难理解页面上的内容。

JPanel.getComponent(int)
只返回一个组件,即位于传入索引位置的组件。如果要迭代JPanel中的所有组件,可以使用
JPanel.getComponents
,如下所示:

public void removeJPanel () {
 Component[] components : mainPanel.getComponents();
 for (int i = 0; i < components.length; i++) {
    Component comp = components[i];
    if (comp.isVisible () == true) {
        card.removeLayoutComponent (comp);
        number++;
    } else {
      System.out.print("It didn't work!");
    }
 }
}
public void removeJPanel(){
组件[]组件:mainPanel.getComponents();
对于(int i=0;i
getComponent()
返回单个组件。你认为你会循环什么?哦,我应该用getComponents()来代替标题。对不起,我不明白你所说的“Arrays.asList(getComponents())”是什么意思。你能详细说明一下吗?如果这有助于解决问题的话。
public void replayDialog () {
   int response = JOptionPane.showConfirmDialog (this, "Do you want to play again ?", "TicTacToe", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
   if (response == JOptionPane.YES_OPTION) { 
       frame.removeJPanel ();
       frame.newCells = null;
       label.num++;
       frame.JPanelArray ();
   } else if (response == JOptionPane.NO_OPTION) {
        frame.gameOver = true;
   }     
}
.\TicTacToeFrame.java:139: error: for-each not applicable to expression type
     for (Component comp : mainPanel.getComponent (number)) {
                                              ^
 required: array or java.lang.Iterable
 found:    Component
1 error
public void removeJPanel () {
 Component[] components : mainPanel.getComponents();
 for (int i = 0; i < components.length; i++) {
    Component comp = components[i];
    if (comp.isVisible () == true) {
        card.removeLayoutComponent (comp);
        number++;
    } else {
      System.out.print("It didn't work!");
    }
 }
}