这段java程序问题出在哪里?可以编译出class文件,但是运行时总是有异常,不能运行,怎么解决?代码如下
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassMyFrameimpleme...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyFrame implements ActionListener{
JFrame frame=new JFrame("My JFrame");
JButton b1,b2,b3;
JLabel label1,label2;
JTextField tf1,tf2;
public static void main(String[] args){
MyFrame mf=new MyFrame();
mf.go();
}
public void go(){
label1=new JLabel("Source");
label2=new JLabel("Target");
JPanel p3=new JPanel(new GridLayout(2,1));
p3.add(label1);
p3.add(label2);
JPanel p4=new JPanel(new GridLayout(2,1));
p4.add(tf1);
p4.add(tf2);
JPanel p1=new JPanel(new GridLayout(1,2));
p1.add(p3);
p1.add(p4);
JPanel p2=new JPanel(new FlowLayout());
b1=new JButton("Clear");
b1.addActionListener(this);
b2=new JButton("Copy");
b2.addActionListener(this);
b3=new JButton("Close");
b3.addActionListener(this);
p2.add(b1);
p2.add(b2);
p2.add(b3);
Container cp=frame.getContentPane();
cp.add(p1,BorderLayout.CENTER);
cp.add(p2,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
JButton button=(JButton)e.getSource();
if(button==b1){
tf1.setText(" ");
tf2.setText(" ");
}
if(button==b2){
if(tf1.getSelectedText()!=null)
tf2.setText(tf1.getSelectedText());
else
tf1.setText(tf1.getText());}
if(button==b3)
System.exit(0);
}
} 展开
import javax.swing.*;
import java.awt.event.*;
public class MyFrame implements ActionListener{
JFrame frame=new JFrame("My JFrame");
JButton b1,b2,b3;
JLabel label1,label2;
JTextField tf1,tf2;
public static void main(String[] args){
MyFrame mf=new MyFrame();
mf.go();
}
public void go(){
label1=new JLabel("Source");
label2=new JLabel("Target");
JPanel p3=new JPanel(new GridLayout(2,1));
p3.add(label1);
p3.add(label2);
JPanel p4=new JPanel(new GridLayout(2,1));
p4.add(tf1);
p4.add(tf2);
JPanel p1=new JPanel(new GridLayout(1,2));
p1.add(p3);
p1.add(p4);
JPanel p2=new JPanel(new FlowLayout());
b1=new JButton("Clear");
b1.addActionListener(this);
b2=new JButton("Copy");
b2.addActionListener(this);
b3=new JButton("Close");
b3.addActionListener(this);
p2.add(b1);
p2.add(b2);
p2.add(b3);
Container cp=frame.getContentPane();
cp.add(p1,BorderLayout.CENTER);
cp.add(p2,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
JButton button=(JButton)e.getSource();
if(button==b1){
tf1.setText(" ");
tf2.setText(" ");
}
if(button==b2){
if(tf1.getSelectedText()!=null)
tf2.setText(tf1.getSelectedText());
else
tf1.setText(tf1.getText());}
if(button==b3)
System.exit(0);
}
} 展开
2个回答
展开全部
tf1, tf2;
空值,未指向实例,报空指针异常
package com.neo.brickhome;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyFrame implements ActionListener {
JFrame frame = new JFrame("My JFrame");
JButton b1, b2, b3;
JLabel label1, label2;
JTextField tf1, tf2;
public static void main(String[] args) {
MyFrame mf = new MyFrame();
mf.go();
}
public void go() {
label1 = new JLabel("Source");
label2 = new JLabel("Target");
JPanel p3 = new JPanel(new GridLayout(2, 1));
p3.add(label1);
p3.add(label2);
JPanel p4 = new JPanel(new GridLayout(2, 1));
tf1 = new JTextField();
tf2 = new JTextField();
p4.add(tf1);
p4.add(tf2);
JPanel p1 = new JPanel(new GridLayout(1, 2));
p1.add(p3);
p1.add(p4);
JPanel p2 = new JPanel(new FlowLayout());
b1 = new JButton("Clear");
b1.addActionListener(this);
b2 = new JButton("Copy");
b2.addActionListener(this);
b3 = new JButton("Close");
b3.addActionListener(this);
p2.add(b1);
p2.add(b2);
p2.add(b3);
Container cp = frame.getContentPane();
cp.add(p1, BorderLayout.CENTER);
cp.add(p2, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if (button == b1) {
tf1.setText(" ");
tf2.setText(" ");
}
if (button == b2) {
if (tf1.getSelectedText() != null)
tf2.setText(tf1.getSelectedText());
else
tf1.setText(tf1.getText());
}
if (button == b3)
System.exit(0);
}
}
就OK
空值,未指向实例,报空指针异常
package com.neo.brickhome;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyFrame implements ActionListener {
JFrame frame = new JFrame("My JFrame");
JButton b1, b2, b3;
JLabel label1, label2;
JTextField tf1, tf2;
public static void main(String[] args) {
MyFrame mf = new MyFrame();
mf.go();
}
public void go() {
label1 = new JLabel("Source");
label2 = new JLabel("Target");
JPanel p3 = new JPanel(new GridLayout(2, 1));
p3.add(label1);
p3.add(label2);
JPanel p4 = new JPanel(new GridLayout(2, 1));
tf1 = new JTextField();
tf2 = new JTextField();
p4.add(tf1);
p4.add(tf2);
JPanel p1 = new JPanel(new GridLayout(1, 2));
p1.add(p3);
p1.add(p4);
JPanel p2 = new JPanel(new FlowLayout());
b1 = new JButton("Clear");
b1.addActionListener(this);
b2 = new JButton("Copy");
b2.addActionListener(this);
b3 = new JButton("Close");
b3.addActionListener(this);
p2.add(b1);
p2.add(b2);
p2.add(b3);
Container cp = frame.getContentPane();
cp.add(p1, BorderLayout.CENTER);
cp.add(p2, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if (button == b1) {
tf1.setText(" ");
tf2.setText(" ");
}
if (button == b2) {
if (tf1.getSelectedText() != null)
tf2.setText(tf1.getSelectedText());
else
tf1.setText(tf1.getText());
}
if (button == b3)
System.exit(0);
}
}
就OK
2012-08-10
展开全部
你的JTextField tf1, tf2;这俩没创建出实例来。应该是JPanel里不允许添加null的组件吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询