重画不工作,我的私人领域的价值会不会改变? [英] Repaint doesn't work and the value of my private fields won't change?

查看:291
本文介绍了重画不工作,我的私人领域的价值会不会改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在在Netbeans的图形界面,你可以把一系列数字(例如:7 8 5 4 10 13),在文本框punten,当你preSS按钮ververs图形线型图所有的数字应该出现(在我的面板)。我做了一个类桂与文本字段,按钮并在它的面板延伸的JFrame。我还做了一类Grafiek扩展JPanel并与该面板链接我的桂。

这是我遇到的问题是:重绘();命令将不会去的paintComponent(图形G) - 方法和我的私有变量不会改变(平底船和本地人入住0的长度)。

有人可以帮我,我一直在做这个项目了好几天。

我的GUI类:

 进口java.awt.Graphics;公共类桂扩展javax.swing.JFrame中{公共桂(){
    的initComponents();
    面板=新javax.swing.JPanel中();
}/ **
 *这个方法是从构造函数中调用初始化形式。
 *警告:不要修改此code。该方法的内容是始终
 *由窗体编辑器再生。
 * /
@燮pressWarnings(未登记)
//<主编倍defaultstate =崩溃DESC =生成code>私人无效的initComponents(){    punten =新javax.swing.JTextField中的();
    FOUT =新javax.swing.JLabel中的();
    javax.swing.JButton中ververs =新javax.swing.JButton中的();
    面板=新Grafiek();    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);    fout.setText(J);    ververs.setText(Ververs);
    ververs.addActionListener(新java.awt.event.ActionListener(){
        公共无效的actionPerformed(EVT java.awt.event.ActionEvent中){
            verversActionPerformed(EVT);
        }
    });    panel.setBorder(javax.swing.BorderFactory.createLineBorder(新java.awt.Color中(0,0,0),2));    javax.swing.GroupLayout panelLayout =新javax.swing.GroupLayout(面板);
    panel.setLayout(panelLayout);
    panelLayout.setHorizo​​ntalGroup(
        panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0,0,Short.MAX_VALUE)
    );
    panelLayout.setVerticalGroup(
        panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0,195,Short.MAX_VALUE)
    );    javax.swing.GroupLayout布局=新javax.swing.GroupLayout(的getContentPane());
    的getContentPane()的setLayout(布局)。
    layout.setHorizo​​ntalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(面板,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)
                .addComponent(punten,javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING,layout.createSequentialGroup()
                    .addComponent(ververs)
                    .addGap(6,6,6)
                    .addComponent(FOUT)
                    .addGap(0,302,Short.MAX_VALUE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(punten,javax.swing.GroupLayout。preFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout。preFERRED_SIZE)
            .addGap(18,18,18)
            .addComponent(面板,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)
            .addGap(18,18,18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(FOUT)
                .addComponent(ververs))
            .addContainerGap())
    );    包();
} //< /编辑倍>私人无效verversActionPerformed(EVT java.awt.event.ActionEvent中){
    Grafiek格拉夫=新Grafiek();
    graf.verwerkData(punten.getText());
}/ **
 * @参数ARGS的命令行参数
 * /
