java - Creating JPanels with a titled border in a for loop from user input
问题描述
I want to create a for loop that creates JPanel
containers with titled headers. the number of iterations depends on the user input from previous interfaces.
int noofpara=Integer.parseInt(data[6]);
for(int i=1;i<=noofpara;i++){
jPanel1.add(new JPanel().setBorder(new TitledBorder("Perimeter"+i)));
}
The noofpara
is the number of perimeters the user chose according to that the for loop should create panels with the titled border with the number of perimeters. the error appears at the jpanel1.add...
where it says void type not allowed.
解决方案
JPanel#setBorder
方法具有void
返回类型,这意味着在调用该方法时它不返回任何值。
但是JPanel#add
方法需要一个值才能调用,它会给出编译错误,因为 setBorder 是无效的。
你可以简单地通过这个来解决这个问题。
JPanel childPanel = new JPanel();
childPanel.setBorder(new TitledBorder("Perimeter" + i));
jPanel1.add(childPanel);
推荐阅读
- c# - C++ 互操作的限制(隐式 PInvoke)
- ios - Youtube_player_flutter 不在 iOS 设备上播放视频
- laravel - 重复功能更新
- java - 如何从 Java/Android 访问 Unity 设置?
- jquery - Select2 - 监听字符输入
- javascript - VueJS - 将具有 v-model 属性的动态追加到 div?
- c# - C# OpenFiledialog.Showdialog 给出错误 CS1061
- typescript - 避免带有子字段类型保护的样板
- label - 标签和文本输入对齐的斗争
- visual-studio-code - 如何修复“在 PATH 上找不到运行时 'nodemon'。确保安装了 'nodemon'。”?