当尝试使用 class SpringUtilities 制作 CompactGrid 时,'no such child' 在 java swing 中意味着什么
What 'no such child' means in java swing when trying to makeCompactGrid with class SpringUtilities
所以我尝试制作一个简单的 java swing 程序,我没有完成程序,但我想在执行功能部分之前先看看它的外观,这是我的代码:
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;}}}
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)
为什么会出现这种情况,我做错了什么?
我还使用从 Oracle 复制的 SpringUtilities class。
非常感谢任何帮助!
我从 here 下载了 class SpringUtilities
的源代码。
您的问题是您没有将所有组件添加到 panel
。当您应该调用 panel.add()
时,您调用了 add()
。因此,您只将 4 个组件添加到 panel
而不是 8 个。因此出现错误消息。
我还添加了对 class JFrame
方法 pack()
的调用,以便使 JFrame
足够大以显示它包含的所有组件。
这是你的代码和我的修正:
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("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)); // change here
panel.add(napredakProgressBar); // change here
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(); // I added this line
frame.setLocationRelativeTo(null); // I added this line
frame.setVisible(true);
}
public class primeNumberJavaSwingApp extends SwingWorker<Long, Integer> {
@Override
protected Long doInBackground() throws Exception {
Long l = (long) 3.2;
return l;
}
}
}
这是 JFrame
上面代码 运行 时的屏幕截图。
所以我尝试制作一个简单的 java swing 程序,我没有完成程序,但我想在执行功能部分之前先看看它的外观,这是我的代码:
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;}}}
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)
为什么会出现这种情况,我做错了什么?
我还使用从 Oracle 复制的 SpringUtilities class。 非常感谢任何帮助!
我从 here 下载了 class SpringUtilities
的源代码。
您的问题是您没有将所有组件添加到 panel
。当您应该调用 panel.add()
时,您调用了 add()
。因此,您只将 4 个组件添加到 panel
而不是 8 个。因此出现错误消息。
我还添加了对 class JFrame
方法 pack()
的调用,以便使 JFrame
足够大以显示它包含的所有组件。
这是你的代码和我的修正:
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("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)); // change here
panel.add(napredakProgressBar); // change here
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(); // I added this line
frame.setLocationRelativeTo(null); // I added this line
frame.setVisible(true);
}
public class primeNumberJavaSwingApp extends SwingWorker<Long, Integer> {
@Override
protected Long doInBackground() throws Exception {
Long l = (long) 3.2;
return l;
}
}
}
这是 JFrame
上面代码 运行 时的屏幕截图。