What 'no such child' means in java swing when trying to makeCompactGrid with class SpringUtilities

huangapple go评论83阅读模式
英文:

What 'no such child' means in java swing when trying to makeCompactGrid with class SpringUtilities

问题

我试图制作一个简单的Java Swing程序我没有完成这个程序但在做功能部分之前我想看看它的外观这是我的代码

```java
import layouts.SpringUtilities;

import javax.swing.*;
import layouts.SpringUtilities;

public class FactorialCalculatorFrame extends JFrame {

    public FactorialCalculatorFrame(){
        JPanel panel = new JPanel();
        panel.setLayout(new SpringLayout());

        JTextField brojText = new JTextField();
        brojText.setColumns(10);
        panel.add(new JLabel("Broj:", SwingConstants.RIGHT));
        panel.add(brojText);

        JButton start = new JButton("Start");

        panel.add(new JLabel("Pokreni izracun:", SwingConstants.RIGHT));
        panel.add(start);

        JProgressBar napredakProgressBar = new JProgressBar();

        add(new JLabel("Napredak:", SwingConstants.RIGHT));
        add(napredakProgressBar);

        JLabel rezultat = new JLabel("Rezultat:");
        JLabel ispisiRez = new JLabel("");

        add(new JLabel("Rezultat:", SwingConstants.RIGHT));
        add(ispisiRez);

        start.addActionListener((e) -> {
            try {
                int number = Integer.parseInt(brojText.getText());

                //reset GUI components
                napredakProgressBar.setValue(0);
                start.setEnabled(false);
                ispisiRez.setText("");

                //schedule for execution on one of working threads
                new primeNumberJavaSwingApp().execute();

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });

        SpringUtilities.makeCompactGrid(panel, 4, 2, 0, 0, 5, 5);

        add(panel);
    }

    public static void main(String[] args){
        FactorialCalculatorFrame frame = new FactorialCalculatorFrame();
        frame.setVisible(true);
    }

    public class primeNumberJavaSwingApp extends SwingWorker<Long, Integer> {
        @Override
        protected Long doInBackground() throws Exception {
            Long l = (long)3.2;
            return l;
        }
    }
}
发生的异常是在线程 "main" 中的异常 java.lang.ArrayIndexOutOfBoundsException: 没有这样的子项4
位于 layouts.SpringUtilities.makeCompactGrid(SpringUtilities.java:190)
位于 FactorialCalculatorFrame.<init>(FactorialCalculatorFrame.java:55)

为什么会发生这种情况?我做错了什么?

我还使用了从Oracle复制的SpringUtilities类。非常感谢任何帮助!


<details>
<summary>英文:</summary>
So I tried to make a simple java swing program, i didn&#39;t finish the program but I wanted to see how it looks before doing the functionality part, this is my code: 

import layouts.SpringUtilities;

import javax.swing.*;
import layouts.SpringUtilities;

public class FactorialCalculatorFrame extends JFrame {

public FactorialCalculatorFrame(){
JPanel panel = new JPanel();
panel.setLayout(new SpringLayout());
JTextField brojText = new JTextField();
brojText.setColumns(10);
panel.add(new JLabel(&quot;Broj:&quot;, SwingConstants.RIGHT));
panel.add(brojText);
JButton start = new JButton(&quot;Start&quot;);
panel.add(new JLabel(&quot;Pokreni izracun:&quot;,SwingConstants.RIGHT));
panel.add(start);
JProgressBar napredakProgressBar = new JProgressBar();
add(new JLabel(&quot;Napredak:&quot;, SwingConstants.RIGHT));
add(napredakProgressBar);
JLabel rezultat = new JLabel(&quot;Rezultat:&quot;);
JLabel ispisiRez = new JLabel(&quot;&quot;);
add(new JLabel(&quot;Rezultat:&quot;, SwingConstants.RIGHT));
add(ispisiRez);
start.addActionListener((e)-&gt;{
try {
int number = Integer.parseInt(brojText.getText());
//reset GUI components
napredakProgressBar.setValue(0);
start.setEnabled(false);
ispisiRez.setText(&quot;&quot;);
//schedule for execution on one of working threads
new primeNumberJavaSwingApp().execute();
} catch (Exception ex) {
ex.printStackTrace();
}
});
SpringUtilities.makeCompactGrid(panel,4, 2, 0, 0, 5, 5);
add(panel);
}
public static void main(String[] args){
FactorialCalculatorFrame frame = new FactorialCalculatorFrame();
frame.setVisible(true);
}
public class primeNumberJavaSwingApp extends SwingWorker&lt;Long, Integer&gt; {
@Override
protected Long doInBackground() throws Exception {
Long l = (long)3.2;

return l;}}}


the Exception that occurs is this Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: No such child: 4
at layouts.SpringUtilities.makeCompactGrid(SpringUtilities.java:190)
at FactorialCalculatorFrame.<init>(FactorialCalculatorFrame.java:55)


Why does this occurs, what did I do wrong? 
I also use SpringUtilities class that I copied from Oracle.
Any help is greatly appriciated!
</details>
# 答案1
**得分**: 0
我从[这里][1]下载了`SpringUtilities`类的源代码。
你的问题在于你没有将所有的组件添加到`panel`中。你在应该调用`panel.add()`时调用了`add()`。因此,你只向`panel`中添加了4个组件,而不是8个。这也是错误的原因。
我还在`JFrame`类的`pack()`方法中添加了一个调用,以便使`JFrame`的大小足够大,以显示其中包含的所有组件。
以下是已经修复的代码:
```java
import layout.SpringUtilities;
import javax.swing.*;
public class FactorialCalculatorFrame extends JFrame {
public FactorialCalculatorFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new SpringLayout());
JTextField brojText = new JTextField();
brojText.setColumns(10);
panel.add(new JLabel("Broj:", SwingConstants.RIGHT));
panel.add(brojText);
JButton start = new JButton("Start");
panel.add(new JLabel("Pokreni izracun:",SwingConstants.RIGHT));
panel.add(start);
JProgressBar napredakProgressBar = new JProgressBar();
panel.add(new JLabel("Napredak:", SwingConstants.RIGHT));
panel.add(napredakProgressBar);
JLabel rezultat = new JLabel("Rezultat:");
JLabel ispisiRez = new JLabel("");
panel.add(new JLabel("Rezultat:", SwingConstants.RIGHT));
panel.add(ispisiRez);
start.addActionListener((e) -> {
try {
int number = Integer.parseInt(brojText.getText());
//reset GUI components
napredakProgressBar.setValue(0);
start.setEnabled(false);
ispisiRez.setText("");
//schedule for execution on one of working threads
new primeNumberJavaSwingApp().execute();
} catch (Exception ex) {
ex.printStackTrace();
}
});
SpringUtilities.makeCompactGrid(panel, 4, 2, 0, 0, 5, 5);
add(panel);
}
public static void main(String[] args){
FactorialCalculatorFrame frame = new FactorialCalculatorFrame();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class primeNumberJavaSwingApp extends SwingWorker<Long, Integer> {
@Override
protected Long doInBackground() throws Exception {
Long l = (long) 3.2;
return l;
}
}
}

