java小程序,怎么把界面和算法结合起来
importjava.awt.Color;importjava.awt.Container;importjavax.swing.*;publicclasstext{pub...
import java.awt.Color;
import java.awt.Container;
import javax.swing.*;
public class text {
public static void main(String[] args) {
JFrame frame1 = new JFrame("Swing测试");
frame1.setSize(320, 240);
frame1.setLocation(220, 160);
Container contentPane=frame1.getContentPane();
contentPane.setBackground(Color.white);
contentPane.setLayout(null);
JLabel jp1=new JLabel("请输入字符串:");
jp1.setBounds(30, 20, 120, 100);
contentPane.add(jp1);
JTextField field1 = new JTextField(10);
field1.setBounds(120,50,100,30);
contentPane.add(field1);
JLabel jp2=new JLabel("字符串长度为:");
jp2.setBounds(30, 80, 120, 100);
contentPane.add(jp2);
JTextField field2 = new JTextField(10);
contentPane.add(field2);
field2.setBounds(120,110,100,30);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
和
import java.util.*;
public class text {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = s.nextLine();
System.out.println("字符串的长度是:"+str.length());
}
} 展开
import java.awt.Container;
import javax.swing.*;
public class text {
public static void main(String[] args) {
JFrame frame1 = new JFrame("Swing测试");
frame1.setSize(320, 240);
frame1.setLocation(220, 160);
Container contentPane=frame1.getContentPane();
contentPane.setBackground(Color.white);
contentPane.setLayout(null);
JLabel jp1=new JLabel("请输入字符串:");
jp1.setBounds(30, 20, 120, 100);
contentPane.add(jp1);
JTextField field1 = new JTextField(10);
field1.setBounds(120,50,100,30);
contentPane.add(field1);
JLabel jp2=new JLabel("字符串长度为:");
jp2.setBounds(30, 80, 120, 100);
contentPane.add(jp2);
JTextField field2 = new JTextField(10);
contentPane.add(field2);
field2.setBounds(120,110,100,30);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
和
import java.util.*;
public class text {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = s.nextLine();
System.out.println("字符串的长度是:"+str.length());
}
} 展开
展开全部
建议在JFrame中增加一个Button,
然后在Button上增加一个事件监听函数
Button b= new Button(String button名);
b.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str = field1.getText();
field2.setText(str.length());
}
});
追问
刚刚在学习java,很多情况都不懂,可以帮忙做一下吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询