Hey Stack Overflow人员,
我一直在尝试让代码工作,我可以双击JList
中的某个项目,并在所有对象名称的框架上的另一个地方创建一个新的JList
属于该类别的项目(我猜这是无用的信息)。
但问题是,当我双击列表中的项目时,它会运行代码将组件添加到JFrame
但它永远不会显示,这是因为我使用鼠标事件来构建它运行时间之后还是什么?
我的Gui课程是:
public class MediaGUI extends JFrame
并在此处添加代码
_mediaList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
if (event.getClickCount() == 2) {
CreateObjectPane(_mediaList.getSelectedValue().toString(););
}
}
});
这里是构建代码,这个方法属于MediaGUI,所以这个。是指JFrame
private void CreateObjectPane(String category)
{
/*=======================================================================
* Create ther list on Objects that belong to each category
* Also a scroll bar for the list
*=======================================================================*/
String objects[] = _mediaHandler.GetObjects(category);
_mediaList = new JList(objects);
_mediaList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
_mediaList.setLayoutOrientation(_mediaList.VERTICAL);
_mediaList.setVisibleRowCount(-1);
_mediaScrollPane = new JScrollPane(_mediaList);
_mediaScrollPane.setPreferredSize(new Dimension(100,100));
_mediaScrollPane.setAlignmentX(CENTER_ALIGNMENT);
_mediaPanel = new JPanel();
_mediaPanel.setLayout(new BoxLayout(_mediaPanel, BoxLayout.PAGE_AXIS));
_mediaLabel = new JLabel("Media Objects");
_mediaLabel.setLabelFor(_mediaList);
_mediaPanel.add(_mediaLabel);
_mediaPanel.add(Box.createRigidArea(new Dimension(0, 15)));
_mediaPanel.add(_mediaScrollPane);
_mediaPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
this.add(_mediaPanel, BorderLayout.CENTER);
}
如果需要更多解释,您可以提供的任何帮助都很棒,我很乐意提出要求
答案 0 :(得分:3)
Note: If a component has been added to a container that has been displayed,
validate must be called on that container to display the new component.
If multiple components are being added, you can improve efficiency by
calling validate only once, after all the components have been added.
其他一些提示:
答案 1 :(得分:0)
我需要在新创建的组件的容器上调用revalidate()方法。