【问题标题】:Resize a jpanel which has no Jframe in java swing application在java swing应用程序中调整没有Jframe的jpanel
【发布时间】:2015-11-26 17:53:12
【问题描述】:

作为学习的一部分,我在一个简单的 Swing 应用程序中制作了一个 JPanel。 我让它没有装饰。 现在只显示一个 JPanel。我在此面板上应用了组件移动并添加了关闭按钮,但面板的大小是固定的。

如何使我的 JPanel 可调整大小?

这是我的面板

包装 x; 公共类 TestRun 扩展 javax.swing.JFrame {

/**
 * Creates new form TestRun
 */
public TestRun() {
    setUndecorated(true);
    initComponents();
    setMouseListener();
}
private void setMouseListener() {
    MoveMouseListener mlistener = new MoveMouseListener(jMenuBar1);
    jMenuBar1.addMouseListener(mlistener);
    jMenuBar1.addMouseMotionListener(mlistener);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenu2 = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 1090, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 689, Short.MAX_VALUE)
    );

    jMenu1.setText("File");
    jMenuBar1.add(jMenu1);

    jMenu2.setText("Exit");

    jMenuItem1.setText("Exit");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItem1ActionPerformed(evt);
        }
    });
    jMenu2.add(jMenuItem1);

    jMenuBar1.add(jMenu2);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

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

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
    System.exit(EXIT_ON_CLOSE);
}                                          

/**
 * @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(TestRun.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(TestRun.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(TestRun.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(TestRun.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 TestRun().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JPanel jPanel1;
// End of variables declaration                   

}

【问题讨论】:

  • 你能展示你的JPanel代码吗?以及您希望如何调整 JPanel 的大小?在运行期间?
  • 你如何显示这个JPanel
  • I applied component move - 这是什么意思?
  • 更新了代码.. 这个 JPanel。如何使其在运行时可调整大小? (与我们调整窗口或 JFrame 大小的方式相同)
  • 从概念上讲,您需要执行类似this 的操作。此示例向您展示如何移动未装饰的窗口,这也是调整大小的基础

标签: java swing jframe jpanel


【解决方案1】:

您应该能够在JPanel 上使用setPreferredSize(Dimension size); 并在JFrame 上调用pack();

【讨论】:

  • 我知道设置最小/首选/最大尺寸不是实现这种效果的最优雅的方式,而是结合 'pack();'这是将组件调整为所需尺寸并重新计算其周围容器的最简单方法。通常在这样做时,我将所有 3 个维度设置为相同的大小。
  • “其他”问题是,这不是操作实际要求的,他们希望能够手动调整未装饰框架的大小。如果您“需要”提供大小提示,最好根据需要覆盖 getter 方法
猜你喜欢
  • 1970-01-01
  • 2011-05-25
  • 2012-03-27
  • 1970-01-01
  • 1970-01-01
  • 2012-09-01
  • 2013-10-11
相关资源
最近更新 更多