0

我有这段代码可以为每个问题添加一系列问题和 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&nbsp;" + 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();
    }
    }
}
4

1 回答 1

0

GridBagConstrains、带有循环的组件多:性能差,代码阅读风格过于复杂。最好更改代码的设计:

  1. 在使用类似类型的组件组时,尝试将它们包含在另一个容器组件中:

    MyQuestionPanel extends JPanel
    {
        public JLabel questionLab;
        public JTextField qAnsField1;
        public JTextFeild qAnsField2;
    
     MyQuestionPanel(int questionNo)
     {
       setLayout(/* GridBagLayout any layout relevant to your design */);
       //add your compoenents suing add(component, gridBagConstraint),  
       //or with relevant add() method e.g., questionLab, qAnsField1, qAnsField2
     }
    
    } 
    
  2. 现在,假设您有要维护问题面板的根面板(它是问题标签和答案输入字段的容器),具有必要的布局(BoxLayout或与您的设计相关的其他布局),创建一个新实例MyQuestionPanel并将它们添加到root面板。您不需要像您所做的那样删除所有问题面板并将它们重新添加到root按钮单击面板中,而只需将root.add(MyQuestionPanel)

  3. 你不需要打电话updateUI();实际上你不应该在这种情况下,因为它会首先卸载调用组件的所有属性,然后使用额外的调用重新安装 UIrevalidate()repaint(). 只需调用root.revalidate()然后root.repaint()反映添加和删除组件的更改。

如果你这样做,你会发现你的代码会更短、更简单、更漂亮。试试看。

于 2013-11-10T10:16:17.257 回答