我有这段代码可以为每个问题添加一系列问题和 4 个答案。但是,当我添加第三个问题时,它会覆盖前一行。anypne 可以帮助我解决这个问题。我只需要提示它为什么这样做这个
package Generators;
/**
*
* @author samim
*/
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.JScrollPane;
import javax.swing.JTextField;
public class CreateFormFields
{
// Field members
static JPanel panel = new JPanel();
static Integer indexer = 1;
// static List<JLabel> question = new ArrayList<JLabel>();
static List<JTextField> question = new ArrayList<JTextField>();
static List<JTextField> ans1 = new ArrayList<JTextField>();
static List<JTextField> ans2 = new ArrayList<JTextField>();
static List<JTextField> ans3 = new ArrayList<JTextField>();
static List<JTextField> ans4 = new ArrayList<JTextField>();
static List<JTextField> result = 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("Form Creator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Frame constraints
GridBagConstraints frameConstraints = new GridBagConstraints();
// Construct button
JButton addButton = new JButton("Add");
addButton.addActionListener(new ButtonListener());
// Add button to frame
frameConstraints.gridx = 0;
frameConstraints.gridy = 0;
frame.add(addButton, frameConstraints);
// Construct panel
panel.setPreferredSize(new Dimension(950, 400));
panel.setLayout(new GridBagLayout());
JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setPreferredSize(new Dimension(900, 400));
frameConstraints.gridx = 0;
frameConstraints.gridy = 1;
frame.add(scrollPane, frameConstraints);
// Pack frame
frame.pack();
// Make frame visible
frame.setVisible(true);
}
static class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
// Clear panel
panel.removeAll();
panel.setPreferredSize(new Dimension(950,panel.getHeight()+200));
// Create label and text field
JTextField jTextField = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JTextField jTextField5 = new JTextField();
JTextField jTextField6 = new JTextField();
question.add(jTextField);
ans1.add(jTextField2);
ans2.add(jTextField3);
ans3.add(jTextField4);
ans4.add(jTextField5);
result.add(jTextField6);
// Create constraints
GridBagConstraints textFieldConstraints = new GridBagConstraints();
GridBagConstraints labelConstraints = new GridBagConstraints();
GridBagConstraints gridans1 = new GridBagConstraints();
GridBagConstraints temp = new GridBagConstraints();
// Add labels and text fields
for(int i = 1; i <= indexer; i++)
{
int R = (int) (Math.random() * 255);
int G = (int) (Math.random() * 255);
int B = (int) (Math.random() * 255);
String total = "rgb(" + R + "," + G + "," + B + ")";
// Label constraints
labelConstraints.gridx = 0;
labelConstraints.gridy = i * 7;
labelConstraints.insets = new Insets(5, 300, 5, 300);
panel.add(new JLabel("<html><font color='" + total + "'>Question " + i + "<font></html>"),
labelConstraints);
temp.gridx = 0;
temp.gridy = i * 8;
temp.insets = new Insets(10, -550, 10, 2);
panel.add(new JLabel("Question :"), temp);
// Text field constraints
textFieldConstraints.fill = 1;//GridBagConstraints.HORIZONTAL;
textFieldConstraints.insets = new Insets(10, -550, 10, 0);
textFieldConstraints.gridy = i * 8;
panel.add(question.get(i - 1), textFieldConstraints);
gridans1.fill = 1;//GridBagConstraints.HORIZONTAL;
gridans1.insets = new Insets(10, -550, 10, 290);
gridans1.gridy = i * 9;
temp.gridy = i * 9;
panel.add(new JLabel("Answer 1 :"), temp);
panel.add(ans1.get(i - 1), gridans1);
gridans1.gridy = i * 10;
temp.gridy = i * 10;
panel.add(new JLabel("Answer 2 :"), temp);
panel.add(ans2.get(i - 1), gridans1);
gridans1.gridy = i * 11;
temp.gridy = i * 11;
panel.add(new JLabel("Answer 3:"), temp);
panel.add(ans3.get(i - 1), gridans1);
gridans1.gridy = i * 12;
temp.gridy = i * 12;
panel.add(new JLabel("Answer 4:"), temp);
panel.add(ans4.get(i - 1), gridans1);
gridans1.gridy = i * 13;
temp.gridy = i * 13;
panel.add(new JLabel("Good Result :"), temp);
panel.add(result.get(i - 1), gridans1);
}
// Align components top-to-bottom
// Increment indexer
indexer++;
panel.updateUI();
}
}
}