Java 无法从我的Tester类访问画布类中的paint(Graphics g)方法
这是一个待办事项列表,当用户选择5时,图像应显示在画布上,但在我的测试仪中,我无法从Canvas类访问paintGraphics g方法 我在测试仪中得到一个错误,它说不能从静态上下文引用非静态方法。在第5种情况下,变量g也无法解析 这是帆布课Java 无法从我的Tester类访问画布类中的paint(Graphics g)方法,java,android,image,canvas,Java,Android,Image,Canvas,这是一个待办事项列表,当用户选择5时,图像应显示在画布上,但在我的测试仪中,我无法从Canvas类访问paintGraphics g方法 我在测试仪中得到一个错误,它说不能从静态上下文引用非静态方法。在第5种情况下,变量g也无法解析 这是帆布课 import java.awt.*; import javax.swing.*; import java.awt.Canvas; class Motivation extends JFrame { { Motivation a = new
import java.awt.*;
import javax.swing.*;
import java.awt.Canvas;
class Motivation extends JFrame {
{
Motivation a = new Motivation();
a.setVisible(true);
}
Image img = Toolkit.getDefaultToolkit().getImage("motivation.jpg");
public Motivation() {
super("motivation");
getContentPane().add(new Canvas(), BorderLayout.CENTER);
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
class MyCanvas extends Canvas {
public void paint(Graphics g) {
g.drawImage(img, 5, 5, this);
paint(g);
}
}
}
这是测试仪
import java.awt.*;
import java.io.*;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicReference;
public class ToDoListTester {
public static void main(String[] args) throws IOException {
AtomicReference<String> userName = new AtomicReference<String>();
Scanner name = new Scanner(System.in);
System.out.println("\n FINALS TO-DO LIST \n");
System.out.println("Please Enter Your name");
userName.set(name.nextLine());
System.out.println("Hello " + userName + "!");
int select = -1;
while (select != 0) {
select = ToDoList.menu();
switch (select) {
case 1: // case: different form of for/while loop. If the user selects 1, show the To Do list
ToDoList.showList();
break;
case 2: //if the user selects 2, show the To Do list
ToDoList.addTask();
break;
case 3: //if the user selects 3, show the To Do list
ToDoList.removeTask();
break;
case 4:
ToDoList.setTimer(); //if the user selects 4, the countdown timer is started.
break;
case 5:
Motivation motivateMe = new Motivation();
motivateMe.MyCanvas.paint(Graphics g);
case 0: //if the user selects 0, the program is stopped, takes us out of the loop.
break;
default: // different form of else. If the user does something we don't recognize/
System.out.println("Well you're just a little rebel aren't you?");
System.out.println("Try again.");
System.out.println("Select 1 to display your To Do List.");
System.out.println("Select 2 to add a task to your To Do list.");
System.out.println("Select 3 to remove a task from your To Do list.");
System.out.println("Select 4 to set a countdown timer for your task.");
System.out.println("Select 5 for some motivation. ");
System.out.println("Select 0 to exit the program.");
}
}
}
}永远不要调用绘画。。。方法,Swing将确定何时需要重新绘制组件。Swing随后将创建绘画方法使用的图形对象 如果要手动重新绘制组件,请使用:
someComponent.repaint();
虽然在你的情况下,那么没有必要这样做。创建组件后,实际上必须将画布添加到框架中,以便对其进行绘制
还有,为什么要使用画布?这是一个AWT组件。使用Swing时应该使用JPanel
实际上,如果您只是试图显示图像,那么请使用JLabel。阅读Swing教程中关于的部分以了解更多信息和工作示例。请详细说明错误消息好吗?@JoeC case 5:Motivation.MyCanvas.paintg;当我尝试从MyCanvas类访问paint方法时,我得到:不能从静态上下文引用非静态方法“paintjava.awt.Graphics”。在这一点上,我甚至不确定我是否有从主类访问该方法的正确代码。可能重复@JoeC谢谢,这是一个很好的建议。我从中得到的是,我需要在调用该方法之前创建一个实例。但是,我仍然得到一个错误,变量“g”无法解析。您在main方法中的何处定义g?