公共静态无效的主要(字符串ARGS []){
    / *设置Nimbus外观* /
    //<主编倍defaultstate =崩溃DESC =外观设置code(可选)>
    / *如果雨云(Java SE 6中引入)不可用,留在默认的外表和感觉。
     *详见http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     * /
    尝试{
        对于(javax.swing.UIManager.LookAndFeelInfo信息:javax.swing.UIManager.getInstalledLookAndFeels()){
            如果(雨云.equals(info.getName())){
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                打破;
            }
        }
    }赶上(ClassNotFoundException的前){
        。java.util.logging.Logger.getLogger(Gui.class.getName())日志(java.util.logging.Level.SEVERE,空,前);
    }赶上(InstantiationException前){
        。java.util.logging.Logger.getLogger(Gui.class.getName())日志(java.util.logging.Level.SEVERE,空,前);
    }赶上(IllegalAccessException前){
        。java.util.logging.Logger.getLogger(Gui.class.getName())日志(java.util.logging.Level.SEVERE,空,前);
    }赶上(javax.swing.UnsupportedLookAndFeelException前){
        。java.util.logging.Logger.getLogger(Gui.class.getName())日志(java.util.logging.Level.SEVERE,空,前);
    }
    //< /编辑倍>    / *创建和显示表单* /
    了java.awt.EventQueue.invokeLater(新的Runnable(){
        公共无效的run(){
            新的GUI()调用setVisible(真)。
        }
    });
}//变量声明 - 不修改
私人javax.swing.JLabel中FOUT;
私人javax.swing.JPanel中板;
私人javax.swing.JTextField中punten;
//变量声明结束
}

和我的Grafiek级:

 进口java.awt.Graphics;公共类Grafiek扩展javax.swing.JPanel中{私有String []撑船;
私人诠释[]本地人;
私人INT afstandX,afstandY,puntX1 = 0,puntY1 = 0,puntX2 = 0,puntY2 = 0;
私人INT最大= 1;/ **
 *创建新形式Grafiek
 * /
公共Grafiek(){
    的initComponents();
}@覆盖
公共无效的paintComponent(图形G){
    super.paintComponent方法(G);    尝试{
        的for(int i = 0; I< punti.length;我++){
            如果(MAX< =本地人[I]){
                最大=本地人[I]
            }
        }        afstandX =的getWidth()/ punt.length;
        afstandY =的getHeight()/ MAX;        的for(int i = 0; I< punti.length;我++){
            puntX1 = puntX2;
            如果(我== 0){
                puntY1 =的getHeight();
            }
            否则puntY1 = puntY2;
            puntX2 + = afstandX;
            puntY2 =的getHeight() - 本地人[I] * afstandY;            g.drawLine(puntX1,puntY1,puntX2,puntY2);
        }        puntX2 = 0;
        puntY2 = 0;
    }赶上(NPE显示java.lang.NullPointerException){
        super.paintComponent方法(G);
    }
}公共无效verwerkData(String s)将{
    平底船= s.split();    本地人=新INT [punt.length]    的for(int i = 0; I< punt.length;我++){
        本地人[I] =的Integer.parseInt(平底船[I]);
    }    重绘();
}/ **
 *这个方法是从构造函数中调用初始化形式。
 *警告:不要修改此code。该方法的内容是始终
 *由窗体编辑器再生。
 * /
@燮pressWarnings(未登记)
//<主编倍defaultstate =崩溃DESC =生成code>
私人无效的initComponents(){    的setBackground(java.awt.Color中新(255,255,255));    javax.swing.GroupLayout布局=新javax.swing.GroupLayout(本);
    this.setLayout(布局);
    layout.setHorizo​​ntalGroup(
        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)
    );
} //< /编辑倍>}


解决方案

由于您添加指向错误的对象中的code不起作用。

首先,你的面板对象不是类型 Grafiek ,所有的第二它首先在初始化该方法的initComponents(),然后在构造函数中再次改写。

问题二,在方法 verversActionPerformed 的操作侦听器调用,您创建 Grafiek 的新实例显然不是前加入已创建的/。

因此​​,为了使工作改变code如下:

构造看起来应该如下:

 公共桂(){
    的initComponents();
    //面板=新javax.swing.JPanel中();
}

这样的方法:

 私人无效verversActionPerformed(EVT java.awt.event.ActionEvent中){
    Grafiek格拉夫=(Grafiek)面板;
    graf.verwerkData(punten.getText());
}

因此​​,它可以作为你的预期。

不过,这是远离不错code。你应该设置变量面板类型为正确的类型 Grafiek

