当前位置: 技术问答>java相关
问个问题?
来源: 互联网 发布时间:2015-05-05
本文导语: JAVA的普通GUI编程,代码如下: import java.applet.*; import java.awt.*; public class ScrollbarTest extends Applet { double d1,d2,d3; Label label1,label2,label3; TextField edit1,edit2,edit3; Button button1,button2,button3,button4,butt...
JAVA的普通GUI编程,代码如下:
import java.applet.*;
import java.awt.*;
public class ScrollbarTest extends Applet
{
double d1,d2,d3;
Label label1,label2,label3;
TextField edit1,edit2,edit3;
Button button1,button2,button3,button4,button5;
public void init()
{
resize(300, 300);
setLayout(null);
addNotify();
resize(getInsets().left + getInsets().right + 467, getInsets().top +
getInsets().bottom + 238);
label1 = new Label("Number1:");
label1.setFont(new Font("Helvetica", Font.BOLD|Font.ITALIC,14));
add(label1);
label1.setBounds(getInsets().left + 28, getInsets().top+32, 82, 21);
label2 = new Label("Number2:");
label2.setFont(new Font("Helvetica", Font.BOLD|Font.ITALIC, 14));
add(label2);
label2.setBounds(getInsets().left + 28, getInsets().top + 78,84,83);
label3 = new Label("Result:");
label3.setFont(new Font("Helvetica", Font.BOLD|Font.ITALIC,14));
add(label3);
label3.setBounds(getInsets().left + 28, getInsets().top + 126, 84,23);
edit1 = new TextField(12);
add(edit1);
edit1.setBounds(getInsets().left + 119, getInsets().top + 30, 105, 22);
edit2 = new TextField(12);
add(edit2);
edit2.setBounds(getInsets().left + 119, getInsets().top + 75, 105, 22);
edit3 = new TextField(12);
add(edit3);
edit3.setBounds(getInsets().left + 119, getInsets().top +120, 105, 22);
button1 = new Button("Add");
button1.setFont(new Font("Helvetica", Font.PLAIN, 14));
add(button1);
button1.setBounds(getInsets().left + 315, getInsets().top + 15, 70, 22);
button2 = new Button("Sub");
add(button2);
button2.setBounds(getInsets().left + 315, getInsets().top + 55, 70, 22);
button3 = new Button("Mul");
add(button3);
button3.setBounds(getInsets().left + 315, getInsets().top + 95, 70, 22);
button4 = new Button("Div");
add(button4);
button4.setBounds(getInsets().left + 315, getInsets().top + 135, 70, 22);
button5 = new Button("Exit");
button5.setFont(new Font("Helvetica", Font.BOLD, 16));
add(button5);
button5.setBounds(getInsets().left + 173, getInsets().top + 168, 100, 22);
}
public boolean handleEvent(Event event)
{
if(event.id == Event.ACTION_EVENT && event.target == button5)
{
clickedButton5();
return true;
}
else
if(event.id == Event.ACTION_EVENT && event.target == button4)
{
clickedButton4();
return true;
}
else
if(event.id == Event.ACTION_EVENT && event.target == button3)
{
clickedButton3();
return true;
}
else
if(event.id == Event.ACTION_EVENT && event.target == button2)
{
clickedButton2();
return true;
}
else
if(event.id == Event.ACTION_EVENT && event.target == button1)
{
clickedButton1();
return true;
}
return super.handleEvent(event);
}
public void clickedButton1()
{
d1 = Double.valueOf(edit1.getText()).doubleValue();
d2 = Double.valueOf(edit2.getText()).doubleValue();
d3 = d1 + d2;
edit3.setText("" + d3);
}
public void clickedButton2()
{
d1 = Double.valueOf(edit1.getText()).doubleValue();
d2 = Double.valueOf(edit2.getText()).doubleValue();
d3 = d1 - d2;
edit3.setText("" + d3);
}
public void clickedButton3()
{
d1 = Double.valueOf(edit1.getText()).doubleValue();
d2 = Double.valueOf(edit2.getText()).doubleValue();
d3 = d1 * d2;
edit3.setText("" + d3);
}
public void clickedButton4()
{
d1 = Double.valueOf(edit1.getText()).doubleValue();
d2 = Double.valueOf(edit2.getText()).doubleValue();
d3 = d1 / d2;
edit3.setText("" + d3);
}
public void clickedButton5()
{
// to do: put event handler code here.
}
}
编译能通过。
但是用appletviewer看执行效果时有如下错误:
C:>appletviewer ScrollbarTest.class
进行读取时出现 I/O 异常: null
请问该如何解决?
import java.applet.*;
import java.awt.*;
public class ScrollbarTest extends Applet
{
double d1,d2,d3;
Label label1,label2,label3;
TextField edit1,edit2,edit3;
Button button1,button2,button3,button4,button5;
public void init()
{
resize(300, 300);
setLayout(null);
addNotify();
resize(getInsets().left + getInsets().right + 467, getInsets().top +
getInsets().bottom + 238);
label1 = new Label("Number1:");
label1.setFont(new Font("Helvetica", Font.BOLD|Font.ITALIC,14));
add(label1);
label1.setBounds(getInsets().left + 28, getInsets().top+32, 82, 21);
label2 = new Label("Number2:");
label2.setFont(new Font("Helvetica", Font.BOLD|Font.ITALIC, 14));
add(label2);
label2.setBounds(getInsets().left + 28, getInsets().top + 78,84,83);
label3 = new Label("Result:");
label3.setFont(new Font("Helvetica", Font.BOLD|Font.ITALIC,14));
add(label3);
label3.setBounds(getInsets().left + 28, getInsets().top + 126, 84,23);
edit1 = new TextField(12);
add(edit1);
edit1.setBounds(getInsets().left + 119, getInsets().top + 30, 105, 22);
edit2 = new TextField(12);
add(edit2);
edit2.setBounds(getInsets().left + 119, getInsets().top + 75, 105, 22);
edit3 = new TextField(12);
add(edit3);
edit3.setBounds(getInsets().left + 119, getInsets().top +120, 105, 22);
button1 = new Button("Add");
button1.setFont(new Font("Helvetica", Font.PLAIN, 14));
add(button1);
button1.setBounds(getInsets().left + 315, getInsets().top + 15, 70, 22);
button2 = new Button("Sub");
add(button2);
button2.setBounds(getInsets().left + 315, getInsets().top + 55, 70, 22);
button3 = new Button("Mul");
add(button3);
button3.setBounds(getInsets().left + 315, getInsets().top + 95, 70, 22);
button4 = new Button("Div");
add(button4);
button4.setBounds(getInsets().left + 315, getInsets().top + 135, 70, 22);
button5 = new Button("Exit");
button5.setFont(new Font("Helvetica", Font.BOLD, 16));
add(button5);
button5.setBounds(getInsets().left + 173, getInsets().top + 168, 100, 22);
}
public boolean handleEvent(Event event)
{
if(event.id == Event.ACTION_EVENT && event.target == button5)
{
clickedButton5();
return true;
}
else
if(event.id == Event.ACTION_EVENT && event.target == button4)
{
clickedButton4();
return true;
}
else
if(event.id == Event.ACTION_EVENT && event.target == button3)
{
clickedButton3();
return true;
}
else
if(event.id == Event.ACTION_EVENT && event.target == button2)
{
clickedButton2();
return true;
}
else
if(event.id == Event.ACTION_EVENT && event.target == button1)
{
clickedButton1();
return true;
}
return super.handleEvent(event);
}
public void clickedButton1()
{
d1 = Double.valueOf(edit1.getText()).doubleValue();
d2 = Double.valueOf(edit2.getText()).doubleValue();
d3 = d1 + d2;
edit3.setText("" + d3);
}
public void clickedButton2()
{
d1 = Double.valueOf(edit1.getText()).doubleValue();
d2 = Double.valueOf(edit2.getText()).doubleValue();
d3 = d1 - d2;
edit3.setText("" + d3);
}
public void clickedButton3()
{
d1 = Double.valueOf(edit1.getText()).doubleValue();
d2 = Double.valueOf(edit2.getText()).doubleValue();
d3 = d1 * d2;
edit3.setText("" + d3);
}
public void clickedButton4()
{
d1 = Double.valueOf(edit1.getText()).doubleValue();
d2 = Double.valueOf(edit2.getText()).doubleValue();
d3 = d1 / d2;
edit3.setText("" + d3);
}
public void clickedButton5()
{
// to do: put event handler code here.
}
}
编译能通过。
但是用appletviewer看执行效果时有如下错误:
C:>appletviewer ScrollbarTest.class
进行读取时出现 I/O 异常: null
请问该如何解决?
|
C:>appletviewer ScrollbarTest
|
我在jbuilder中将你的代码试了一下,发现把下面两句屏蔽掉,可以运行,只是界面布局不好,不知道对你有没有帮助。
//setLayout(null);
//addNotify();
//setLayout(null);
//addNotify();
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。