【问题标题】:How to change the Default color of a JFrame如何更改 JFrame 的默认颜色
【发布时间】:2014-08-31 16:03:07
【问题描述】:

我正在将背景颜色添加到 My JFrame 程序。但是,在我的程序中,我将菜单栏和收费栏添加到 JFrame。这里背景颜色应用于工具栏。我想避免收费栏的背景颜色。我想要在工具栏后应用背景颜色。请帮助我。在此先感谢您。

我的代码是:

public class JFrameDemo extends javax.swing.JFrame {

public JFrameDemo() {

   Color b=new Color(0,150,255);
   getContentPane().setBackground(b);
   initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    tollBar = new javax.swing.JToolBar();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenu2 = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    tollBar.setRollover(true);

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

    jMenu2.setText("Edit");
    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(tollBar, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(tollBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 254, Short.MAX_VALUE))
    );

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

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

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
           // ImageIcon img = new ImageIcon("C:\\Icons\book-edit-icon.png");
            JFrameDemo fdemo=new JFrameDemo();

            fdemo.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.JToolBar tollBar;
// End of variables declaration                   }

【问题讨论】:

  • 将 JPanel 添加到您的 JFrame 并更改它的颜色。菜单栏不会受到影响
  • if ("Nimbus".equals(info.getName())) { 为什么不使用不同的 PLAF(将工具栏的 BG 颜色绘制成更好的颜色)?
  • 谢谢。请提供示例代码。
  • 请给工具栏设置背景色

标签: java swing colors awt background-color


【解决方案1】:
container=getContentPane();
container.setBackground(Color.white);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-05
    • 2021-10-23
    • 2012-01-26
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多