代碼如下:
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供汨羅企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、HTML5建站、小程序制作等業(yè)務(wù)。10年已為汨羅眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
public?class?Test?{
public?static?void?main(String[]?args)?{
String[]?nameArr?=?new?String[5];
//?賦值4個
nameArr[0]?=?"name0";
nameArr[1]?=?"name1";
nameArr[2]?=?"name2";
nameArr[3]?=?"name3";
//?再添加一個
nameArr[4]?=?"最后添加的";
//?遍歷數(shù)組,分別取到數(shù)組中的元素
for?(int?i?=?0;?i??nameArr.length;?i++)?{
System.out.print(nameArr[i]?+?"?");
}
}
}
運行效果:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class Test {
private JFrame frame;
private JTextField jid,jname,jsex,jedu,jlike;
private JLabel lid,lname,lsex,ledu,llike;
private JButton ok;
private JButton quit;
public Test(){//用戶ID、姓名、性別、學(xué)歷、愛好
frame=new JFrame("用戶注冊");
jid=new JTextField(10);
jname=new JTextField(10);
jsex=new JTextField(10);
jedu=new JTextField(10);
jlike=new JTextField(10);
lid=new JLabel("用戶id:");
lname=new JLabel("用戶名:");
lsex=new JLabel("性別:");
ledu=new JLabel("學(xué)歷:");
llike=new JLabel("愛好:");
ok=new JButton("注冊");
quit=new JButton("取消");
init();
showMe();
addEventHandler();
}
private void addEventHandler() {
quit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public void init(){
frame.setLayout(new BorderLayout(2,2));
Border titled = BorderFactory.createTitledBorder(BorderFactory
.createMatteBorder(5, 5, 5, 5, Color.CYAN));
JPanel center = new JPanel();
center.setBorder(titled);
center.setLayout(new GridLayout(5,2,9,10));
JPanel south=new JPanel();
south.setLayout(new FlowLayout());
center.add(lid);
center.add(jid);
center.add(lname);
center.add(jname);
center.add(lsex);
center.add(jsex);
center.add(ledu);
center.add(jedu);
center.add(llike);
center.add(jlike);
south.add(ok);
south.add(quit);
frame.add(center,BorderLayout.CENTER);
frame.add(south,BorderLayout.SOUTH);
}
public void showMe(){
frame.setLocation(500, 300);
frame.setSize(210,280);
frame.setResizable(false);//不能拖動大小
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test().showMe();
}
}
我不知道你是怎樣寫得,我給你一段自己看下
package CenTo;
/**
* 本程序?qū)⒃贖KEY_CURRENT_USER\Software\JavaSoft\Prefs設(shè)置目錄設(shè)置相應(yīng)的操作。
* 本程序?qū)崿F(xiàn)對注冊表:
* 增加目錄、增加鍵及相應(yīng)的值、刪除、保存為XML文件
*/
import java.io.*;
import java.util.prefs.*;
public class Prefs {
public static void main(String args[]) {
String denominations[] = {"One", "Two", "Five", "Ten", "Twenty"};
String pictures[] = {"Washington", "Jefferson", "Lincoln", "Hamilton", "Jackson"};
NodeChangeListener nodeChangeListener = new NodeChangeListener() {
public void childAdded(NodeChangeEvent event) {
Preferences parent = event.getParent();
Preferences child = event.getChild();
System.out.println(parent.name() + " has a new child " +child.name());
}
public void childRemoved(NodeChangeEvent event) {
Preferences parent = event.getParent();
Preferences child = event.getChild();
System.out.println(parent.name() + " lost a child " +child.name());
}
};
PreferenceChangeListener preferenceChangeListener = new PreferenceChangeListener() {
public void preferenceChange(PreferenceChangeEvent event) {
String key = event.getKey();
String value = event.getNewValue();
Preferences node = event.getNode();
System.out.println(node.name() + " now has a value of " + value + " for " + key);
}
};
// 設(shè)定需要根目錄
Preferences prefs = Preferences.userRoot().node("/net/fubin/ibm");
// 添加監(jiān)聽
prefs.addNodeChangeListener(nodeChangeListener);
prefs.addPreferenceChangeListener(preferenceChangeListener);
//保存一個束的鍵名和相應(yīng)的值
for (int i=0, n=denominations.length; i n; i++) {
prefs.put(denominations[i], pictures[i]);
}
// 顯示所有的鍵名字及值
try {
String keys[] = prefs.keys();
for (int i=0, n=keys.length; i n; i++) {
System.out.println(keys[i] + ": " + prefs.get(keys[i], "Unknown"));
}
} catch (BackingStoreException e) {
System.err.println("Unable to read backing store: " + e);
}
// 建立 子目錄
Preferences child = Preferences.userRoot().node("/net/fubin/ibm/foo");
//保存為XML文件
try {
FileOutputStream fos = new FileOutputStream("prefs.xml");
prefs.exportNode(fos);
} catch (Exception e) {
System.err.println("Unable to export nodes: " + e);
}
//刪除新生成的接點
try {
prefs.removeNode();
} catch (BackingStoreException e) {
System.err.println("Unable to access backing store: " + e);
}
}
}