重绘不工作

时间:2015-07-23 16:32:35

标签: java swing awt

我遇到了重绘功能的问题。 这是在我的jFrame中,我将这个词传递给我的jPanel。

package Grafisch;
import Logica.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class LingoApp extends javax.swing.JFrame implements ActionListener {

/**
 * Creates new form LingoApp
 */

Letter letter = new Letter();
Woordenlijst woordenlijst = new Woordenlijst();
Lingo lingo = new Lingo();
NewJPanel NewJPanel = new NewJPanel();

public LingoApp() {
    initComponents();
    VolgendeKnop.setEnabled(false);
    ControleerKnop.setEnabled(false);
    GokInput.addActionListener(this);  
}




private void ControleerKnopActionPerformed(java.awt.event.ActionEvent evt) {                                               

    String input = GokInput.getText().replaceAll("\\s","");
    if(input.contains("0123456789") || input.length() <5 || input.length() >5 || input.isEmpty()){
        GokInput.setText("fout");
    }
    else {
        letter.checkLetters(input);
        lingo.vermeerderGok();
        BeurtLabelHoeveelheid.setText(Integer.toString(lingo.geefGokken()));
        ///Here we go
        NewJPanel.setWord(input);
    }
    if(!lingo.checkGokken()){
        AllerleiLabel.setText("Helaas, het woord was: "+ letter.geefWoord()+".");
        ControleerKnop.setEnabled(false);
        VolgendeKnop.setEnabled(true);
    }
    if(lingo.checkWoord(input)){
        AllerleiLabel.setText("Proficiat, het woord was inderdaad "+ letter.geefWoord()+".");
        BeurtLabelHoeveelheid.setText("6");//interface fix
        ScoreLabelHoeveelheid.setText(Integer.toString(lingo.updateScore()));
        lingo.vermeerderBeurt();
        ControleerKnop.setEnabled(false);
        VolgendeKnop.setEnabled(true);
    }
    if(lingo.checkBeurten()){
        if(lingo.checkGoedGeraden()){
            System.out.println("ditmoetnogwordengedaan, 10x goed geraden");
        }
        else {
            System.out.println("ditmoetnogwordengedaan, !10x goed geraden");
        }
        VolgendeKnop.setEnabled(false);
        OpgevenKnop.setVisible(true);
        StartSpelKnop.setVisible(true);
    }

}                                              

private void StartSpelKnopActionPerformed(java.awt.event.ActionEvent evt) {                                              

    GokInput.setText("");//fix voor nieuwe ronde
    AllerleiLabel.setText("");//fix voor nieuwe ronde
    OpgevenKnop.setVisible(false);
    StartSpelKnop.setVisible(false);
    woordenlijst.maakLijst();
    String temp = woordenlijst.geefWillekeurigWoord();
    letter.setWoord(temp);
    lingo.setWoord(temp);
    ControleerKnop.setEnabled(true);
    //maakt lijst bij start spel nadat op de knop is gedrukt.De knop zal dan ook verdwijnen.
    //geef woord door aan de andere klasses.
}                                             

private void VolgendeKnopActionPerformed(java.awt.event.ActionEvent evt) {                                             

    ///Add reset code pls
    String temp = woordenlijst.geefWillekeurigWoord();
    letter.setWoord(temp);
    lingo.setWoord(temp);
    ControleerKnop.setEnabled(true);
    VolgendeKnop.setEnabled(false);
    AllerleiLabel.setText("");
    GokInput.setText("");
    lingo.softReset();
}                                            

private void OpgevenKnopActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
    dispose();
}                                           

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {


    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new LingoApp().setVisible(true);
        }
    });
}



