十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
方法中出现横线,是因为该方法已经不建议被使用,应该有替代的方案
成都创新互联公司是一家专注于网站设计制作、成都网站制作与策划设计,三原网站建设哪家好?成都创新互联公司做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:三原等地区。三原做网站价格咨询:18982081108
你说的show()方法是不是JFrame中的show()方法呢,可以用setVisible(true)这个方法来替代
祝你好运!
java方法名上出现横线代表该方法已经过时!所以只要使用一个过时的方法,该方法名上就能出来横线。最简单的就是使用Data对象的toLocaleString()方法,即new Data().toLocaleString()获取本地格式的时间,这个方法已经被标记为过时方法!
可以定义全局的变量来保存Graphics。
在组件处于可见的状态下使用getGraphics。代码如下:
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class J extends JFrame implements KeyListener{
Graphics g=null;//全局变量
JPanel jp=new JPanel();
public J()
{
add(jp);
setSize(100, 100);
setVisible(true);//必须放在getGraphics之前
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g=jp.getGraphics();
if(g==null)
{
System.out.println("NULL");
}
this.addKeyListener(this);
}
static public void main(String []agr){
new J();
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("draw");
g.drawLine(0, 0, 50, 50);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
那些是被@Deprecated注解标记的方法或者属性或类等。
意思是“已过时”。
如果你是新写代码,那么不推荐你这么做,有更好的替代方案,如果是老系统,那么告知你你这个方法已过时,不过JDK还将继续对他支持。
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MouseDrawPanel extends Frame
{
private static final long serialVersionUID = 1L;
MouseEvent e = null;
int startX = -1;
int startY = -1;
int endX = -1;
int endY = -1;
boolean left = false;
public MouseDrawPanel ( String title )
{
setTitle (title);
setLayout (new BorderLayout ());
setResizable (false);
setSize (500, 400);
setLocationRelativeTo (null);
addWindowListener (new WindowAdapter ()
{
@Override
public void windowClosing ( WindowEvent e )
{
System.exit (0);
}
});
}
@Override
public void paintComponents ( Graphics g )
{
float lineWidth = 8.0f;
( (Graphics2D) g ).setStroke (new BasicStroke (lineWidth));
g.setColor (Color.YELLOW);
g.drawLine (startX, startY, endX, endY);
g.dispose ();
}
public static void main ( String[] args )
{
final MouseDrawPanel mdp = new MouseDrawPanel ("Use Mouse Draw");
Panel panel = new Panel ();
panel.setLayout (new FlowLayout (FlowLayout.LEFT));
Label startL = new Label ("start: ");
Label endL = new Label ("end: ");
final Label nowL = new Label ("now: ");
final Label startR = new Label ("000,000");
final Label endR = new Label ("000,000");
final Label nowN = new Label ("000,000");
panel.add (startL);
panel.add (startR);
panel.add (endL);
panel.add (endR);
panel.add (nowL);
panel.add (nowN);
mdp.add (panel, "South");
mdp.addMouseMotionListener (new MouseMotionListener ()
{
@Override
public void mouseMoved ( MouseEvent e )
{
if (mdp.left)
{
nowN.setText (e.getX () + " , " + e.getY ());
}
}
@Override
public void mouseDragged ( MouseEvent e )
{
if (mdp.left)
{
mdp.endX = e.getX ();
mdp.endY = e.getY ();
mdp.paintComponents (mdp.getGraphics ());
mdp.startX = mdp.endX;
mdp.startY = mdp.endY;
endR.setText (mdp.endX + " , " + mdp.endY);
}
}
});
mdp.addMouseListener (new MouseAdapter ()
{
@Override
public void mousePressed ( MouseEvent e )
{
if (e.getButton () == MouseEvent.BUTTON1)
{
mdp.startX = e.getX ();
mdp.startY = e.getY ();
startR.setText (mdp.startX + " , " + mdp.startY);
mdp.left = true;
}
else
{
mdp.left = false;
}
}
@Override
public void mouseReleased ( MouseEvent e )
{
if (mdp.left)
{
endR.setText (e.getX () + " , " + e.getY ());
}
}
});
mdp.setVisible (true);
}
}
得覆盖 paint 方法,在paint 方法中画。。。。。。。。。。
或者,先在BufferedImage中画好,再paint上去 。。。。。。。。