哪位高手帮忙改一下这个JAVA程序,现在在eclipse运行不成功,谢谢!
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.JFrame;...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextArea;
class Num extends JFrame implements ActionListener {
private JFrame jFrame = null;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JTextArea jTextArea = null;
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setSize(new Dimension(450, 210));
jFrame.setTitle("提取数字");
jFrame.setContentPane(getJContentPane());
}
return jFrame;
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(10, 5, 65, 30));
jLabel.setText("输入字符串");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTextArea(), null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(90, 4, 220, 35));
}
return jTextField;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(325, 4, 80, 35));
jButton.setText("确定");
jButton.addActionListener(this) ;
}
return jButton;
}
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setBounds(new Rectangle(2, 45,430, 125));
jTextArea.setText("");
}
return jTextArea;
}
public void actionPerformed(ActionEvent e)
{
String str;
str=jTextField.getText();
char s[]={};
int a[]={0};
int i,k,m;
i=0;k=0;m=0;
char p[]=str.toCharArray();
while(p[m]!='\0')
{
if(p[m]>='0'&&p[m]<='9')
{
s[i]=p[m];
a[k]=10*a[k]+s[i]-'0';
i++;
}
else
{
if(i>0)
{
k++;
i=0;
}
}
m++;
}
if(i>0)
k++;
jTextArea.append("字符串中有"+k+"个数字,分别为:\n");
for(i=0;i<k;i++)
jTextArea.append(a[i]+"n");
}
}
public class Java
{
public static void main(String args[])
{
JFrame Num=new JFrame();
}
} 展开
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextArea;
class Num extends JFrame implements ActionListener {
private JFrame jFrame = null;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JTextArea jTextArea = null;
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setSize(new Dimension(450, 210));
jFrame.setTitle("提取数字");
jFrame.setContentPane(getJContentPane());
}
return jFrame;
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(10, 5, 65, 30));
jLabel.setText("输入字符串");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTextArea(), null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(90, 4, 220, 35));
}
return jTextField;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(325, 4, 80, 35));
jButton.setText("确定");
jButton.addActionListener(this) ;
}
return jButton;
}
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setBounds(new Rectangle(2, 45,430, 125));
jTextArea.setText("");
}
return jTextArea;
}
public void actionPerformed(ActionEvent e)
{
String str;
str=jTextField.getText();
char s[]={};
int a[]={0};
int i,k,m;
i=0;k=0;m=0;
char p[]=str.toCharArray();
while(p[m]!='\0')
{
if(p[m]>='0'&&p[m]<='9')
{
s[i]=p[m];
a[k]=10*a[k]+s[i]-'0';
i++;
}
else
{
if(i>0)
{
k++;
i=0;
}
}
m++;
}
if(i>0)
k++;
jTextArea.append("字符串中有"+k+"个数字,分别为:\n");
for(i=0;i<k;i++)
jTextArea.append(a[i]+"n");
}
}
public class Java
{
public static void main(String args[])
{
JFrame Num=new JFrame();
}
} 展开
3个回答
展开全部
一下代码是我在你写的代码基础上修改的:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextArea;
class Num extends JFrame implements ActionListener {
private JFrame jFrame = null; //删掉
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JTextArea jTextArea = null;
private JFrame getJFrame() { //修改成: public Num(){
if (jFrame == null) { //删掉
jFrame = new JFrame(); //删掉
jFrame.setSize(new Dimension(450, 210)); //修改成: this.setSize(new Dimension(450, 210));
jFrame.setTitle("提取数字"); //修改成: this.setTitle("提取数字");
jFrame.setContentPane(getJContentPane()); //修改成: this.setContentPane(getJContentPane());
} //删掉
return jFrame; //删掉
//增加: this.setVisible(true);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(10, 5, 65, 30));
jLabel.setText("输入字符串");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTextArea(), null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(90, 4, 220, 35));
}
return jTextField;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(325, 4, 80, 35));
jButton.setText("确定");
jButton.addActionListener(this) ;
}
return jButton;
}
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setBounds(new Rectangle(2, 45,430, 125));
jTextArea.setText("");
}
return jTextArea;
}
public void actionPerformed(ActionEvent e)
{
String str;
str=jTextField.getText();
char s[]={};
/* //删掉整个注释里的内容:
int a[]={0};
int i,k,m;
i=0;k=0;m=0;
char p[]=str.toCharArray();
while(p[m]!='\0')
{
if(p[m]>='0'&&p[m]<='9')
{
s[i]=p[m];
a[k]=10*a[k]+s[i]-'0';
i++;
}
else
{
if(i>0)
{
k++;
i=0;
}
}
m++;
}
if(i>0)
k++;
jTextArea.append("字符串中有"+k+"个数字,分别为:\n");
for(i=0;i<k;i++)
jTextArea.append(a[i]+"n");
}
*/
//增加下面的内容:
int k;
k=0;
char p[]=str.toCharArray();
s = p;
for(int j = 0; j < p.length; j++)
{
if(p[j] >= '0' && p[j] <= '9')
{
s[k] = p[j];
k++;
}
}
if(k == 0)
{
jTextArea.setText("字符串中有"+k+"个数字\n");
}
else
{
jTextArea.setText("字符串中有"+k+"个数字,分别为:\n");
for(int z = 0; z < k; z++)
{
jTextArea.append(s[z]+"\n");
}
}
}
public class Java
{
public static void main(String args[])
{
JFrame Num=new JFrame();//删掉
//增加: Num JFrame = new Num();
}
}
而下面的代码是我修改后可直接运行的代码:
他们有不同之处,请自己看看:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextArea;
class Num extends JFrame implements ActionListener {
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JTextArea jTextArea = null;
public Num() {
this.setSize(new Dimension(450, 210));
this.setTitle("提取数字");
this.setContentPane(getJContentPane());
this.setVisible(true);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(10, 5, 65, 30));
jLabel.setText("输入字符串");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTextArea(), null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(90, 4, 220, 35));
}
return jTextField;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(325, 4, 80, 35));
jButton.setText("确定");
jButton.addActionListener(this) ;
}
return jButton;
}
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setBounds(new Rectangle(2, 45,430, 125));
jTextArea.setText("");
}
return jTextArea;
}
public void actionPerformed(ActionEvent e)
{
String str;
str=jTextField.getText();
char s[]={};
int k;
k=0;
char p[]=str.toCharArray();
s = p;
for(int j = 0; j < p.length; j++)
{
if(p[j] >= '0' && p[j] <= '9')
{
s[k] = p[j];
k++;
}
}
if(k == 0)
{
jTextArea.setText("字符串中有"+k+"个数字\n");
}
else
{
jTextArea.setText("字符串中有"+k+"个数字,分别为:\n");
for(int z = 0; z < k; z++)
{
jTextArea.append(s[z]+"\n");
}
}
}
}
public class Java
{
public static void main(String args[])
{
Num JFrame = new Num();
}
}
首先你在main函数当中应该创建你自己的写的类Num,不然怎么
会运行Num类当中的方法,而你在main函数中创建JFrame类是可以编译的,但是根本无法调用Num类中的方法getJFrame(),而这个getJFrame(),你的意图是将它设为构造函数,但是这样的写法不对,构造函数应该是public修饰,函数名即为类名Num。
这里的Num因为继承了JFrame类,可以认为他就可以行使JFrame类
权利,当所有容器都放入JFrame类后要将它显示出来,因为,
默认的它是不显示的,即写出this.setVisible(true);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextArea;
class Num extends JFrame implements ActionListener {
private JFrame jFrame = null; //删掉
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JTextArea jTextArea = null;
private JFrame getJFrame() { //修改成: public Num(){
if (jFrame == null) { //删掉
jFrame = new JFrame(); //删掉
jFrame.setSize(new Dimension(450, 210)); //修改成: this.setSize(new Dimension(450, 210));
jFrame.setTitle("提取数字"); //修改成: this.setTitle("提取数字");
jFrame.setContentPane(getJContentPane()); //修改成: this.setContentPane(getJContentPane());
} //删掉
return jFrame; //删掉
//增加: this.setVisible(true);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(10, 5, 65, 30));
jLabel.setText("输入字符串");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTextArea(), null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(90, 4, 220, 35));
}
return jTextField;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(325, 4, 80, 35));
jButton.setText("确定");
jButton.addActionListener(this) ;
}
return jButton;
}
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setBounds(new Rectangle(2, 45,430, 125));
jTextArea.setText("");
}
return jTextArea;
}
public void actionPerformed(ActionEvent e)
{
String str;
str=jTextField.getText();
char s[]={};
/* //删掉整个注释里的内容:
int a[]={0};
int i,k,m;
i=0;k=0;m=0;
char p[]=str.toCharArray();
while(p[m]!='\0')
{
if(p[m]>='0'&&p[m]<='9')
{
s[i]=p[m];
a[k]=10*a[k]+s[i]-'0';
i++;
}
else
{
if(i>0)
{
k++;
i=0;
}
}
m++;
}
if(i>0)
k++;
jTextArea.append("字符串中有"+k+"个数字,分别为:\n");
for(i=0;i<k;i++)
jTextArea.append(a[i]+"n");
}
*/
//增加下面的内容:
int k;
k=0;
char p[]=str.toCharArray();
s = p;
for(int j = 0; j < p.length; j++)
{
if(p[j] >= '0' && p[j] <= '9')
{
s[k] = p[j];
k++;
}
}
if(k == 0)
{
jTextArea.setText("字符串中有"+k+"个数字\n");
}
else
{
jTextArea.setText("字符串中有"+k+"个数字,分别为:\n");
for(int z = 0; z < k; z++)
{
jTextArea.append(s[z]+"\n");
}
}
}
public class Java
{
public static void main(String args[])
{
JFrame Num=new JFrame();//删掉
//增加: Num JFrame = new Num();
}
}
而下面的代码是我修改后可直接运行的代码:
他们有不同之处,请自己看看:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextArea;
class Num extends JFrame implements ActionListener {
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JTextArea jTextArea = null;
public Num() {
this.setSize(new Dimension(450, 210));
this.setTitle("提取数字");
this.setContentPane(getJContentPane());
this.setVisible(true);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(10, 5, 65, 30));
jLabel.setText("输入字符串");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTextArea(), null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(90, 4, 220, 35));
}
return jTextField;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(325, 4, 80, 35));
jButton.setText("确定");
jButton.addActionListener(this) ;
}
return jButton;
}
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setBounds(new Rectangle(2, 45,430, 125));
jTextArea.setText("");
}
return jTextArea;
}
public void actionPerformed(ActionEvent e)
{
String str;
str=jTextField.getText();
char s[]={};
int k;
k=0;
char p[]=str.toCharArray();
s = p;
for(int j = 0; j < p.length; j++)
{
if(p[j] >= '0' && p[j] <= '9')
{
s[k] = p[j];
k++;
}
}
if(k == 0)
{
jTextArea.setText("字符串中有"+k+"个数字\n");
}
else
{
jTextArea.setText("字符串中有"+k+"个数字,分别为:\n");
for(int z = 0; z < k; z++)
{
jTextArea.append(s[z]+"\n");
}
}
}
}
public class Java
{
public static void main(String args[])
{
Num JFrame = new Num();
}
}
首先你在main函数当中应该创建你自己的写的类Num,不然怎么
会运行Num类当中的方法,而你在main函数中创建JFrame类是可以编译的,但是根本无法调用Num类中的方法getJFrame(),而这个getJFrame(),你的意图是将它设为构造函数,但是这样的写法不对,构造函数应该是public修饰,函数名即为类名Num。
这里的Num因为继承了JFrame类,可以认为他就可以行使JFrame类
权利,当所有容器都放入JFrame类后要将它显示出来,因为,
默认的它是不显示的,即写出this.setVisible(true);
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextArea;
class Num extends JFrame implements ActionListener {
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JTextArea jTextArea = null;
//原来的getJFrame函数改成现在的Num构造函数
public Num() {
super("提取数字");
setSize(new Dimension(450, 210));
setContentPane(getJContentPane());
setVisible(true);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(10, 5, 65, 30));
jLabel.setText("输入字符串");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTextArea(), null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(90, 4, 220, 35));
}
return jTextField;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(325, 4, 80, 35));
jButton.setText("确定");
jButton.addActionListener(this) ;
}
return jButton;
}
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setBounds(new Rectangle(2, 45,430, 125));
jTextArea.setText("");
}
return jTextArea;
}
public void actionPerformed(ActionEvent e)
{
//算法太差,下标一个劲越界,懒的给你看了,重给你写一个
int count=0,i;
String strNumber="";
char[] chars=jTextField.getText().toCharArray();
for(char ch:chars){
if(ch>='0'&&ch<='9'){
strNumber+=ch;
count++;
}
}
jTextArea.append(String.format("字符串中有%1$d个数字,分别为:\n%2$s\n",count,strNumber));
}
}
public class Java
{
public static void main(String args[])
{
//原来创建JFrame实例现在改成创建Num实例
Num num=new Num();
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextArea;
class Num extends JFrame implements ActionListener {
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JTextArea jTextArea = null;
//原来的getJFrame函数改成现在的Num构造函数
public Num() {
super("提取数字");
setSize(new Dimension(450, 210));
setContentPane(getJContentPane());
setVisible(true);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(10, 5, 65, 30));
jLabel.setText("输入字符串");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTextArea(), null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(90, 4, 220, 35));
}
return jTextField;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(325, 4, 80, 35));
jButton.setText("确定");
jButton.addActionListener(this) ;
}
return jButton;
}
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setBounds(new Rectangle(2, 45,430, 125));
jTextArea.setText("");
}
return jTextArea;
}
public void actionPerformed(ActionEvent e)
{
//算法太差,下标一个劲越界,懒的给你看了,重给你写一个
int count=0,i;
String strNumber="";
char[] chars=jTextField.getText().toCharArray();
for(char ch:chars){
if(ch>='0'&&ch<='9'){
strNumber+=ch;
count++;
}
}
jTextArea.append(String.format("字符串中有%1$d个数字,分别为:\n%2$s\n",count,strNumber));
}
}
public class Java
{
public static void main(String args[])
{
//原来创建JFrame实例现在改成创建Num实例
Num num=new Num();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JFrame Num=new JFrame();后面加一句
Num.setVisible(true);试一下
Num.setVisible(true);试一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询