【发布时间】:2017-05-21 14:38:02
【问题描述】:
我正在制作一个作业计划程序。对于容器(分配),我想将某些组件放置到某些位置。然而。我似乎只能通过使用锚来移动组件。好像我的 gridx 和 gridy 什么都不做。谁能指出我的问题并可能提供一些建议。下面提供了我的代码和预期最终结果的图片。
代码:
import javax.swing.*;
import java.awt.*;
import static java.awt.GridBagConstraints.*;
//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;
private int scrollPaneValue = 110;
//ArrayList<JFrame> frame = new ArrayList<>();
public MyWindow()
{
frame.setTitle("Assignment Planner");
this.contents();
}
private void contents()//make a border above each panel stating "TO-DO" or "COMPLETED"
{//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, 110));
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(Box.createRigidArea(new Dimension(0,1)));
toDoList.add(assignment());
scrollPaneValue += 110; //add these 2 lines of code, beginning after the first two containers to increase jscrollpane
toDoList.setPreferredSize(new Dimension(250, scrollPaneValue));
//toDoList.revalidate(); may not even need
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 cDefault = new GridBagConstraints();
cDefault.weightx = 0.5;
cDefault.insets = new Insets(5,5,5,5);
JCheckBox cb;
JLabel dueDate, description;
JButton edit;
cb = new JCheckBox();
GridBagConstraints cCb = new GridBagConstraints();
cCb.weightx = 0.5;
cCb.weighty = 1;
cCb.fill = GridBagConstraints.NONE;//originally none
cCb.gridx = 0;
cCb.gridy = 0;
//cCb.gridwidth = 1;
//cCb.gridheight = 1;
cCb.anchor = FIRST_LINE_START;//may not need, plus needs static import
cCb.insets = cDefault.insets;
cb.setBackground(Color.RED);
container.add(cb, cCb);
dueDate = new JLabel("Due Date");
GridBagConstraints cDueDate = new GridBagConstraints();
cDueDate.gridx = 1;
cDueDate.gridy = 0;
cDueDate.gridwidth = 2;
//cDueDate.fill = GridBagConstraints.HORIZONTAL;
cDueDate.anchor = PAGE_START;
dueDate.setBackground(Color.BLUE);
//cDueDate.anchor = FIRST_LINE_START;
container.add(dueDate, cDueDate);
edit = new JButton("Edit");
GridBagConstraints e = new GridBagConstraints();
e.gridx = 4;
e.gridy = 0;
e.anchor = GridBagConstraints.FIRST_LINE_END;
e.insets = cDefault.insets;
edit.setBackground(Color.GREEN);
container.add(edit, e);
description = new JLabel("Description...");
GridBagConstraints d = new GridBagConstraints();
d.gridx = 1;
d.gridy = 3;
d.gridwidth = 3;
d.fill = GridBagConstraints.HORIZONTAL;
description.setBackground(Color.YELLOW);
container.add(description, d);
container.setBackground(Color.LIGHT_GRAY);//does no fill area behind checkbox
return container;
}
}
我想要容器的样子:
【问题讨论】:
-
从另一个问题.. “你能详细说明为什么你和垃圾神建议的方式会更好地解决我的问题” 看。向上。 ;)
-
@AndrewThompson 现在我明白你在说什么了。我只接受了 MadProgrammer 的建议,因为在添加容器和扩展“面板”时,它似乎是最容易实现的。当你提出其他建议时,我已经调查过了。我没想过去。我的心态每次都是一个问题。
-
@AndrewThompson 另外,这似乎根本不是 JScrollPane 的问题。这只是因为我对 GridBagLayout 缺乏了解。
-
关于您的其他评论 - 也许,但如果这是我的 GUI,我会做不同的事情。我将创建具有描述 String、dueDate Date、taskNumber、int 以及完成的 boolean、equals 和 hashCode 方法的非 GUI 任务类......以及一个包含类集合的 TaskList 类我将创建一个 TaskPanel 类它包含一个 Task 对象并显示来自 Task 对象的数据。如果选中该复选框,我会让 JPanel 通知侦听器,将完成的布尔值更改为 true,从其容器中删除该任务面板,然后创建一个新的...
-
TaskPanel 与包含的 Task 对象,并将其放入已完成的 JPanel 中。或者,如果不编辑已完成的任务,那么我会使用合适的渲染器将其显示在 JList 中……有很多更简洁的方法可以给这只猫换皮。
标签: java swing user-interface layout-manager gridbaglayout