我是jpanel的总菜鸟,我从网上学到了一切,所以我不知道自己做错了什么。我有一个简单的Jpanel设置功能:
private static void GUI(String TIME, int Action){
if (Action == 1){
JFrame EnterMessage = new JFrame("Tester");
EnterMessage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EnterMessage.setSize(190, 80);
EnterMessage.setVisible(true);
JPanel Panel = new JPanel();
Panel.setBackground(Color.WHITE);
JLabel TextLabel = new JLabel(TIME);
Panel.add(TextLabel);
EnterMessage.add(Panel);
}else {
Panel.removeAll(); //Comment 1
}
}
}
评论1:在删除所有内容之后我仍然需要刷新但是我还没有达到这个目的,首先我需要修复错误(否则你会滚动到很长时间。)
基本上当我将鼠标悬停在' removeAll();'在eclipse中它告诉我:无法从类型Container
中对静态方法removeAll()进行静态引用这是什么意思?以及如何解决它?
我确实搜索了谷歌和堆栈这个但我找不到任何看起来像我的代码所以我无法使用它
完整代码:
package Main;
import java.awt.Color;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Panel;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import junit.framework.Test;
public class MainClass {
static String TIME = functions.gettime();
private static boolean wPressed = false;
public static boolean isWPressed() {
synchronized (MainClass.class) {
return wPressed;
}
}
public static void main(String[] args) {
GUI(TIME,1);
System.out.printf("before key \n");
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent ke) {
synchronized (Test.class) {
switch (ke.getID()) {
case KeyEvent.KEY_PRESSED:
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.printf("Enter Key Detected");
TIME = functions.gettime();
GUI(TIME,2);
break;
}
break;
}
}
return false;
}
});
JFrame test = new JFrame();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(false);
}
private static void GUI(String TIME, int Action){
if (Action == 1){
JFrame EnterMessage = new JFrame("Tester");
EnterMessage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EnterMessage.setSize(190, 80);
EnterMessage.setVisible(true);
JPanel Panel = new JPanel();
Panel.setBackground(Color.WHITE);
JLabel TextLabel = new JLabel(TIME);
Panel.add(TextLabel);
EnterMessage.add(Panel);
}else {
Panel.removeAll();
}
}
}