我要写一个程序,要求在文本框中输入一串文本,然后按下OK,将其送入2个class里面处理,然后新建一个窗口将结果显示出来(就是main里面那5个输出语句),按下Cancel则关闭程序,求高手帮忙写完剩下部分,我手边的教材都是英文,而且事件的操作都没有提到这种情况,请指教
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;class Caesar{
String ori,enc,dec;
Caesar(String stri){
ori = stri;
}
Caesar(){
ori = "";
}
public char shiftchar(char oriChar,int pos){
int intChar;
intChar = (int)oriChar;
if (pos == 3)
if(((intChar>119)&&(intChar<123))||((intChar>87)&&(intChar<91)))
intChar -= (26-pos);
else intChar += 3;
else
if(((intChar>96)&&(intChar<100))||((intChar>64)&&(intChar<68)))
intChar += (26+pos);
else intChar -= 3;
return (char)intChar;
}
public String Encrypted(){
enc="";
ori += "   ";
int space_pos,index_pos=0;
space_pos = ori.indexOf(' ');
while(space_pos != (ori.length()-1)){
for(;index_pos<space_pos;index_pos++)
enc += shiftchar(ori.charAt(index_pos),3);
enc += " ";
index_pos += 1;
space_pos = ori.indexOf(' ',index_pos);
}
return enc.substring(0,enc.length()-2);
}
public String Decrypted(){
dec="";
int space_pos,index_pos=0;
space_pos = enc.indexOf(' ');
while(space_pos != (enc.length()-1)){
for(;index_pos<space_pos;index_pos++)
dec += shiftchar(enc.charAt(index_pos),-3);
dec += " ";
index_pos += 1;
space_pos = enc.indexOf(' ',index_pos);
}
return dec.substring(0,dec.length()-1);
}}
class Transpose{
String ori,enc,dec;
Transpose(String stri){
ori = stri;
}
Transpose(){
ori = "";
}
public String shiftround(String ori){
String round="";
ori += "   ";
int index_count,pre_space_pos,space_pos,index_pos;
pre_space_pos = -1;
space_pos = ori.indexOf(' ');
while(space_pos != (ori.length()-1)){
index_pos = space_pos - 1;
index_count = index_pos;
for(;index_count>pre_space_pos;index_count--)
round += ori.charAt(index_count);
round += " ";
pre_space_pos = space_pos;
index_pos += 2;
space_pos = ori.indexOf(' ',index_pos);
}
return round.substring(0,round.length()-2);
}
public String Encrypted(){
enc = shiftround(ori);
return enc;
}
public String Decrypted(){
dec = shiftround(enc);
return dec;
}
}
public class ShowFlowLayout extends JFrame{
private ImageIcon icon = new ImageIcon("images/icon.jpg");
private JButton jbtOK = new JButton("OK");
private JButton jbtCancel = new JButton("Cancel");
public ShowFlowLayout(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1,2,5,5));
p1.add(jbtOK);
p1.add(jbtCancel);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(3,1,5,5));
p2.add(new JLabel("Input a String to be encrypted"));
p2.add(new JTextField(20));
p2.add(p1, BorderLayout.SOUTH);
add(new JLabel(icon),BorderLayout.CENTER);
add(p2, BorderLayout.EAST);
jbtOK.addActionListener(new ActionListener(){
});
jbtCancel.addActionListener(new ActionListener(){
});
}
public static void main(String arg[]){
ShowFlowLayout frame = new ShowFlowLayout();
frame.setTitle("String");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,200);
frame.setVisible(true);
String str="This is the secret message";
Caesar c1= new Caesar(str);
System.out.println(c1.ori);
System.out.println(c1.Encrypted());
System.out.println(c1.Decrypted());
Transpose t1= new Transpose(str);
System.out.println(t1.ori);
System.out.println(t1.Encrypted());
System.out.println(t1.Decrypted());
}
}

