[英]boolean not working on graphics java
Here is my code这是我的代码
package javaapplication7;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Graphics2D;``
import java.util.*;
import java.io.*;
public class JavaApplication7 extends JPanel implements ActionListener{
JButton j = new JButton();
boolean drawHello = true;
boolean drawWorld = false;
String hello = "Hello";
String World = "World";
public JavaApplication7(){
this.setLayout(new BorderLayout());
add(j,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e) {
boolean drawWorld = true;
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (drawHello)
g.drawString(hello, 50, 50);
if (drawWorld)
g.drawString(World, 70, 70);
}
public static void main(String[] args) {
JFrame f = new JFrame("Swing Paint Demo");
JavaApplication7 j7 = new JavaApplication7();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(250,250);
f.setVisible(true);
f.add(j7);
}
}
I think you want我想你想要
public void actionPerformed(ActionEvent e) {
drawWorld = true;
repaint();
}
If you re-declare it in your method, the global value will not be updated.如果你在你的方法中重新声明它,全局值将不会更新。
What I don't understand that you've defined a Boolean
and then you create another one in actionPerformed()
just remove the Boolean
as follows:我不明白你已经定义了一个Boolean
,然后你在actionPerformed()
创建了另一个Boolean
,如下所示:
public void actionPerformed(ActionEvent e) {
drawWorld = true;
repaint();
}
Because you allready created it there :因为你已经在那里创建了它:
public class JavaApplication7 extends JPanel implements ActionListener{
JButton j = new JButton();
boolean drawHello = true;
boolean drawWorld = false; //<------
Also add还添加
j.addActionListener(this);
Like this :像这样 :
public JavaApplication7(){
this.setLayout(new BorderLayout());
j.addActionListener(this);
add(j,BorderLayout.NORTH);
}
you have two mistakes in your code.你的代码中有两个错误。
The first one, you want to add the ActionListener
to the JButton
, otherwise you will never execute the actionPerformed
method.(i simply guess you want the action to be performed on the button).第一个,您想将ActionListener
添加到JButton
,否则您将永远不会执行actionPerformed
方法。(我只是猜测您希望在按钮上执行操作)。
public JavaApplication7() {
this.setLayout(new BorderLayout());
add(j, BorderLayout.NORTH);
j.addActionListener(this);
}
the second one is that you are using a local variable in the actionPerformed
method, remove the type identifier there to assign the class variable.第二个是你在actionPerformed
方法中使用了一个局部变量,删除那里的类型标识符来分配类变量。 Otherwise you will only set the local variable drawWorld
to true, which wont change the initial value of the class variable drawWorld
.否则,您只会将局部变量drawWorld
设置为 true,这不会更改类变量drawWorld
的初始值。 In this case the paintComponent
wont notify the "change" you have done to the variable darwWorld
because you never reassigned it as true
.在这种情况下, paintComponent
不会通知您对变量darwWorld
所做的“更改”,因为您从未将其重新分配为true
。
public void actionPerformed(ActionEvent e) {
drawWorld = true;
repaint();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.