小程序背景闪烁



我做了一个简单的测试小程序,它有一个红色的背景和几个按钮。当我运行applet(在http://nuevawave.org/sandbox/JavaGallery/GUIApplet.html)按钮显示,但红色没有。当我点击小程序时,有时部分背景会闪烁红色。有人知道是什么问题吗?

下面是applet代码:

package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JApplet.*;
public class GUIApplet extends javax.swing.JApplet {
/** Initializes the applet GUIApplet */
public void init() {
    try {
        java.awt.EventQueue.invokeAndWait(new Runnable() {
            public void run() {
                initComponents();
            }
        });
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
public void paint(Graphics g) {
    super.paint(g);
}
/** This method is called from within the init() method 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() {
    jTextField1 = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    setBackground(new java.awt.Color(255, 0, 0));
    setMaximumSize(new java.awt.Dimension(250, 300));
    setPreferredSize(new java.awt.Dimension(250, 300));
    setSize(new java.awt.Dimension(250, 300));
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
    jTextField1.setText("jTextField1");
    getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 170, -1, -1));
    jButton1.setText("jButton1");
    getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(17, 90, -1, -1));
    jButton2.setText("jButton2");
    getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 90, -1, -1));
}// </editor-fold>

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}

您需要设置JApplet的contentPane的背景,而不是JApplet本身,因为实际上是contentPane保存组件并显示组件。在初始化方法中调用getContentPane().setBackground(...);,而不是直接调用setBackground(...)

相关内容

  • 没有找到相关文章

最新更新