当前位置: 技术问答>java相关
谁来看看我的这个程序,不该出错啊!(
来源: 互联网 发布时间:2014-12-28
本文导语: import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class aa extends Applet implements ActionListener,KeyListener { String text=""; int x=0; int y=0; Button boldbutton,italicbutton,largebutton,romanbutton,courierbutton,clearbutton; boolean bBol...
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class aa extends Applet implements ActionListener,KeyListener {
String text="";
int x=0;
int y=0;
Button boldbutton,italicbutton,largebutton,romanbutton,courierbutton,clearbutton;
boolean bBoldFlag=false;
boolean bItalicFlag=false;
boolean bLargeFlag=false;
boolean bRomanFlag=true;
boolean bCourierFlag=false;
FontMetrics fontmetrics;
public void init()
{
boldbutton=new Button("bold font");
boldbutton.addActionListener(this);
italicbutton=new Button("italic font");
italicbutton.addActionListener(this);
largebutton=new Button("large font");
largebutton.addActionListener(this);
romanbutton=new Button("roman font");
romanbutton.addActionListener(this);
courierbutton=new Button("courier font");
courierbutton.addActionListener(this);
clearbutton=new Button("clear font");
clearbutton.addActionListener(this);
add(boldbutton);
add(italicbutton);
add(largebutton);
add(romanbutton);
add(courierbutton);
add(clearbutton);
addKeyListener(this);
requestFocus();
}
public void actionPerformed(ActionEvent event){
if (event.getSource()==boldbutton)
{bBoldFlag=!bBoldFlag;
requestFocus();
}
if (event.getSource()==italicbutton)
{bItalicFlag=!bItalicFlag;
requestFocus();
}
if (event.getSource()==largebutton)
{bLargeFlag=!bLargeFlag;
requestFocus();
}
if (event.getSource()==romanbutton)
{bRomanFlag=true;
bCourierFlag=false;
requestFocus();
}
if (event.getSource()==courierbutton)
{bCourierFlag=true;
bRomanFlag=false;
requestFocus();
}
if (event.getSource()==clearbutton)
{text="";
requestFocus();
}
repaint();
}
public void paint(Graphics g)
{
String fontname="Roman";
int type=Font.PLAIN;
int size=24;
Font font;
if (bBoldFlag)
{type=type|Font.BOLD;
}
if (bItalicFlag)
{type=type|Font.ITALIC;
}
if (bLargeFlag)
{size=48;
}
if (bRomanFlag)
{fontname="Roman";
}
if (bCourierFlag)
{fontname="Courier";
}
font=new Font(fontname,type,size);
g.setFont(font);
fontmetrics=getFontMetrics(font);
x=(size().width-fontmetrics.stringWidth(text))/2;
y=(size().height+fontmetrics.getHeight())/2;
g.drawString(text,x,y);
}
public void keyTyped(KeyEvent e){
text=text+e.getKeyChar();
repaint();
}
public void keypressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
顺便问一下,出错后怎么用deprecation重新编译?
import java.awt.*;
import java.awt.event.*;
public class aa extends Applet implements ActionListener,KeyListener {
String text="";
int x=0;
int y=0;
Button boldbutton,italicbutton,largebutton,romanbutton,courierbutton,clearbutton;
boolean bBoldFlag=false;
boolean bItalicFlag=false;
boolean bLargeFlag=false;
boolean bRomanFlag=true;
boolean bCourierFlag=false;
FontMetrics fontmetrics;
public void init()
{
boldbutton=new Button("bold font");
boldbutton.addActionListener(this);
italicbutton=new Button("italic font");
italicbutton.addActionListener(this);
largebutton=new Button("large font");
largebutton.addActionListener(this);
romanbutton=new Button("roman font");
romanbutton.addActionListener(this);
courierbutton=new Button("courier font");
courierbutton.addActionListener(this);
clearbutton=new Button("clear font");
clearbutton.addActionListener(this);
add(boldbutton);
add(italicbutton);
add(largebutton);
add(romanbutton);
add(courierbutton);
add(clearbutton);
addKeyListener(this);
requestFocus();
}
public void actionPerformed(ActionEvent event){
if (event.getSource()==boldbutton)
{bBoldFlag=!bBoldFlag;
requestFocus();
}
if (event.getSource()==italicbutton)
{bItalicFlag=!bItalicFlag;
requestFocus();
}
if (event.getSource()==largebutton)
{bLargeFlag=!bLargeFlag;
requestFocus();
}
if (event.getSource()==romanbutton)
{bRomanFlag=true;
bCourierFlag=false;
requestFocus();
}
if (event.getSource()==courierbutton)
{bCourierFlag=true;
bRomanFlag=false;
requestFocus();
}
if (event.getSource()==clearbutton)
{text="";
requestFocus();
}
repaint();
}
public void paint(Graphics g)
{
String fontname="Roman";
int type=Font.PLAIN;
int size=24;
Font font;
if (bBoldFlag)
{type=type|Font.BOLD;
}
if (bItalicFlag)
{type=type|Font.ITALIC;
}
if (bLargeFlag)
{size=48;
}
if (bRomanFlag)
{fontname="Roman";
}
if (bCourierFlag)
{fontname="Courier";
}
font=new Font(fontname,type,size);
g.setFont(font);
fontmetrics=getFontMetrics(font);
x=(size().width-fontmetrics.stringWidth(text))/2;
y=(size().height+fontmetrics.getHeight())/2;
g.drawString(text,x,y);
}
public void keyTyped(KeyEvent e){
text=text+e.getKeyChar();
repaint();
}
public void keypressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
顺便问一下,出错后怎么用deprecation重新编译?
|
//试试这个吧.
//你的public void keypressed(KeyEvent e){};
//应改为 public void keyPressed(KeyEvent e){};
//这样就不会出错了.
//aa.java 使用或覆盖一个不鼓励使用的API。但是这不会影响程序执行的.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class aa extends Applet implements ActionListener,KeyListener {
String text="";
int x=0;
int y=0;
Button boldbutton,italicbutton,largebutton,romanbutton,courierbutton,clearbutton;
boolean bBoldFlag=false;
boolean bItalicFlag=false;
boolean bLargeFlag=false;
boolean bRomanFlag=true;
boolean bCourierFlag=false;
FontMetrics fontmetrics;
public void init() {
boldbutton=new Button("bold font");
boldbutton.addActionListener(this);
italicbutton=new Button("italic font");
italicbutton.addActionListener(this);
largebutton=new Button("large font");
largebutton.addActionListener(this);
romanbutton=new Button("roman font");
romanbutton.addActionListener(this);
courierbutton=new Button("courier font");
courierbutton.addActionListener(this);
clearbutton=new Button("clear font");
clearbutton.addActionListener(this);
add(boldbutton);
add(italicbutton);
add(largebutton);
add(romanbutton);
add(courierbutton);
add(clearbutton);
addKeyListener(this);
requestFocus();
}
public void actionPerformed(ActionEvent event){
if (event.getSource()==boldbutton){
bBoldFlag=!bBoldFlag;
requestFocus();
}
if (event.getSource()==italicbutton){
bItalicFlag=!bItalicFlag;
requestFocus();
}
if (event.getSource()==largebutton){
bLargeFlag=!bLargeFlag;
requestFocus();
}
if (event.getSource()==romanbutton){
bRomanFlag=true;
bCourierFlag=false;
requestFocus();
}
if (event.getSource()==courierbutton){
bCourierFlag=true;
bRomanFlag=false;
requestFocus();
}
if (event.getSource()==clearbutton){
text="";
requestFocus();
}
repaint();
}
public void paint(Graphics g){
String fontname="Roman";
int type=Font.PLAIN;
int size=24;
Font font;
if (bBoldFlag){
type=type|Font.BOLD;
}
if (bItalicFlag){
type=type|Font.ITALIC;
}
if (bLargeFlag){
size=48;
}
if (bRomanFlag){
fontname="Roman";
}
if (bCourierFlag){
fontname="Courier";
}
font=new Font(fontname,type,size);
g.setFont(font);
fontmetrics=getFontMetrics(font);
x=(size().width-fontmetrics.stringWidth(text))/2;
y=(size().height+fontmetrics.getHeight())/2;
g.drawString(text,100,100);
}
public void keyTyped(KeyEvent e){
text=text+e.getKeyChar();
repaint();
}
public void keyPressed(KeyEvent e){}//注意别在出错了呀.
public void keyReleased(KeyEvent e){}
}
//你的public void keypressed(KeyEvent e){};
//应改为 public void keyPressed(KeyEvent e){};
//这样就不会出错了.
//aa.java 使用或覆盖一个不鼓励使用的API。但是这不会影响程序执行的.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class aa extends Applet implements ActionListener,KeyListener {
String text="";
int x=0;
int y=0;
Button boldbutton,italicbutton,largebutton,romanbutton,courierbutton,clearbutton;
boolean bBoldFlag=false;
boolean bItalicFlag=false;
boolean bLargeFlag=false;
boolean bRomanFlag=true;
boolean bCourierFlag=false;
FontMetrics fontmetrics;
public void init() {
boldbutton=new Button("bold font");
boldbutton.addActionListener(this);
italicbutton=new Button("italic font");
italicbutton.addActionListener(this);
largebutton=new Button("large font");
largebutton.addActionListener(this);
romanbutton=new Button("roman font");
romanbutton.addActionListener(this);
courierbutton=new Button("courier font");
courierbutton.addActionListener(this);
clearbutton=new Button("clear font");
clearbutton.addActionListener(this);
add(boldbutton);
add(italicbutton);
add(largebutton);
add(romanbutton);
add(courierbutton);
add(clearbutton);
addKeyListener(this);
requestFocus();
}
public void actionPerformed(ActionEvent event){
if (event.getSource()==boldbutton){
bBoldFlag=!bBoldFlag;
requestFocus();
}
if (event.getSource()==italicbutton){
bItalicFlag=!bItalicFlag;
requestFocus();
}
if (event.getSource()==largebutton){
bLargeFlag=!bLargeFlag;
requestFocus();
}
if (event.getSource()==romanbutton){
bRomanFlag=true;
bCourierFlag=false;
requestFocus();
}
if (event.getSource()==courierbutton){
bCourierFlag=true;
bRomanFlag=false;
requestFocus();
}
if (event.getSource()==clearbutton){
text="";
requestFocus();
}
repaint();
}
public void paint(Graphics g){
String fontname="Roman";
int type=Font.PLAIN;
int size=24;
Font font;
if (bBoldFlag){
type=type|Font.BOLD;
}
if (bItalicFlag){
type=type|Font.ITALIC;
}
if (bLargeFlag){
size=48;
}
if (bRomanFlag){
fontname="Roman";
}
if (bCourierFlag){
fontname="Courier";
}
font=new Font(fontname,type,size);
g.setFont(font);
fontmetrics=getFontMetrics(font);
x=(size().width-fontmetrics.stringWidth(text))/2;
y=(size().height+fontmetrics.getHeight())/2;
g.drawString(text,100,100);
}
public void keyTyped(KeyEvent e){
text=text+e.getKeyChar();
repaint();
}
public void keyPressed(KeyEvent e){}//注意别在出错了呀.
public void keyReleased(KeyEvent e){}
}
|
出错信息应该只是warning,不用理会,可以运行
之所以有warning,应该是因为你使用了旧的api, 现在不提倡使用了。
之所以有warning,应该是因为你使用了旧的api, 现在不提倡使用了。