代码如下:
package neuq.zpr1;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MyFrame2 extends JFrame implements ActionListener {
JTextField textinput;
JTextField textoutput;
JButton sort;
void init()
{
textinput=new JTextField(20);
textoutput=new JTextField(20);
sort=new JButton("排序");
add(textinput);
add(textoutput);
add(sort);
sort.addActionListener(this);
}
public MyFrame2()
{
init();
this.setLayout(new FlowLayout());
this.setBounds(100,100,190,150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
String s1=textinput.getText();
s1.split("");
String[] ss;
ss=s1.split("");
int temp;
for(int i=0 ; i < ss.length ; ++i)
{
for(int j=0; j <ss.length - i - 1; ++j)
{
int x,t;
x=Integer.parseInt(ss[j]);
t=Integer.parseInt(ss[j+1]);
if(x>t)
{
String temp1;
temp1 = ss[j];
ss[j]=ss[j+1];
ss[j+1]=temp1;
}
}
}
textoutput.setText(s1);
// TODO Auto-generated method stub
}
}创建个主类后,运行没反应。感觉这块有问题textoutput.setText(s1);
求各位大神解决
------解决方案--------------------
你字符串切分的有问题,改成下面的就OK了,还有输出的字符串的那个部分给你改了一下
要求输入 1,5,32,14,23 这样的形式就可以正常输出
- Java code
import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;public class MyFrame2 extends JFrame implements ActionListener { JTextField textinput; JTextField textoutput; JButton sort; void init(){ textinput=new JTextField(20); textoutput=new JTextField(20); sort=new JButton("排序"); add(textinput); add(textoutput); add(sort); sort.addActionListener(this); } public MyFrame2() { init(); this.setLayout(new FlowLayout());// this.setBounds(100,100,190,150); this.pack(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void actionPerformed(ActionEvent e) { String s1=textinput.getText();// s1.split(""); String[] ss; ss=s1.split(","); for(int i=0 ; i < ss.length ; ++i) { for(int j=0; j <ss.length - i - 1; ++j) { int x,t; x=Integer.parseInt(ss[j]); t=Integer.parseInt(ss[j+1]); if(x>t){ String temp1; temp1 = ss[j]; ss[j]=ss[j+1]; ss[j+1]=temp1; } } } String output=""; int temp=0; for(String s:ss){ if(temp==0){ output=output+s; } else output=output+","+s; temp++; } textoutput.setText(output);// TODO Auto-generated method stub } public static void main(String[] args) { new MyFrame2(); }}