Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/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 是否可以在每次添加容器时增加面板长度?_Java_Swing_Panel - Fatal编程技术网

Java 是否可以在每次添加容器时增加面板长度?

Java 是否可以在每次添加容器时增加面板长度?,java,swing,panel,Java,Swing,Panel,我目前正在设计一个GUI任务规划器。在此过程中,我遇到了一些问题。在我的课程中,有一部分我有一个小组来处理仍然需要完成的作业。我想要创建的是一个面板,它将向面板添加容器(保存组件以显示分配)。但是,我能想到的唯一方法是,每次添加容器时都延长面板。不幸的是,据我们所知,这是不可能的,因为面板在创建时会指定一个定义的宽度和深度。理想情况下,如果可能的话,我希望在每次添加容器时增加面板长度。滚动条将在面板上向下滚动 那么这可能吗?我这样做对吗?我对GUI非常陌生,所以我愿意接受改进和建议。如果有人想让

我目前正在设计一个GUI任务规划器。在此过程中,我遇到了一些问题。在我的课程中,有一部分我有一个小组来处理仍然需要完成的作业。我想要创建的是一个面板,它将向面板添加容器(保存组件以显示分配)。但是,我能想到的唯一方法是,每次添加容器时都延长面板。不幸的是,据我们所知,这是不可能的,因为面板在创建时会指定一个定义的宽度和深度。理想情况下,如果可能的话,我希望在每次添加容器时增加面板长度。滚动条将在面板上向下滚动

那么这可能吗?我这样做对吗?我对GUI非常陌生,所以我愿意接受改进和建议。如果有人想让我发布我的代码,我会的。(当心,它现在的形状很粗糙)

以下是我试图制作的草稿:

短暂性脑缺血发作

到目前为止,这是我的大部分代码。问题是,一旦我添加了一定数量的容器,它们就会变得更小。我只想在每次添加容器时扩展它。大多数评论都是为了记录或提醒做某事

import javax.swing.*;
import java.awt.*;
//import java.util.ArrayList;

public class MyWindow
{
    private final JFrame frame = new JFrame();
    private final int WINDOW_WIDTH = 500, WINDOW_DEPTH = 500;
    private JPanel panel;
    private JPanel toDoList, completed;
    //ArrayList<JFrame> frame = new ArrayList<>();

    public MyWindow()
    {
        frame.setTitle("Assignment Planner");
        this.contents();
    }

    private void contents()
    {//use an arraylist to create containers    ArrayList<JPanel> container = new ArrayList<>();
        frame.setSize(WINDOW_WIDTH, WINDOW_DEPTH);
        panel = new JPanel(new GridLayout(2, 1));

        toDoList = new JPanel();
        toDoList.setLayout(new /*GridLayout(0,1,5,5)*/BoxLayout(toDoList, BoxLayout.PAGE_AXIS));
        toDoList.setPreferredSize(new Dimension(250, 250));
        panel.add(toDoList);

        completed = new JPanel();
        //panelCompleted.setLayout(new GridLayout(0, 1)); //fix like one above

        panel.add(completed);

        JScrollPane scroll = new JScrollPane(toDoList); 
        panel.add(scroll);                                      //scroll panes for both panels
        JScrollPane scroll2 = new JScrollPane(completed);
        panel.add(scroll2);

        toDoList.add(assignment());
        toDoList.add(Box.createRigidArea(new Dimension(0,1)));
        toDoList.add(assignment());
        toDoList.add(Box.createRigidArea(new Dimension(0,1)));
        toDoList.add(assignment());
        toDoList.add(Box.createRigidArea(new Dimension(0,1)));
        beginningScrollPaneValue += 110;
        toDoList.setPreferredSize(new Dimension(250, beginningScrollPaneValue));
        toDoList.revalidate(); //scroll.revalidate();
        toDoList.repaint(); //scroll.repaint();

        frame.getContentPane().add(panel, BorderLayout.CENTER);//add the panel in the JFrame's content pane in the center
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    JPanel assignment()
    {
        JPanel container = new JPanel(new GridBagLayout());
        container.setMaximumSize(new Dimension(500,100));
        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 0.5;

        JCheckBox cb = new JCheckBox();
        c.fill = GridBagConstraints.NONE;
        c.gridx = 1;
        c.gridy = 0;
        container.add(cb, c);
        container.setBackground(Color.red);//does no fill area behind checkbox

        return container;
    }
}
import javax.swing.*;
导入java.awt.*;
//导入java.util.ArrayList;
公共类MyWindow
{
私有最终JFrame=新JFrame();
私有最终整数窗口宽度=500,窗口深度=500;
私人JPanel小组;
私人JPanel toDoList,已完成;
//ArrayList frame=新的ArrayList();
公共MyWindow()
{
frame.setTitle(“任务计划员”);
这个.contents();
}
私有无效内容()
{//使用arraylist创建容器arraylist container=new arraylist();
帧设置大小(窗口宽度、窗口深度);
面板=新JPanel(新网格布局(2,1));
toDoList=newjpanel();
toDoList.setLayout(新的/*GridLayout(0,1,5,5)*/BoxLayout(toDoList,BoxLayout.PAGE_轴));
setPreferredSize(新维度(250250));
面板。添加(toDoList);
已完成=新JPanel();
//panelCompleted.setLayout(新的GridLayout(0,1));//像上面一样修复
面板。添加(已完成);
JScrollPane scroll=新的JScrollPane(toDoList);
panel.add(滚动);//滚动两个面板的窗格
JScrollPane scroll2=新的JScrollPane(已完成);
面板。添加(滚动2);
添加(赋值());
添加(Box.createRigidArea(新维度(0,1));
添加(赋值());
添加(Box.createRigidArea(新维度(0,1));
添加(赋值());
添加(Box.createRigidArea(新维度(0,1));
起始ScrollPaneValue+=110;
setPreferredSize(新维度(250,起始ScrollPanEvalue));
toDoList.revalidate();//scroll.revalidate();
toDoList.repaint();//滚动.repaint();
frame.getContentPane().add(panel,BorderLayout.CENTER);//将面板添加到JFrame的内容窗格的中央
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
JPanel赋值()
{
JPanel container=newJPanel(newGridBagLayout());
容器。设置最大尺寸(新尺寸(500100));
GridBagConstraints c=新的GridBagConstraints();
c、 权重x=0.5;
JCheckBox cb=新的JCheckBox();
c、 fill=GridBagConstraints.NONE;
c、 gridx=1;
c、 gridy=0;
容器。添加(cb,c);
container.setBackground(Color.red);//不填充复选框后面的区域
返回容器;
}
}

“不幸的是,据我们所知,这是不可能的,因为面板在创建时会指定一个定义的宽度和深度”-应该是,应该给它一个布局管理器,它可以根据子组件计算大小要求-这将允许您使用
JScrollPane
包装容器,并在容器超出scollpane的视图边界时自动显示滚动条。您应该查看一下,我可以查看自定义的例如,
可滚动组件中的渲染器。我同意@trashgord re。使用可滚动组件。链接示例演示了列表的使用,这是我在查看图形时想到的第一个组件。如果使用
JTable
(带行过滤器/排序器),“将已完成的任务移到末尾”的要求会更简单。在认可@AndrewThompson的
JTable
建议时,请注意它支持编辑器和渲染器。