I am making a graphical interface in Netbeans where you can put a series of numbers (example: 7 8 5 4 10 13) in the textfield "punten" and when you press the button "ververs" a graphical linechart of all the numbers should appear (in my panel). I made a class "Gui" that extends JFrame with the Textfield, the button and a panel in it. I also made a class "Grafiek" that extends JPanel and that is linked with the panel in my "Gui".

The problems that I experience are: the repaint(); command won't go to the paintComponent(Graphics g)-method and my private variables won't change (the length of punt and punti stays 0).

Can somebody please help me, I've been working on this project for days.

My Gui-class:

import java.awt.Graphics;

public class Gui extends javax.swing.JFrame {

public Gui() {
    initComponents();
    panel = new javax.swing.JPanel();
}

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

    punten = new javax.swing.JTextField();
    fout = new javax.swing.JLabel();
    javax.swing.JButton ververs = new javax.swing.JButton();
    panel = new Grafiek();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    fout.setText("j");

    ververs.setText("Ververs");
    ververs.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            verversActionPerformed(evt);
        }
    });

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

    javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
    panel.setLayout(panelLayout);
    panelLayout.setHorizontalGroup(
        panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );
    panelLayout.setVerticalGroup(
        panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 195, 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()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(punten, javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                    .addComponent(ververs)
                    .addGap(6, 6, 6)
                    .addComponent(fout)
                    .addGap(0, 302, Short.MAX_VALUE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(punten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(fout)
                .addComponent(ververs))
            .addContainerGap())
    );

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

private void verversActionPerformed(java.awt.event.ActionEvent evt) {                                        
    Grafiek graf = new Grafiek();
    graf.verwerkData(punten.getText());
}                                       

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

// Variables declaration - do not modify                     
private javax.swing.JLabel fout;
private javax.swing.JPanel panel;
private javax.swing.JTextField punten;
// End of variables declaration                   
}

And my "Grafiek"-class:

import java.awt.Graphics;

public class Grafiek extends javax.swing.JPanel {

private String[] punt;
private int[] punti;
private int afstandX, afstandY, puntX1=0, puntY1=0, puntX2=0, puntY2=0;
private int max=1;

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

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    try{
        for(int i=0; i<punti.length; i++) {
            if(max <= punti[i]) {
                max = punti[i];
            }
        }

        afstandX = getWidth()/punt.length;
        afstandY = getHeight()/max;

        for(int i=0; i<punti.length; i++) {
            puntX1 = puntX2;
            if(i == 0) {
                puntY1 = getHeight();
            }
            else puntY1 = puntY2;
            puntX2 += afstandX;
            puntY2 = getHeight() - punti[i]*afstandY;

            g.drawLine(puntX1, puntY1, puntX2, puntY2);
        }

        puntX2 = 0;
        puntY2 = 0;
    }catch(java.lang.NullPointerException npe) {
        super.paintComponent(g);
    }
}

public void verwerkData(String s) {
    punt = s.split(" ");

    punti = new int[punt.length];

    for(int i=0; i<punt.length; i++) {
        punti[i] = Integer.parseInt(punt[i]);
    }

    repaint();
}

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

    setBackground(new java.awt.Color(255, 255, 255));

    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)
    );
}// </editor-fold>                        

}

解决方案

The code does not work because you add your points to the wrong object.

First of all, your panel object is not of type Grafiek, second of all it is first initialized in the method initComponents() and then overwritten again in the constructor.

Second problem, in the method verversActionPerformed called by the action listener, you create a new instance of Grafiek that is obviously not the one you have created/added before.

Thus, to make it work alter the code as follows:

The constructor should look the following:

public Gui() {
    initComponents();
    //panel = new javax.swing.JPanel();
}

The method like this:

private void verversActionPerformed(java.awt.event.ActionEvent evt) {                                        
    Grafiek graf = (Grafiek)panel;
    graf.verwerkData(punten.getText());
}

Thus, it works as you expected.

However, this is far away from good code. You should set the type of variable panel to the correct type Grafiek.

这篇关于重画不工作,我的私人领域的价值会不会改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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