gpt4 book ai didi

java - Netbeans 中相同 swing 代码的异常行为

转载 作者:行者123 更新时间:2023-12-02 00:18:33 24 4
gpt4 key购买 nike

我有两个几乎相似的代码。

代码一

   JFrame pFrame=new NetBeansFrame();
JPanel myPanel=new myPanel();
pFrame.add(myPanel);
Dimension windowDim=myPanel.getSize();

pFrame.pack();
pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);
pFrame.setVisible(true);

代码二

   JFrame pFrame=new JFrame();
JPanel myPanel=new myPanel();
pFrame.add(myPanel);
Dimension windowDim=myPanel.getSize();

pFrame.pack();
pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);
pFrame.setVisible(true);

在代码 I 中,NetBeansFrame 是我使用 netbeans->Jframe 创建的一个框架,并将其命名为 NetBeansFrame。它不包含任何内容。我使用 codeI 将面板添加到其中。

NetBeansFrame.java

public class NetBeansFrame extends javax.swing.JFrame {


public NetBeansFrame() {
initComponents();

}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 407, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 429, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NetBeansFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NetBeansFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NetBeansFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NetBeansFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new NetBeansFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration

}

在代码 II 中,我从 JFrame 创建一个框架。逻辑上,这两个代码是等效的。

但是在执行代码 I 时,该面板不会出现在 netbeansFrame 中,而对于代码 II 则会出现该面板。

所以,我想知道几乎相同的代码出现这种异常行为的原因是什么。

最佳答案

  1. Dimension windowDim=myPanel.getSize(); 仅返回 Dimension[0, 0],因为从 <返回 Dimension/p>

    • 已经可见的容器(在您的情况下带有组件JPanel)

    • 调用 pack();

  2. 然后pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);返回Dimension[-100, -50]

  3. 你可以

    • 如果 JPanel 为空,则返回其 PreferredSize

    • 如果 JPanel 不为空,则其 JComponents 返回 PreferredSize 您可以尝试删除/禁用代码行pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);

关于java - Netbeans 中相同 swing 代码的异常行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11468187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com