[英]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.