求帮忙看看这个关于界面的程序
刚学窗口界面设计,写了一个抽签的小程序,但是运行起来有问题,求大神帮忙诊断下
代码如下:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
public class EX3jiemian extends JFrame implements ActionListener{
private JLabel jprint1,jprint2,jprint3;
private JTextField input1,input2,input3;
private JButton button1,button2;
public EX3jiemian(){
this.setLayout(new FlowLayout ());
jprint1=new JLabel("参与抽签人数");
jprint2=new JLabel("从几号抽取");
jprint3=new JLabel("抽取人数");
input1=new JTextField(5);
input2=new JTextField(5);
input3=new JTextField(5);
button1=new JButton("确定");
button2=new JButton("重置");
this.add(jprint1);
this.add(input1);
this.add(jprint2);
this.add(input2);
this.add(jprint3);
this.add(input3);
this.add(button1);
this.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
this.setSize(200,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
int i=0;
JFrame j=new JFrame();
JTextArea jput=new JTextArea();
j.add(jput);
j.setSize(200, 200);
if(e.getSource()==button1){
Vector vector=new Vector(3,3);
int a=Integer.getInteger(input1.getText()); //用getText拿到input1里面的内容
int b=Integer.getInteger(input2.getText());
int c=Integer.getInteger(input3.getText());
j.setVisible(true);
for(i=0;i<c;i++){
int n=(int)(Math.random()*(b+1)+a-b);
vector.add(n);
for(int k=0;k<i;k++){ //这个地方为什么会死循环?
if(vector.get(i)==vector.get(k)){
--i;
break;
}
}
}
for(i=0;i<c;i++){
jput.setText((String) vector.get(i));
}
}
if(e.getSource()==button2){
input1.setText("");
input2.setText("");
input3.setText("");
jput.setText("");
}
}
public static void main(String[] args){
new EX3jiemian();
}
}
异常:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at shangji9.EX3jiemian.actionPerformed(EX3jiemian.java:58)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
抛出这么多异常。。。。。。。。
请问是需要怎么修改。。哪里出错了。貌似不只一处错误
JAVA界面
[解决办法]
首先看你的按钮点击事件里的
int a = Integer.getInteger(input1.getText());
这里的getInteger方法的并不是你认为的字符串转int,而是获取一个指定名称的int类型的系统属性,正确的方法应该是使用Integer.valueOf()这才是你需要的
第二个:jput.setText((String) vector.get(i));
你的Vector中存储的是Integer类型,而你让一个Integer类型强制转换成String类型是会抛出一个ClassCastException异常的,至于解决方法,直接使用toString()方法就行了
jput.setText(vector.get(i).toString());
或者jput.setText(vector.get(i) + ""); //这个用法不明白的话自己去查查相关资料
[解决办法]
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
public class EX3jiemian extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel jprint1,jprint2,jprint3;
private JTextField input1,input2,input3;
private JButton button1,button2;
public EX3jiemian(){
this.setLayout(new FlowLayout ());
jprint1=new JLabel("参与抽签人数");
jprint2=new JLabel("从几号抽取");
jprint3=new JLabel("抽取人数");
input1=new JTextField(5);
input2=new JTextField(5);
input3=new JTextField(5);
button1=new JButton("确定");
button2=new JButton("重置");
this.add(jprint1);
this.add(input1);
this.add(jprint2);
this.add(input2);
this.add(jprint3);
this.add(input3);
this.add(button1);
this.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
this.setSize(200,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
int i=0;
JFrame j=new JFrame();
JTextArea jput=new JTextArea();
j.add(jput);
j.setSize(200, 200);
if(e.getSource()==button1){
Vector vector=new Vector(3,3);
int a=Integer.parseInt(input1.getText()); //用getText拿到input1里面的内容
int b=Integer.parseInt(input2.getText());
int c=Integer.parseInt(input3.getText());
//int a=Integer.getInteger(input1.getText()); //用getText拿到input1里面的内容
//int b=Integer.getInteger(input2.getText());
//int c=Integer.getInteger(input3.getText());
j.setVisible(true);
for(i=0;i<c;i++){
int n=(int)(Math.random()*(a-b)+b+1);
vector.add(n);
for(int k=0;k<i;k++){ //这个地方为什么会死循环?
if(vector.get(i)==vector.get(k)){
--i;
vector.remove(i);
break;
}
}
}
for(i=0;i<c;i++){
jput.append(vector.get(i).toString()+"\n");
}
}
if(e.getSource()==button2){
input1.setText("");
input2.setText("");
input3.setText("");
jput.setText("");
}
}
public static void main(String[] args){
new EX3jiemian();
}
}
你试试,我就改了说的那几点,没有问题啊,比如你设置a=10,b=7,c=2,那么抽签的结果就只能是8,9或8,10,或9,10