【问题标题】:Java Painting a Triangle and Trying to use other Swing ObjectsJava 绘制三角形并尝试使用其他 Swing 对象
【发布时间】:2011-11-23 02:41:16
【问题描述】:

我在使用 Netbeans 设计 GUI(是的,我很懒:\)并手动尝试在 JFrame 上绘制三角形时遇到了一点问题。在我按下制表符并将焦点放在对象上之前,Swing 组件被“隐藏”起来。我附上了picture 和下面的问题代码。

所有自动生成的 GUI 代码都在代码的 initComponents() 部分。并且分类生成在 JFrame Paint 方法的覆盖代码中。 看起来正在发生的是 initComponents 代码在绘制之前运行,因为该对象是在 setVisibile(true) 之前创建的。一旦调用了 setVisible(true),paint 方法就会在所有生成的 initComponents 对象上进行绘制。只是在寻找一个解决方案,以免被掩盖。

任何帮助将不胜感激。

 $/*
  * To change this template, choose Tools | Templates
  * and open the template in the editor.
  */

 /*
  * SimpleClient.java
  *
  * Created on Sep 22, 2011, 11:38:30 AM
  */
 package Assignment3;

 import java.awt.Graphics;

 /**
  *
  * @author Mark
  */
 public class SimpleClient extends javax.swing.JFrame {

/** Creates new form SimpleClient */
public SimpleClient() {
    initComponents();
}

public void paint(Graphics g) {
    int[] xPoints = {100, 100, 200};
    int[] yPoints = {100, 200, 200};
    g.drawPolygon(xPoints, yPoints, 3);
}

/** 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() {

    jTextField1 = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jTextField1.setText("jTextField1");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(103, 103, 103)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(238, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(220, Short.MAX_VALUE)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(60, 60, 60))
    );

    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(SimpleClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(SimpleClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(SimpleClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(SimpleClient.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 SimpleClient().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JTextField jTextField1;
// End of variables declaration
}

【问题讨论】:

    标签: java swing awt paint graphics2d


    【解决方案1】:

    一些快速建议:

    • 不要直接在 JFrame 中绘制。
    • 改为在 JComponent(如 JPanel)中进行绘制。
    • 覆盖 JPanel 的paintComponent 方法,而不是paint 方法。
    • 调用 super.paintComponent(g),通常作为您的 paintComponent 方法的第一个方法调用,以允许您的 JPanel 进行内部管理并删除旧图像。
    • 阅读一些关于 Swing 图形的教程,因为对于我们许多人(尤其是我)来说,它并不直观,您必须打破一些假设才能正确地做到这一点。

    【讨论】:

      【解决方案2】:

      不要覆盖顶级容器(JFrame、JDialog...)的 paint() 方法。

      自定义绘画是通过覆盖 JPanel(或 JComponent)的 paintCompnent() 方法来完成的。然后将该组件添加到框架的内容窗格中。不要忘记覆盖组件的 getPreferredSize() 方法,这样布局管理器才能正常工作。

      阅读Custom Painting 了解更多信息和工作示例。

      【讨论】:

        猜你喜欢
        • 2017-06-29
        • 1970-01-01
        • 2021-11-07
        • 2014-11-18
        • 2012-10-17
        • 1970-01-01
        • 2013-07-04
        • 2017-01-03
        • 1970-01-01
        相关资源
        最近更新 更多