我的重绘功能遇到问题。 这是在我的 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 中绘制一个简单的字符串。 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();
}
}
最佳答案
请注意,我不认为您发布的代码向我们展示了错误的根源,如果您确实努力创建并发布最小示例,您可能会得到一个不错的答案,但如果没有这个,这个问题可能仍然没有答案。很可能您可能有两个保存上述代码的类的实例,一个显示,一个不显示,并且您可以在不显示的实例上调用 getWord(String input)
,但是这个只是一个疯狂的猜测。顺便说一句,考虑更改该方法的名称,因为它不是真正的“getter”方法,而是“setter”方法,因此应重命名为 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();
}
});
}
}
关于java - 重新喷漆不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31593164/