java drawstring输出字符数组时为什么成了数字,
还有我这个代码能不能帮忙改进一下,新手自学,请多关照packagecom.life;importjava.applet.Applet;importjava.awt.But...
还有我这个代码能不能帮忙改进一下,新手自学,请多关照
package com.life;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
public class PaiXv extends Applet implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -5347316269588713361L;
String s;
int m;
Label prompt=new Label("请输入要查找的数");
Button go=new Button("确定");
TextField putin=new TextField(5);
Label state=new Label("待输入");
Label result=new Label("fdf ") ;
static char[] abc= {'a','b','c','e','f','g','s','d','g','l'};
public void init()
{
add (prompt);
add (go);
add (putin);
add(state);
add(result);
go.addActionListener(this);
putin.addActionListener(this);
}
public void paint(Graphics g)
{
for(int i=0;i<abc.length;i++)
{
g.drawString(Integer.toString(abc[i]),10+30*i,70);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==putin)
{
s=putin.getText();
state.setText("已输入");
}
if(e.getActionCommand()=="确定")
{
Arrays.sort(abc);
char n[]=s.toCharArray();
m=Arrays.binarySearch(abc,n[0]) ;
int y=m+1;
System.out.println(y);
result.setText ("要查找的数据在第"+y+"个位置");
}
}
public static void main(String args[])
{
}
} 展开
package com.life;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
public class PaiXv extends Applet implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -5347316269588713361L;
String s;
int m;
Label prompt=new Label("请输入要查找的数");
Button go=new Button("确定");
TextField putin=new TextField(5);
Label state=new Label("待输入");
Label result=new Label("fdf ") ;
static char[] abc= {'a','b','c','e','f','g','s','d','g','l'};
public void init()
{
add (prompt);
add (go);
add (putin);
add(state);
add(result);
go.addActionListener(this);
putin.addActionListener(this);
}
public void paint(Graphics g)
{
for(int i=0;i<abc.length;i++)
{
g.drawString(Integer.toString(abc[i]),10+30*i,70);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==putin)
{
s=putin.getText();
state.setText("已输入");
}
if(e.getActionCommand()=="确定")
{
Arrays.sort(abc);
char n[]=s.toCharArray();
m=Arrays.binarySearch(abc,n[0]) ;
int y=m+1;
System.out.println(y);
result.setText ("要查找的数据在第"+y+"个位置");
}
}
public static void main(String args[])
{
}
} 展开
3个回答
展开全部
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PaiXv extends Applet implements ActionListener
{
private static final long serialVersionUID = 1L;
String s;
int m;
Label prompt = new Label("请输入要查找的数");
Button go = new Button("确定");
TextField putin = new TextField(5);
String result = "";
String abc = "abcefgsdgl";
public void init()
{
add(prompt);
add(go);
add(putin);
go.addActionListener(this);
putin.addActionListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
g.drawString(abc, 10, 70);
g.drawString(result, 10, 90);
g.dispose();
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == go)
{
result = "要查找的数据在第" + abc.indexOf(putin.getText()) + "个位置";
repaint();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询