2 carvin zh Carvin_Zh 于 2015.06.09 22:45 提问

JPanel继承问题... 写了一个类,继承自JPanel,但是控件不显示,跪求求各位大牛...

在做一个世界各个国家的时间的一个Demo。写了一个继承自JPanel类的panel,里面简单的设置了一个Logo,国家名字,还有一个按秒跳动时间,现在的问题就是在这个panel上的控件都不显示了,调了好久了也没有找到原因,所以没办法了,特来求助各位大牛们.....

话不多说,直接贴代码:

package timerDemo;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Panel;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class ClockDemo extends Panel implements Runnable{
private JLabel imageLabel;

private JLabel localeLabel;
private JLabel dataLabel;
private JPanel panel;
private Thread t;
private GregorianCalendar calendar;
public ClockDemo(String city,String time){
super();
panel=new JPanel();
imageLabel=new JLabel();
imageLabel.setLabelFor(imageLabel);
ImageIcon imageL=new ImageIcon("Z:\study\JAVA\JAVATimer\watchgif.gif");
imageL.setImage(imageL.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
imageLabel.setIcon(imageL);
imageLabel.setHorizontalAlignment(SwingConstants.CENTER);

    localeLabel=new JLabel(city,JLabel.CENTER);//城市名

    dataLabel=new JLabel();
    panel.setLayout(new GridLayout(3,1));
    panel.add(imageLabel);
    panel.add(localeLabel);
    panel.add(dataLabel);
    JFrame jf=new JFrame();
    jf.getContentPane().add(panel);
    t=new Thread(this);
    t.start();
}
public void showTime(){
    String str="";
    str=str+new Date();
    dataLabel.setText(str);
    dataLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        Thread.sleep(1000);
        showTime();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}


3个回答

tongyi55555
tongyi55555   2015.06.10 07:44
已采纳

下面是根据你的代码改的,主要就是在线程中增加了while循环,否则时间不会更新的。
此外你的JFrame写到了JPanel之中,我就直接增加了显示的代码并增加了主方法,你可以试一下。
图片的路径已经修改了,你测试的时候需要改成你自己的。

import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Panel;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class ClockDemo extends Panel implements Runnable {
    /**
     * 
     */
    private static final long serialVersionUID = -7055793302605026523L;

    private JLabel imageLabel;

    private JLabel localeLabel;
    private JLabel dataLabel;
    private JPanel panel;
    private Thread t;

    public ClockDemo(String city, String time) {
        super();
        panel = new JPanel();
        imageLabel = new JLabel();
        imageLabel.setLabelFor(imageLabel);
        // 修改了图片的路径
        ImageIcon imageL = new ImageIcon("D:\\Beijing.jpg");
        imageL.setImage(imageL.getImage().getScaledInstance(50, 50,
                Image.SCALE_DEFAULT));
        imageLabel.setIcon(imageL);
        imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
        localeLabel = new JLabel(city, JLabel.CENTER);// 城市名

        dataLabel = new JLabel();
        panel.setLayout(new GridLayout(3, 1));
        panel.add(imageLabel);
        panel.add(localeLabel);
        panel.add(dataLabel);
        JFrame jf = new JFrame();
        jf.getContentPane().add(panel);
        t = new Thread(this);
        t.start();

        // 增加的内容
        jf.setLocationRelativeTo(null);
        jf.setSize(300, 300);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void showTime() {
        String str = "";
        str = str + new Date();
        dataLabel.setText(str);
        dataLabel.setHorizontalAlignment(SwingConstants.CENTER);
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        // 没有while循环时间没有办法更新
        while (true) {
            try {
                Thread.sleep(1000);
                showTime();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    // 主方法测试
    @SuppressWarnings("unused")
    public static void main(String[] args) {
        ClockDemo clockDemo = new ClockDemo("beijing", null);
    }
}

运行的结果如下所示:

图片说明

此外,下面是我前段时间帮一个网友改的时间显示代码,你可以参考下,你上面的代码中JFrame使用的不是很好,不建议JPanel中使用JFrame,应该放到外面声明,然后将JPanel放到其中就行了。
这个样例主要是增加了绘制时钟的功能,可以显示一个简单地时间钟表,并且秒针是动的。你可以测试下。

import java.awt.Color;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Clockone extends JFrame implements Runnable {
    /**
     * 
     */
    private static final long serialVersionUID = 8754113577355207407L;
    private JPanel JP = new JPanel();
    private Color C1 = new Color(123, 154, 45);
    private SimpleDateFormat FM = new SimpleDateFormat(
            "yyyy EEE MM dd HH:mm:ss");
    private Date DT = new Date();
    private Calendar C = Calendar.getInstance();
    private int s, m, h;
    private int XC = 101, YC = 101;
    private int xs, ys, xm, ym, xh, yh;
    private JLabel JL = new JLabel();

    public Clockone(String title) {
        super(title);
        setLayout(null);
        add(JP);
        JP.setBounds(0, 0, 300, 300);
        JP.setLayout(null);
        JP.add(JL);
        JL.setBounds(50, 50, 100, 50);

    }

    public void paint(Graphics g) {
        g.clearRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(Color.BLUE);
        g.drawOval(50, 50, 100, 100);
        g.drawOval(100, 100, 5, 5);
        g.fillOval(XC, YC, 3, 3);
        g.drawString("3", 143, 105);
        g.drawString("6", 98, 150);
        g.drawString("9", 51, 105);
        g.drawString("12", 95, 62);
        g.drawString("北京时间:" + String.valueOf((FM.format(DT))), 20, 200);
        setBackground(C1);
        s = C.get(Calendar.SECOND);
        m = C.get(Calendar.MINUTE);
        h = C.get(Calendar.HOUR_OF_DAY);
        xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + XC);
        ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + YC);
        xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + XC);
        ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + YC);
        xh = (int) (Math.cos((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + XC);
        yh = (int) (Math.sin((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + YC);
        g.drawLine(xs, ys, XC, YC);
        g.drawLine(xm, ym, XC - 1, YC);
        g.drawLine(xm, ym, XC, YC - 1);
        g.drawLine(xh, yh, XC - 1, YC);
        g.drawLine(xh, yh, XC, YC - 1);
    }

    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
                C = Calendar.getInstance();
                DT = C.getTime();
            } catch (InterruptedException e) {
            }
            repaint();
        }
    }

    public static void main(String[] args) {

        Clockone CO = new Clockone("Clock");
        CO.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CO.setLocationRelativeTo(null);
        CO.setSize(300, 300);
        CO.setVisible(true);
        Thread thread = new Thread(CO);
        thread.start();
        thread.setPriority(MAXIMIZED_BOTH);
    }
}
Carvin_Zh
Carvin_Zh 问题出在这里,因为由于本身这个类就是继承JPanel类,结果在类里面又实例化一个JPanel,而且将东西就添加进去那里面,这样就把本身的类给覆盖掉!今天研究了好久才发现这个问题!灰常感谢,给了那么多建议和例子!感谢!
2 年多之前 回复
Loweir
Loweir   2015.06.09 23:11
 package test;

import guiTest.Id;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Panel;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class ClockDemo extends Panel implements Runnable {
    private JLabel imageLabel;
    private JLabel localeLabel;
    private JLabel dataLabel;
    private JPanel panel;
    private Thread t;
    private GregorianCalendar calendar;

    public ClockDemo(String city, String time) {
        super();
        panel = new JPanel();
        imageLabel = new JLabel();
        imageLabel.setLabelFor(imageLabel);
        ImageIcon imageL = new ImageIcon("F:/Photo/QQ图片20141013122213.gif");
        imageL.setImage(imageL.getImage().getScaledInstance(50, 50,
                Image.SCALE_DEFAULT));
        imageLabel.setIcon(imageL);
        imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
        localeLabel = new JLabel(city, JLabel.CENTER);// 城市名

        dataLabel = new JLabel();
        panel.setLayout(new GridLayout(3, 1));
        panel.add(imageLabel);
        panel.add(localeLabel);
        panel.add(dataLabel);
        JFrame jf = new JFrame();
        jf.getContentPane().add(panel);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        t = new Thread(this);
        t.start();
    }

    public void showTime() {
        String str = "";
        str = str + new Date();
        dataLabel.setText(str);
        dataLabel.setHorizontalAlignment(SwingConstants.CENTER);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClockDemo("china","12").setVisible(true);
            }
        });
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            Thread.sleep(1000);
            showTime();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

如果不对 可以联系我

Carvin_Zh
Carvin_Zh 问题找到了,本身写的就是继承JPanel类,结果在类里面又实例化一个JPanel,这样就会把本身的类给覆盖掉,今天研究了好久才发现这个问题!非常感谢,很好很强大!
2 年多之前 回复
songhtao
songhtao   Rxr 2015.06.11 13:22

问题解决了就该结贴了啊

Csdn user default icon
上传中...
上传图片
插入图片