从 Eclipse 运行应用程序时未调用 repaint()?

发布于 2024-09-15 03:18:19 字数 2005 浏览 10 评论 0原文

当我使用 Eclipse 和 Run->cmd 运行程序时,我从未将“paint”写入命令行窗口。如果我在另一个程序中从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();
   }
 }

I never get "paint" written to my command line window when I use Eclipse and Run->cmd to run the program. It works fine if I run System.out.print() from paintComponent in another program. Someone who can help?

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();
   }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云胡 2024-09-22 03:18:20

我将它与 AWT 一起使用(不能 100% 确定它是否也可以在 Swing 中工作......)

Graphics g = _yourcomponent_.getGraphics();
if (g != null) {
    _yourcomponent_.paint(g);
    // below the estimated code for Swing:
    _yourcomponent_.paintComponent(g);
}

I use this with AWT (not 100% sure whether it's working in Swing too...)

Graphics g = _yourcomponent_.getGraphics();
if (g != null) {
    _yourcomponent_.paint(g);
    // below the estimated code for Swing:
    _yourcomponent_.paintComponent(g);
}
做个少女永远怀春 2024-09-22 03:18:19

该组件必须添加到可见窗口/框架/组件中才能调用它的 paintComponent
GUI 仅作为 KeyListener 添加,但既不添加到 JFrame,也不添加到上面代码中的任何其他可见组件。没有理由调用 paintComponent 因为该组件根本没有显示。

The component must be added to a visible window/frame/component for it's paintComponent to be called.
GUI is only added as a KeyListener but is neither added to the JFrame, nor any other visible component in the code above. There is no reason for calling paintComponent since the component is not being displayed at all.

一梦浮鱼 2024-09-22 03:18:19

您的代码存在许多问题:

  1. 您的 GUI 面板不在框架中(不应该添加它而不是平移吗?)
  2. String s 未初始化,这会导致 NullPointerException
  3. 绘制应被覆盖而不是 PaintComponents
  4. 绘制不应更改组件的状态,因为它可以随时调用。
  5. ETC...

There are a number of issues with your code:

  1. Your GUI panel is not in the frame (shouldn't it be added instead of pan?)
  2. String s is uninitialized, which causes a NullPointerException
  3. paint should be overridden instead of paintComponents
  4. paint should not change the state of the component, because it can be called any time.
  5. etc...
晨光如昨 2024-09-22 03:18:19

您可能会错过“System.out.println(“paint”);”的输出?

Windows 下的 GUI 应用程序无法写入控制台(它们没有控制台,因为如果每个 GUI 应用程序也打开一个黑色窗口,那就太糟糕了)。

Windows 下有两个 java 解释器:“javaw.exe”,它是一个 GUI 应用程序,会默默地丢弃任何 System.out-writes。 “java.exe”是一个控制台应用程序,允许写入控制台。尝试使用“java.exe”启动您的程序

You probably miss the output of "System.out.println("paint");" ?

GUI-Apps under Windows cant write to the console (they dont have a console, because it would suck if every GUI-App would also open a black window).

There are two java-interpreters under windows: "javaw.exe" which is a GUI-App and silently discards any System.out-writes. And "java.exe" which is a console-app and allows writing to the console. Try to start your program with "java.exe"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文