以下是运行上述代码时JFrame的屏幕截图。
What 'no such child' means in java swing when trying to makeCompactGrid with class SpringUtilities

英文:

I downloaded source code for class SpringUtilities from here.

Your problem is that you are not adding all the components to the panel. You are calling add() when you should be calling panel.add(). Hence you are only adding 4 components to panel rather than 8. Hence the error message.

I also added a call to method pack(), of class JFrame, in order to make the JFrame big enough to display all the components it contains.

Here is your code with my fixes:

import layout.SpringUtilities; // in your code this import is added twice

import javax.swing.*;

public class FactorialCalculatorFrame extends JFrame {
    public FactorialCalculatorFrame() {
        setDefaultCloseOperation(EXIT_ON_CLOSE); // I added this line
        JPanel panel = new JPanel();
        panel.setLayout(new SpringLayout());

        JTextField brojText = new JTextField();
        brojText.setColumns(10);
        panel.add(new JLabel(&quot;Broj:&quot;, SwingConstants.RIGHT));
        panel.add(brojText);

        JButton start = new JButton(&quot;Start&quot;);

        panel.add(new JLabel(&quot;Pokreni izracun:&quot;,SwingConstants.RIGHT));
        panel.add(start);

        JProgressBar napredakProgressBar = new JProgressBar();

        panel.add(new JLabel(&quot;Napredak:&quot;, SwingConstants.RIGHT)); // change here
        panel.add(napredakProgressBar); // change here

        JLabel rezultat = new JLabel(&quot;Rezultat:&quot;);
        JLabel ispisiRez = new JLabel(&quot;&quot;);

        panel.add(new JLabel(&quot;Rezultat:&quot;, SwingConstants.RIGHT));
        panel.add(ispisiRez);

        start.addActionListener((e)-&gt;{
            try {
                int number = Integer.parseInt(brojText.getText());

                //reset GUI components
                napredakProgressBar.setValue(0);
                start.setEnabled(false);
                ispisiRez.setText(&quot;&quot;);

                //schedule for execution on one of working threads
                new primeNumberJavaSwingApp().execute();

                            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        SpringUtilities.makeCompactGrid(panel, 4, 2, 0, 0, 5, 5);
        add(panel);
    }

    public static void main(String[] args){
        FactorialCalculatorFrame frame = new FactorialCalculatorFrame();
        frame.pack(); // I added this line
        frame.setLocationRelativeTo(null); // I added this line
        frame.setVisible(true);
    }

    public class primeNumberJavaSwingApp extends SwingWorker&lt;Long, Integer&gt; {
        @Override
        protected Long doInBackground() throws Exception {
            Long l = (long) 3.2;
            return l;
        }
    }
}

Here is a screen capture of the JFrame when running the above code.

What 'no such child' means in java swing when trying to makeCompactGrid with class SpringUtilities

huangapple
  • 本文由 发表于 2020年7月25日 11:43:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63084086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定