gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:17:17 25 4
gpt4 key购买 nike

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

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方法不起作用。
最终 Clicker c = new Clicker(); 面板.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);

}

}

}

关于java - 尝试显示计时器计数,drawString 方法无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35822077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com