尝试显示计时器计数,drawString 方法无法正常工作



我正在尝试显示计时器计数,但它没有显示,但其他一切正常。顺便说一句,感谢您的帮助。

import javax.swing.*;
import java.util.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Tester {
static Timer timer;
static JFrame frame;
static JPanel panel;
public static void init(){
    frame = new JFrame();
    panel = new JPanel();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(500, 500);
    frame.add(panel);

}

上面的方法只是为了使代码更清晰。

public static void main(String[] args) {
init();

    class Clicker extends JPanel{
        int timesClicked;
        public Clicker(){
            timesClicked = 0;
        }
        void updateClicks(){
            timesClicked++;
            repaint();
        }
         public void paintComponent(Graphics g){
             super.paintComponent(g);
             Graphics2D g2 = (Graphics2D) g;
            System.out.println("Called!!!");
            g2.drawString("Half Seconds: "+timesClicked, 100, 100);
        }
    } //end of Clicker

上面是 drawString 方法不起作用。
最终答题器 c = 新答题器(); panel.add(c);

class TimeChecker implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Clicked!");
            c.updateClicks();
        }
    }// end of TimeChecker
    ActionListener listener = new TimeChecker();
    timer = new Timer(500,listener);
    timer.start();

}
}

您有一系列问题

  • Clicker从未真正添加到任何东西中
  • 默认情况下,panel使用FlowLayout,但Clicker不提供大小调整提示,因此其大小为 0x0
  • 您的代码通常设置得有点奇怪。我会很快学会没有static

快速快速的解决方案...

panel的布局管理器更改为BorderLayout...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Test {
    static Timer timer;
    static JFrame frame;
    static JPanel panel;
    public static void init() {
        frame = new JFrame();
        panel = new JPanel(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(500, 500);
        frame.add(panel);
    }
    public static void main(String[] args) {
        init();
        class Clicker extends JPanel {
            int timesClicked;
            public Clicker() {
                timesClicked = 0;
            }
            void updateClicks() {
                timesClicked++;
                repaint();
            }
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                System.out.println("Called!!!");
                g2.setColor(Color.BLACK);
                g2.drawString("Half Seconds: " + timesClicked, 100, 100);
            }
        } //end of Clicker
        final Clicker c = new Clicker();
        panel.add(c);
        panel.revalidate();
        panel.repaint();
        class TimeChecker implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Clicked!");
                c.updateClicks();
            }
        }// end of TimeChecker
        ActionListener listener = new TimeChecker();
        timer = new Timer(500, listener);
        timer.start();
    }
}

略有不同的方法(无静态)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
    public static void main(String[] args) {
        new Test();
    }
    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                Clicker clicker = new Clicker();
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(clicker);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                class TimeChecker implements ActionListener {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Clicked!");
                        clicker.updateClicks();
                    }
                }// end of TimeChecker
                ActionListener listener = new TimeChecker();
                Timer timer = new Timer(500, listener);
                timer.start();
            }
        });
    }
    public class Clicker extends JPanel {
        private int timesClicked;
        public Clicker() {
            timesClicked = 0;
        }
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 200);
        }
        void updateClicks() {
            timesClicked++;
            repaint();
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            System.out.println("Called!!!");
            g2.setColor(Color.BLACK);
            g2.drawString("Half Seconds: " + timesClicked, 100, 100);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新