大家可以帮我看看这个程序是怎么编译出的吗?实在是想不出了,谢谢各位
(1)易于设计,实现和使用
(2)使用过程中不容易导致出错
(3)更加的安全,可以随意地共用
(4)天然具备线程安全性,无需增加额外的同步操作
答:关于JAVA程序的编译,一般可以通过以下步骤来完成:
1.首先,编写JAVA程序代码,在文本编辑器或IDE中编辑代码。
2.对代码进行编译。在命令行中输入javac FileName.java,其中FileName是你的JAVA程序的文件名。这个过程会将你的代码编译成一个或多个字节码文件,以.class作为文件扩展名。
3.运行JAVA程序。在命令行中输入java FileName,其中FileName是你的JAVA程序的文件名(不需要输入.class扩展名)。这个过程将会启动JAVA虚拟机(JVM),并在虚拟机上运行你的程序。
另外,在JAVA编程中,还需要注意到JAVA中方法的传参只有值传递,没有引用传递,传递的是参数的拷贝。因此,在传递可变对象时需要避免共享可变对象的引用,可以返回原对象的一个拷贝来实现避免改变原数据的取值。
还有,为了保证程序的健壮性和稳定性,我们可以使用不可变对象来编写代码,这样可以简化对象的构造,测试和使用,避免副作用,确保对象状态不变,提高性能等多种好处。创建不可变对象的方法包括将类的成员变量设置为final或被声明为private final,并提供只读的get方法来让其它类使用该对象属性。例如:
public final class ImmutableExample {
private final int value;
private final String name;
public ImmutableExample(int value, String name) {
this.value = value;
this.name = name;
}
public int getValue() {
return value;
}
public String getName() {
return name;
}
}
以上是JAVA编程中的基础知识和注意点,希望对您有所帮助。
有几处小细节需要改进把,如关键字写错了,然后监听事件修改下。
初步修改如下:
参考链接:
package chapter01;
import javax.swing.*; // 这里导包关键字改为import
import java.awt.*;
import java.awt.event.*;
public class PersonalInfoProgram extends JFrame {
private JLabel nameLabel,idLabel,classLabel;
private JTextField nameField,idField,classField;
private JTextArea introArea;
public PersonalInfoProgram(String name,String id) {
setTitle("李四软件工程003");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,260);
nameLabel = new JLabel("姓名");
idLabel = new JLabel("学号");
classLabel = new JLabel("班级");
nameField = new JTextField(20);
idField = new JTextField(20);
// 这里创建对象的关键字改为new
classField = new JTextField(20);
JTextArea introArea = new JTextArea(6,25); // 这里创建一个JTextArea对象
introArea.setEditable(false);
// https://blog.csdn.net/hbjhappy/article/details/46882743
// https://blog.csdn.net/weixin_30588907/article/details/98872836
// 这里使用匿名内部类来实现监听事件
nameField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// https://blog.csdn.net/weixin_43149737/article/details/84257117
introArea.setText("我的姓名是"+nameField.getText());
}
});
// 同上
idField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
introArea.setText("我的学号是"+idField.getText());
}
});
// 同上
classField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
introArea.setText("我的班级是"+classField.getText());
}
});
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(nameLabel);
panel.add(nameField);
panel.add(idLabel);
panel.add(idField);
panel.add(classLabel);
panel.add(classField);
panel.add(introArea);
getContentPane().add(panel,BorderLayout.NORTH);
//getContentPane().add(new JScrollPane(textArea),BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String name = "李四";
String id= "003";
new PersonalInfoProgram(name,id);
}
}