Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GridBagLayout过度加载previos行_Java_Swing_User Interface_Awt - Fatal编程技术网

Java GridBagLayout过度加载previos行

Java GridBagLayout过度加载previos行,java,swing,user-interface,awt,Java,Swing,User Interface,Awt,我有一段代码,可以为每个问题添加一系列问题和4个答案。但是,当我添加第3个问题时,它会覆盖上一行。有人能帮我解决这个问题吗?我只需要一个关于它为什么这样做的提示 package Generators; /** * * @author samim */ import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; im

我有一段代码,可以为每个问题添加一系列问题和4个答案。但是,当我添加第3个问题时,它会覆盖上一行。有人能帮我解决这个问题吗?我只需要一个关于它为什么这样做的提示

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();
    }
    }
}
包生成器;
/**
*
*@作者萨米姆
*/
导入java.awt.Dimension;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTextField;
公共类CreateFormFields
{
//现场成员
静态JPanel面板=新JPanel();
静态整数索引器=1;
//静态列表问题=新建ArrayList();
静态列表问题=新建ArrayList();
静态列表ans1=新的ArrayList();
静态列表ans2=新的ArrayList();
静态列表ans3=新的ArrayList();
静态列表ans4=新的ArrayList();
静态列表结果=新建ArrayList();
公共静态void main(字符串[]args)
{
//构造框架
JFrame=新JFrame();
frame.setLayout(新的GridBagLayout());
frame.setPreferredSize(新尺寸(990990));
frame.setTitle(“表单创建者”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//框架约束
GridBagConstraints frameConstraints=新的GridBagConstraints();
//构造按钮
JButton addButton=新JButton(“添加”);
addButton.addActionListener(newButtonListener());
//将按钮添加到框架
frameConstraints.gridx=0;
frameConstraints.gridy=0;
添加(addButton,frameConstraints);
//构造面板
面板。设置首选尺寸(新尺寸(950400));
panel.setLayout(新的GridBagLayout());
JScrollPane scrollPane=新的JScrollPane(面板,JScrollPane.VERTICAL\u滚动条\u始终,
JScrollPane.HORIZONTAL\u SCROLLBAR\u NEVER);
scrollPane.setPreferredSize(新维度(900400));
frameConstraints.gridx=0;
frameConstraints.gridy=1;
添加(滚动窗格,框架约束);
//包装架
frame.pack();
//使框架可见
frame.setVisible(true);
}
静态类ButtonListener实现ActionListener
{
@凌驾
已执行的公共无效操作(操作事件arg0)
{
//透明面板
panel.removeAll();
panel.setPreferredSize(新尺寸(950,panel.getHeight()+200));
//创建标签和文本字段
JTextField JTextField=新的JTextField();
JTextField jTextField2=新的JTextField();
JTextField jTextField3=新的JTextField();
JTextField jTextField4=新的JTextField();
JTextField jTextField5=新的JTextField();
JTextField jTextField6=新的JTextField();
添加(jTextField);
ans1.add(jTextField2);
ans2.add(jTextField3);
ans3.add(jTextField4);
ans4.add(jTextField5);
结果.添加(jTextField6);
//创建约束
GridBagConstraints textFieldConstraints=新的GridBagConstraints();
GridBagConstraints labelConstraints=新的GridBagConstraints();
GridBagConstraints gridans1=新的GridBagConstraints();
GridBagConstraints temp=新的GridBagConstraints();
//添加标签和文本字段

对于(int i=1;iFour
gridbagsconstraints
),许多组件具有循环:性能差,代码读取样式太复杂。最好更改代码的设计:

  • 在处理类似类型组件的组时,请尝试将它们封装在另一个容器组件中:

    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
     }
    
    } 
    
  • 现在,假设您有根面板,它将维护问题面板(问题标签和答案输入字段的容器),具有必要的布局(
    BoxLayout
    或与您的设计相关的其他布局),创建一个新的
    MyQuestionPanel
    实例,并将其添加到
    root
    面板中。您不需要删除所有问题面板,然后按按钮单击将其重新添加到
    root
    面板中,就像您所做的那样,只需将
    root.add(MyQuestionPanel)

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


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

    使用这种方法,当我添加一个新问题时,它不会删除前一个问题,如果不会,那么我可以继续,我的意思是当我执行root.add(MyQuestionPanel)时当我点击该按钮时,它会将其附加到框架中还是仅仅添加并删除previosones@user2327579,我想顺便过来一下,你的问题现在解决了吗?