[英]repaint() not called when running app from Eclipse?
当我使用Eclipse和Run-> cmd运行程序时,我从未将“绘画”写入命令行窗口。 如果我从另一个程序中的paintComponent运行System.out.print(),它将很好地工作。 有人可以帮忙吗?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI extends JPanel implements KeyListener, ActionListener
{
private static final long serialVersionUID = 1L;
JFrame frmMain = new JFrame("Kodning");
JTextField text = new JTextField();
JPanel pan = new JPanel();
static char bokstav;
static int x=10, y=80;
boolean convert = false;
String s;
Timer t = new Timer(10, this);
public static void main(String[] args)
{
@SuppressWarnings("unused")
GUI g = new GUI();
}
public GUI()
{
frmMain.setSize(600, 120);
frmMain.setLayout(new GridLayout(2, 1));
frmMain.addWindowListener(hornStang());
frmMain.add(text);
frmMain.add(pan);
frmMain.setFocusable(true);
frmMain.setVisible(true);
frmMain.addKeyListener(this);
text.addKeyListener(this);
pan.addKeyListener(this);
t.start();
}
private static WindowAdapter hornStang()
{
return new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()== KeyEvent.VK_ENTER)
{
System.out.println("dechifrera");
repaint();
deshiffrera(text.getText());
}
}
public void keyReleased(KeyEvent arg0){}
public void keyTyped(KeyEvent arg0){}
public void deshiffrera(String s)
{
s = this.s;
repaint();
}
@override
public void paintComponent(Graphics g)
{
System.out.println("paint");
for(int i=0;i<s.length();i++)
{
bokstav = s.charAt(i);
switch (bokstav)
{
case 'a':nere(g); hoger(g); prick(g, 0); break;
//en massa case
default:break;
}
x=x+12;
}
}
@Override
public void actionPerformed(ActionEvent e)
{
repaint();
}
}
必须将组件添加到可见的窗口/框架/组件中,才能调用其paintComponent
。
GUI
仅作为KeyListener添加,而没有添加到JFrame中,也没有添加到以上代码中的任何其他可见组件中。 由于根本没有显示该组件,因此没有理由调用paintComponent
。
您的代码有很多问题:
您可能会错过“ System.out.println(“ paint”);“的输出。 ?
Windows下的GUI-Apps无法写入控制台(它们没有控制台,因为如果每个GUI-App也都打开黑色窗口,它将很烂)。
Windows下有两个Java解释器:“ javaw.exe”,它是一个GUI应用,它会静默丢弃任何System.out-writes。 还有“ java.exe”,它是一个控制台应用程序,允许写入控制台。 尝试使用“ java.exe”启动程序
我将其与AWT一起使用(不确定100%是否也在Swing中运行...)
Graphics g = _yourcomponent_.getGraphics();
if (g != null) {
_yourcomponent_.paint(g);
// below the estimated code for Swing:
_yourcomponent_.paintComponent(g);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.