gpt4 book ai didi

java - 使用 JPanel 重绘框架

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

我是java初学者;我最近从另一种编程语言转向,我只了解基础知识。
我在使用 JPanel 重绘时遇到问题。当我创建新对象时,我可以绘制文本,但在重新绘制它时遇到问题。函数 tick() 由 Main 类在短时间内调用,并且应该重绘 JPanel。

package main;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Surface extends JPanel{
private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g;
g2d.drawString("Hello World! "+Main.integer, 50, 50);
}

@Override
public void paintComponent(Graphics g) {

super.paintComponent(g);
doDrawing(g);
}
}
class Display extends JFrame {

public Display() {
Surface surface = new Surface();
setTitle("salami");
setSize(400, 300);
}
public void tick() {
surface.redraw();
}
}

最佳答案

实际上,您重绘了 JPanel。

这是我重新绘制的 JPanel 的最简单示例。这是一个每 200 毫秒重新绘制一次的时钟。

Simple Clock GUI

首先创建 GUI,然后使用线程中的 Runnable 或 Swing Timer 定期更新 GUI。

这是代码。

package com.ggl.testing;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class SimpleClock implements Runnable {

private JFrame frame;

private JPanel panel;

private JTextField clockDisplay;

private Timer timer;

@Override
public void run() {
frame = new JFrame("Clock");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent event) {
exitProcedure();
}
});

createClockPanel();

frame.add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

timer = new Timer(this);
new Thread(timer).start();
}

private void createClockPanel() {
panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 6));

clockDisplay = new JTextField(12);
clockDisplay.setEditable(false);
clockDisplay.setHorizontalAlignment(JTextField.CENTER);

panel.add(clockDisplay);
}

public void exitProcedure() {
timer.setRunning(false);
frame.dispose();
System.exit(0);
}

public void setText(String text) {
clockDisplay.setText(text);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new SimpleClock());
}

public class Timer implements Runnable {

private volatile boolean running;

private SimpleClock clock;

private SimpleDateFormat timeFormat;

public Timer(SimpleClock clock) {
this.clock = clock;
this.running = true;
this.timeFormat = new SimpleDateFormat("h:mm:ss a");
}

@Override
public void run() {
while (running) {
displayTime();
sleep();
}

}

public void displayTime() {
final Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
final String s = timeFormat.format(date);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
clock.setText(s);
}
});
}

public void sleep() {
try {
Thread.sleep(200L);
} catch (InterruptedException e) {
}
}

public synchronized void setRunning(boolean running) {
this.running = running;
}

}

}

关于java - 使用 JPanel 重绘框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31293011/

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