// Variables declaration - do not modify                     
private javax.swing.JLabel AllerleiLabel;
private javax.swing.JLabel BeurtLabel;
private javax.swing.JLabel BeurtLabelHoeveelheid;
private javax.swing.JButton ControleerKnop;
private javax.swing.JTextField GokInput;
private javax.swing.JLabel GokLabel;
private javax.swing.JButton OpgevenKnop;
private javax.swing.JLabel ScoreLabel;
private javax.swing.JLabel ScoreLabelHoeveelheid;
private javax.swing.JButton StartSpelKnop;
private javax.swing.JButton VolgendeKnop;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
// End of variables declaration                   

@Override
public void actionPerformed(ActionEvent ae) {
   ControleerKnop.doClick();
   //eventlistener implementatie
}
}

所以我试图在我的jpanel中绘制一个简单的String。 方法setWord通过单击按钮从我的jframe获取输入。 因此在我的jPanel里面;

package Grafisch;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;


public class NewJPanel extends javax.swing.JPanel {

private String word = "example";

/**
 * Creates new form NewJPanel
 */
public void setWord(String input){
    this.word=input;
    repaint();
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawString(word, 75, 100); // changed the location to make it easier to see
}



public NewJPanel() {
    initComponents();
}
}

1 个答案:

答案 0 :(得分:3)

请注意,我并不认为您发布的代码向我们展示了您的错误来源,如果您努力创建和发布您的最小示例,那么您就是这样做的。我可能会得到一个不错的答案,但如果没有这个,很可能这个问题仍然没有答案。很可能你可能有两个类的实例保存上面的代码,一个显示,一个没有显示,你可以在未显示的上调用getWord(String input),但这只是一个疯狂的猜测。顺便说一句,考虑更改该方法的名称,因为它不是真正的吸气剂&#34;方法,而是一个&#34; setter&#34;方法,因此应重命名为setWord(String word)

此时所有人都可以说,您发布的代码中的问题。作为概念证明,请说我将您发布的代码修改为可编辑的可编辑类:

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;

@SuppressWarnings("serial")
class ShowWordPanel extends JPanel {
   private static final int PREF_W = 500;
   private static final int PREF_H = 250;
   private static final Font WORD_FONT = new Font(Font.DIALOG, Font.BOLD, 20);
   private String word = "example";

   // Change the name of this method from getXxx to setXxx 
   public void setWord(String input) {
      this.word = input;
      repaint();
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setFont(WORD_FONT); // let's make it big enough to see well
      g.drawString(word, 75, 100); // changed the location to make it easier to see
   }

   @Override // so that the JPanel wil be big enough to show the text
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }
}

然后我可以在GUI创建类中使用上面的类,它可以正常工作:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings({ "serial"})
public class ShowWordPanelTest extends JPanel {
   // create a ShowWordPanel field -- this is to help guarantee
   // that the instance displayed is the same as the one I change 
   // the value of the word field
   private ShowWordPanel showWordPanel = new ShowWordPanel();
   private JTextField textField = new JTextField(10);
   private AddWordAction addWordAction = new AddWordAction("Add Word");

   public ShowWordPanelTest() {
      textField.setAction(addWordAction);
      JPanel topPanel = new JPanel();
      topPanel.add(textField);
      topPanel.add(new JButton(addWordAction));

      showWordPanel.setBorder(BorderFactory.createTitledBorder("Show Word Panel"));

      setLayout(new BorderLayout());
      add(showWordPanel, BorderLayout.CENTER);
      add(topPanel, BorderLayout.PAGE_START);
   }

   private class AddWordAction extends AbstractAction {
      public AddWordAction(String name) {
         super(name);
         int mnemonic = (int) name.charAt(0);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         String text = textField.getText(); // get text
         showWordPanel.setWord(text); // pass it into the showWordPanel

         // make easy to change text after button press
         textField.selectAll();
         textField.requestFocusInWindow();
      }
   }

   private static void createAndShowGui() {
      ShowWordPanelTest mainPanel = new ShowWordPanelTest();

      JFrame frame = new JFrame("ShowWordPanelTest");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}