java - 自动将 JTextFilds 放在 GUI 上,repaint()
问题描述
当我在另一个文本字段中获得多个文本字段时,如何自动将其他文本字段放在面板上?!我尝试使用 repaint(),但我不明白。
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Example extends JFrame {
private Container container;
private JPanel panel;
private JTextField number; //get a number of TextFields
private JTextField text; // this to put in gui automatically
public Example() {
super("Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 400);
setLocationRelativeTo(null);
openView();
}
public void openView() {
container = this.getContentPane();
panel = new JPanel();
panel.setSize(new Dimension(200, 400));
panel.setLayout(null);
number = new JTextField();
number.setBounds( 50, 10, 100, 40);
number.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
// put others TextFields on gui
int y= 50;
int count = Integer.parseInt(number.getText());
for(int i=0; i<count; i++) {
text = new JTextField();
text.setBounds(50, y, 100, 40);
panel.add(text);
y=y+50;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
});
panel.add(number);
container.add(panel);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Example example = new Example();
example.setVisible(true);
}
}
如您所见,我正在尝试将其他 TetxFields 放在面板上,但是当我在 Textfield 上放置一些数字时,我有一个例外,例如
java.lang.NumberFormatException: For input string: ""
.
但我实际上已经将 String 更改为 int。
解决方案
推荐阅读
- javascript - 对类别和子类别的数组进行排序
- python - 在python pandas中连接两个数据框中的选定列
- java - 为什么 .length 给我返回语句的长度而不是用户输入?
- php - 在 Codeigniter 中生成 URL 安全的加密 id
- excel - 防止Word模板被保存过来;Word doc 是从 Excel 生成的
- haskell - 通过 Nat-kind 重叠实例
- angular - 在 Angular 中使用 ngIf 但我得到 Bindings cannot contain assignments
- rust - Piston 应用程序在几分钟后因内存分配错误而崩溃
- python - Selenium:通过存在_of_element_located() 聚合Selenium
- python - 尝试读取 xpt 文件时,如何修复 Python 中的“str”对象没有属性“tell”错误?