java 在循环中动态创建 JTextField
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16591305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create JTextField dynamically in a loop
提问by saidozcan
I am beginner on Java GUI. I am writing a program that calculates the inverse of the given matrix. To do this, first i have to scan matrix. I decided to scan matrix by this way:
我是 Java GUI 的初学者。我正在编写一个程序来计算给定矩阵的逆。为此,首先我必须扫描矩阵。我决定通过这种方式扫描矩阵:
First i am asking user the count of row or column of square matrix. When user enters a number and clicks Tamam
button i want to create little NxN JTextField
's.
首先,我问用户方阵的行数或列数。当用户输入一个数字并点击Tamam
按钮时,我想创建小 NxNJTextField
的。
So user will be able to enter each element of matrix easily. What i want to ask is this: I am unable to create NxN JTextField
's. My code:
因此用户将能够轻松输入矩阵的每个元素。我想问的是:我无法创建 NxN JTextField
。我的代码:
private void jButtonRowCntMouseClicked(java.awt.event.MouseEvent evt) {
int i,j;
if(jTextFieldRowCnt.getText() != null){
String cnt = jTextFieldRowCnt.getText();
Integer rowCnt = Integer.parseInt(cnt);
for(i=0;i<rowCnt;i++){
for(j=0;j<rowCnt;j++){
JTextField textField = new JTextField();
this.add(textField);
pack();
}
}
}
}
But unfortunately, I could not make it. Where am I going wrong?
但不幸的是,我无法做到。我哪里错了?
My another question is that, how can I set locations of NxN JTextField
s?
我的另一个问题是,如何设置 NxNJTextField
的位置?
回答by Sanjaya Liyanage
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
public class Test
{
// Field members
static JPanel panel = new JPanel();
static Integer indexer = 1;
static List<JTextField> listOfTextFields = new ArrayList<JTextField>();
public static void main(String[] args)
{
// Construct frame
JFrame frame = new JFrame();
frame.setLayout(new GridBagLayout());
frame.setPreferredSize(new Dimension(990, 990));
frame.setTitle("My Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Frame constraints
GridBagConstraints frameConstraints = new GridBagConstraints();
// Construct button
JButton addButton = new JButton("test");
addButton.addActionListener(new ButtonListener());
// Add button to frame
frameConstraints.gridx = 0;
frameConstraints.gridy = 0;
frame.add(addButton, frameConstraints);
// Construct panel
panel.setPreferredSize(new Dimension(600, 600));
panel.setLayout(new GridBagLayout());
panel.setBorder(LineBorder.createBlackLineBorder());
// Add panel to frame
frameConstraints.gridx = 0;
frameConstraints.gridy = 1;
frameConstraints.weighty = 1;
frame.add(panel, frameConstraints);
// Pack frame
frame.pack();
// Make frame visible
frame.setVisible(true);
}
static class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
panel.removeAll();
GridBagConstraints textFieldConstraints = new GridBagConstraints();
int rowCnt=4,i,j;
for(i=0;i<rowCnt;i++){
for(j=0;j<rowCnt;j++){
JTextField g=new JTextField();
g.setText("7");
textFieldConstraints.gridx = i;
textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
textFieldConstraints.weightx = 0.5;
textFieldConstraints.insets = new Insets(10, 10, 10, 10);
textFieldConstraints.gridy = j;
panel.add(g, textFieldConstraints);
}
}
panel.updateUI();
}
}
}
try this and get what you want
试试这个,得到你想要的
回答by polypiel
What is the concrete problem? What is class of the object? It seems a problem with the layout of the container component.
具体问题是什么?什么是对象的类?好像是容器组件的布局有问题。
I'd recommend you to add the matrix text fields into a dedicated JPanel
with a GridLayout
.
我建议您将矩阵文本字段添加到JPanel
带有GridLayout
.
Example:
例子:
// ...
matrixPanel.setLayout(new GridLayout(rowCnt, rowCnt)); // matrixPanel is the dedicated JPanel
for(i=0;i<rowCnt;i++){
for(j=0;j<rowCnt;j++){
JTextField textField = new JTextField();
matrixPanel.add(textField); // add the fields into the panel
//pack(); I think it wouldn't be needed
}
}
More information about GridPanel in http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html
有关 GridPanel 的更多信息,请访问http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html