我想用Graphics在窗口上画图形 但是重载paint函数的话 这个窗口只能是继承JFrame类。如果不想用继承 怎么才能画图形呢?
代码如下:import java.awt.*;
import javax.swing.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class test extends JFrame{
//private JFrame frame;
private JLabel date_display;
private JLabel name_display;
private JLabel map_display; public test() {
//frame = new JFrame("test");
Container c = this.getContentPane();
c.setLayout(null);
DateFormat format = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
String str_Date = format.format(date);
date_display = new JLabel();
date_display.setBounds(30,20,400,30);
date_display.setText("Today's Date is: " + str_Date);
date_display.setFont(new Font("", Font.PLAIN, 25));
date_display.setForeground(Color.red);
c.add(date_display);
name_display = new JLabel();
name_display.setBounds(140,90,200,30);
name_display.setText("Time Zone");
name_display.setFont(new Font("", Font.PLAIN, 25));
name_display.setForeground(Color.red);
c.add(name_display);
ImageIcon image = new ImageIcon("map.gif");
map_display = new JLabel(image);
map_display.setBounds(10,130,image.getIconWidth(),image.getIconHeight());
c.add(map_display);
this.setSize(390,370);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
super.paint(g);
g.drawLine(0,100,390,100);
}
public static void main(String args[])
{
new test();
}
}
还有一个问题,那个super.paint(g); 是干什么用的,为什么去掉以后就只显示一条线 其他组件不显示?
代码如下:import java.awt.*;
import javax.swing.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class test extends JFrame{
//private JFrame frame;
private JLabel date_display;
private JLabel name_display;
private JLabel map_display; public test() {
//frame = new JFrame("test");
Container c = this.getContentPane();
c.setLayout(null);
DateFormat format = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
String str_Date = format.format(date);
date_display = new JLabel();
date_display.setBounds(30,20,400,30);
date_display.setText("Today's Date is: " + str_Date);
date_display.setFont(new Font("", Font.PLAIN, 25));
date_display.setForeground(Color.red);
c.add(date_display);
name_display = new JLabel();
name_display.setBounds(140,90,200,30);
name_display.setText("Time Zone");
name_display.setFont(new Font("", Font.PLAIN, 25));
name_display.setForeground(Color.red);
c.add(name_display);
ImageIcon image = new ImageIcon("map.gif");
map_display = new JLabel(image);
map_display.setBounds(10,130,image.getIconWidth(),image.getIconHeight());
c.add(map_display);
this.setSize(390,370);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
super.paint(g);
g.drawLine(0,100,390,100);
}
public static void main(String args[])
{
new test();
}
}
还有一个问题,那个super.paint(g); 是干什么用的,为什么去掉以后就只显示一条线 其他组件不显示?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货