使用JPanel重绘帧

时间:2015-07-08 12:40:47

标签: java swing jframe jpanel java-2d

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

2 个答案:

答案 0 :(得分:2)

实际上,您重绘了JPanel。

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

Simple Clock GUI

首先创建GUI,然后使用Thread或Swing Timer中的Runnable定期更新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;
        }

    }

}

答案 1 :(得分:1)

您需要将extened JPanel表面添加到JFrame

public Display() {
    Surface surface = new Surface();
    add(surface);
    setTitle("salami");
    setSize(400, 300);
}

如果这不适用于SSCCE