思路:System类的getProperty方法可以获取系统属性。本例要获取系统属性,然后显示在文本框中。
import javax.swing.*;
import java.awt.*;
public class JavaInfo extends JFrame {
JPanel panel;
GridBagLayout gbl;
GridBagConstraints gbc;
JLabel lblArray[];
JTextField txtArray[];
String sProp[]={"path.separator","file.separator","file.encoding","java.vm.version",
"java.class.version","os.arch","sun.cpu.isalist","os.name","os.version","user.name"
,"user.home","user.dir","java.class.path"};
String sLbl[]={"路径分隔符:","文件分隔符:","文件编码格式:","虚拟机版本:",
"类文件版本","操作系统架构:","CPU相关信息:","操作系统名称","操作系统版本",
"登陆用户名称:","登录用户目录","系统当前目录","类搜索路径:"};
public JavaInfo()
{
super("使用标签、文本框数组及网格袋布局来创建界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel=new JPanel();
gbl=new GridBagLayout();
gbc=new GridBagConstraints();
gbc.anchor=GridBagConstraints.NORTHWEST;//靠左上方对齐
gbc.fill=GridBagConstraints.NONE; //不采用拉伸
panel.setLayout(gbl);
lblArray=new JLabel[13];
txtArray=new JTextField[13];
for(int i=0;i<=12;i++)
{
lblArray[i]=new JLabel(sLbl[i]);
addControls(i+1,1,lblArray[i]);
txtArray[i]=new JTextField(System.getProperty(sProp[i]),40);
addControls(i+1,2,txtArray[i]);
}
getContentPane().add(panel);
pack();
setVisible(true);
}
public void addControls(int iRow,int iCol,Component c)
{
gbc.gridy=iRow;
gbc.gridx=iCol;
gbl.setConstraints(c, gbc);
panel.add(c);
}
public static void main(String[] args) {
JavaInfo gbdObj=new JavaInfo();
}
}
运行结果: