如何将帧添加到另一帧?

时间:2014-12-03 09:38:27

标签: java swing jframe

这里我将使用DimensionToolkit类在系统托盘中显示类似框架的弹出窗口。 我有5个弹出窗口。平均5帧显示一对一。之后我想在单帧中显示所有帧,在那里我可以滚动这个帧。

那么你能告诉我如何实现这个目标吗?

int n=0;
while (itr.hasNext()) {
Object element = itr.next();

bean = (JavaBean) element;
System.out.print("---->" + bean.getTime());
System.out.print("---->" + bean.getTitle());
System.out.println("----->" + bean.getUrl());


final URI uri = new URI(bean.getUrl());
final JFrame frame = new JFrame();
frame.setSize(350, 70);
frame.add(new JSeparator(SwingConstants.HORIZONTAL));
frame.setAlwaysOnTop(true);
frame.setUndecorated(true);
frame.setLayout(new GridBagLayout());

JButton cloesButton = new JButton("X");
JButton linkbutton = new JButton("links");
addComponent(frame, linkbutton, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
addComponent(frame, cloesButton, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
linkbutton.setText("<HTML><div style='width:200px; color:#000; border:1px solid #e5e5e5; margin-right:5px;'>" + bean.getTitle() + "</div>" + "</HTML>");
linkbutton.setBackground(Color.LIGHT_GRAY);
linkbutton.setHorizontalAlignment(SwingConstants.LEFT);

cloesButton.setFocusable(false);
linkbutton.setToolTipText(uri.toString());

frame.add(linkbutton);
frame.add(cloesButton);
frame.setVisible(true);

//Set Pop up at bottom - right
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();// size of the screen
Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(frame.getGraphicsConfiguration());// height of the task bar
//frame.setLocation(scrSize.width - frame.getWidth(), scrSize.height - toolHeight.bottom - frame.getHeight());
frame.setLocation(scrSize.width - frame.getWidth(), scrSize.height - toolHeight.bottom - (frame.getHeight() * (n + 1)));

n++;
}

enter image description here

1 个答案:

答案 0 :(得分:0)

将JPanel而不是Frame定义到循环中,并将所有JPanel添加到一个Frame中。

修改

这样的事情:

JFrame frame = ...
while(...) {
    JPanel panel = new JPanel();
    panel.add(buttons, etc)
    frame.add(panel);
}