Java Swing无法正确呈现 [英] Java Swing Not Rendering Correctly

查看:130
本文介绍了Java Swing无法正确呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在从事一个项目.在项目的一部分中,我需要使用JPanel. Graphics2D对象被传递到框架的模型部分,并且模型将绘制在该实例处需要绘制的任何内容.问题是JFrame无法正确呈现.它看起来有一半白色阴影并且无法正确绘制.它可以在我的笔记本电脑(Sony Vaio,Windows7)上正常工作,但不能在我测试过的所有其他系统(2台戴尔笔记本电脑,windows8; 1个东芝,windows7; 1个三星,windows8)上正确渲染.在队友测试过的其他几个系统上,它也可以正确渲染.

I have been working on a project. In one part of the project I needed to draw on JPanel. Graphics2D object is passed to the model part of the framework and model will draw whatever it needs to draw at that instance. Problem is JFrame is not rendered correctly. It looks half white shaded and not painting correctly. It works fine on my laptop (Sony Vaio, Windows7) but it's not rendering correctly in all other systems I tested (2 Dell laptops,windows8;1 Thoshiba,windows7;1 samsung, windows8). It's also rendering correctly on few other systems my teammate tested.

我确定模型零件没有错误.问题似乎不在JPanel上呈现.但是我能提供的一个信息是,我们正在Netbeans中进行项目.也许是由Netbeans IDE生成的代码引起了问题?我检查了代码,但无法确定问题.为此,很难发布SSCCE,但我将分享该问题的一些屏幕截图.

I'm sure there are no errors made in model part. Problem doesn't seem to be rendering on JPanel. But one information I can give is that we're doing our project in Netbeans. Perhaps the code generated by Netbeans IDE causing the problem? I checked the code but couldn't Identify the problem. It's difficult to post an SSCCE for this but I will share some screen shots of the issue.

好吧,因为我没有足够的声誉来发布图片,请参阅下面的链接....

Ok since I don't have enough reputation to post images see the links below....

[Displays Wrongly][1]
   [Displays Wrongly][2]
   [Displays correctly on my system][3]
   [Displays Correctly on my system][4]

我尝试调试它,但不知道问题出在哪里.... 更新 这不是SSCCE,但可能会有所帮助

I tried to debug it but can't know what the problem is.... Update It's not an SSCCE but it may help

JFrame的代码....

Code for JFrame....

<pre>

import btapp.Question;
import btapp.qGens.QGenGeomAbstract;
import btapp.qGens.QGenI;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;

public class UItestGeomFrame extends javax.swing.JFrame implements UIFrameI, UIGeomFrameAbstract {

    /**
     * Creates new form UItestGeomFrame
     */
    public UItestGeomFrame() {
        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() {

        jLabel1 = new javax.swing.JLabel();
        unitTestGeomPanel1 = new btapp.ui.UnitTestGeomPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Draw Panel JUnitTestCase");

        unitTestGeomPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout unitTestGeomPanel1Layout = new javax.swing.GroupLayout(unitTestGeomPanel1);
        unitTestGeomPanel1.setLayout(unitTestGeomPanel1Layout);
        unitTestGeomPanel1Layout.setHorizontalGroup(
            unitTestGeomPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 406, Short.MAX_VALUE)
        );
        unitTestGeomPanel1Layout.setVerticalGroup(
            unitTestGeomPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 260, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(160, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(142, 142, 142))
            .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addComponent(unitTestGeomPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(30, 30, 30)
                .addComponent(unitTestGeomPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

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

    @Override
    public void presentQuestions(Question qO, int qNo) {
        ArrayList qd = qO.getQuestionDraw();
        unitTestGeomPanel1.setQObject(qd);
        unitTestGeomPanel1.repaint();

    }

    @Override
    public void presentAnswers(Question qO, int qNo) {
        ArrayList ad = qO.getAnswerDraw();
        unitTestGeomPanel1.setQObject(ad);
        unitTestGeomPanel1.repaint();
    }

    @Override
    public void presentQuestions(Question qO, int qNo, QGenI qg) {
        unitTestGeomPanel1.setqGen(qg, qO, 0);
        unitTestGeomPanel1.repaint();
    }

    @Override
    public void presentAnswers(Question qO, int qNo, QGenI qg) {
        unitTestGeomPanel1.setqGen(qg, qO, 1);
        unitTestGeomPanel1.repaint();
    }


    @Override
    public int getHeight(){
        return unitTestGeomPanel1.getHeight();
    }

    @Override
    public int getWidth(){
        return unitTestGeomPanel1.getWidth();
    }

}
</pre>

JPanel的代码



    import btapp.Question;
    import btapp.qGens.QGenGeomAbstract;
    import btapp.qGens.QGenI;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import static java.awt.image.ImageObserver.HEIGHT;
    import static java.awt.image.ImageObserver.WIDTH;
    import java.util.ArrayList;


    public class UnitTestGeomPanel extends javax.swing.JPanel {
        private int type;

        /**
         * Creates new form UnitTestGeomPanel
         */
        public UnitTestGeomPanel() {
            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")
        //                           
        private void initComponents() {

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
        }//                         
        // Variables declaration - do not modify                     
        // End of variables declaration                   
        ArrayList dAL = new ArrayList();
        private double h, w;

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            System.out.println("repainign based on QO object of type "+type);

            if(type == 0){
                qg.presentQuestions(qO, (Graphics2D)g);
            }
            else
                if(type == 1)
                    qg.presentAnswers(qO, (Graphics2D)g);
                }

        void setQObject(ArrayList dAL) {
            this.dAL = dAL;
        }

        QGenI qg;
        Question qO;

        void setqGen(QGenI qg, Question qO, int type) {
            this.qO = qO;
            this.qg = qg;
            this.type = type;
        }
    }

请帮助我... 预先感谢

Please help me... Thanks in advance

几天后,我的系统中也出现了问题,但没有那么严重.请参阅注释中的图像.

Days later problem occurs in my system also but not as worse. Please refer to the images in comment.

推荐答案

我遇到了同样的问题,所以我将显示设置更改为高性能",并且解决了该问题(在Windows 8和Java 1.8中).

I had the same problem, so I changed my display setting to High performance and it SOLVED the problem (in Windows 8 And java 1.8).

这篇关于Java Swing无法正确呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