我正在尝试构建一个基本控件,当单击按钮时,它将在其下方显示一个未装饰的 JFrame。我试图模仿下拉类型的功能,但使用我自己的框架而不是面板。我的组件包含我希望它显示的 JFrame 派生控件的类成员。在某些情况下,调用 setVisible 时,不会绘制此 JFrame 的内容。当我试图在我最左边的显示器上显示 JFrame 时,这似乎发生了,它使用负 x 坐标(我的主显示器是中间显示器)。奇怪的是这个问题只出现在我的 Windows 7 机器上,而不出现在 XP 机器上。
这是一个演示问题的非常基本的示例。如您所见,这是一个非常基本的示例,应该只是隐藏和显示 DropFrame。我已经省略了 initComponents 中的代码,在这种情况下,它所做的一切都是为每个框架添加一个按钮,并为每个按钮添加必要的 ActionListeners。
代码:
public class NewJFrame extends javax.swing.JFrame {
private javax.swing.JButton jButton2;
private DropFrame f = new DropFrame();
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}
private void initComponents() {
//Create button and add it to the frame...
pack();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
SwingUtilities.invokeLater(new Runnable()
{
public void run() {
Point p = jButton2.getLocationOnScreen();
f.setLocation(p.x, p.y + 25);
f.setVisible(true);
}
});
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
} }
public class DropFrame extends javax.swing.JFrame {
private javax.swing.JButton jButton1;
/** Creates new form NewJFrame1 */
public DropFrame() {
initComponents();
}
private void initComponents() {
//Create button and add to frame...
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false);
} }
如果我在每次单击按钮时创建一个新的 DropFrame,而不是重用和设置同一 Frame 的可见性,则问题不存在,但这是不希望的。关于为什么我的 DropFrame 有时不画的任何想法?