解决方案 »

  1.   

    只改了ShowFlowLayout一个类,应该实现了你说的要求import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class ShowFlowLayout extends JFrame{
    private ImageIcon icon = new ImageIcon("images/icon.jpg");
    private JButton jbtOK = new JButton("OK");
    private JButton jbtCancel = new JButton("Cancel");
    private JTextField jtfield = new JTextField(20); public ShowFlowLayout(){
    init();
    } private void init(){
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(1,2,5,5));
    p1.add(jbtOK);
    p1.add(jbtCancel);
    JPanel p2 = new JPanel();
    p2.setLayout(new GridLayout(3,1,5,5));
    p2.add(new JLabel("Input a String to be encrypted"));
    p2.add(jtfield);
    p2.add(p1,BorderLayout.SOUTH);
    add(new JLabel(icon),BorderLayout.CENTER);
    add(p2,BorderLayout.CENTER);
    MyListener myListener = new MyListener();
    jbtOK.addActionListener(myListener);
    jbtCancel.addActionListener(myListener);
    setTitle("String");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400,200);
    setVisible(true);
    } class MyListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    if(e.getSource() == jbtOK){
    String str = jtfield.getText();
    if(str == null || "".equals(str)){
    JOptionPane.showMessageDialog(null,"输入不能为空,请重新输入!");
    }else{
    Caesar c1 = new Caesar(str);
    System.out.println(c1.ori);
    System.out.println(c1.Encrypted());
    System.out.println(c1.Decrypted());
    Transpose t1 = new Transpose(str);
    System.out.println(t1.ori);
    System.out.println(t1.Encrypted());
    System.out.println(t1.Decrypted());
    StringBuffer result = new StringBuffer();
    result.append(c1.ori).append("\r\n").append(c1.Encrypted()).append("\r\n").append(c1.Decrypted()).append(
    "\r\n").append(t1.ori).append("\r\n").append(t1.Encrypted()).append("\r\n").append(t1.Decrypted());
    JOptionPane.showMessageDialog(null,result.toString(),"运行结果",JOptionPane.INFORMATION_MESSAGE);
    }
    }
    if(e.getSource() == jbtCancel){
    System.exit(0);
    }
    }
    }
    public static void main(String arg[]){
    new ShowFlowLayout();
    }
    }
      

  2.   

    public class ShowFlowLayout extends JFrame {
    private ImageIcon icon = new ImageIcon("images/icon.jpg");
    private JButton jbtOK = new JButton("OK");
    private JButton jbtCancel = new JButton("Cancel"); public ShowFlowLayout() {
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(1, 2, 5, 5));
    p1.add(jbtOK);
    p1.add(jbtCancel);
    JPanel p2 = new JPanel();
    p2.setLayout(new GridLayout(3, 1, 5, 5));
    p2.add(new JLabel("Input a String to be encrypted"));
    final JTextField jtf = new JTextField(20);
    p2.add(jtf);
    p2.add(p1, BorderLayout.SOUTH);
    add(new JLabel(icon), BorderLayout.CENTER);
    add(p2, BorderLayout.EAST);

    jbtOK.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String str = jtf.getText();
    if(str == null || str.equals("")) {
    JOptionPane.showMessageDialog(null,"输入不能为空,请重新输入!");
    }
    else {
    Caesar c1 = new Caesar(str);
                        Transpose t1 = new Transpose(str);
                        StringBuffer result = new StringBuffer();
                        result.append(c1.ori).append("\r\n").append(c1.Encrypted()).append("\r\n").append(c1.Decrypted()).append(
                                "\r\n").append(t1.ori).append("\r\n").append(t1.Encrypted()).append("\r\n").append(t1.Decrypted());
                        JOptionPane.showMessageDialog(null,result.toString(),"运行结果",JOptionPane.INFORMATION_MESSAGE);
                    }
    }

    });
    jbtCancel.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    System.exit(0);
    }

    });
    }