排版,如果差劲的话,这样的代码让人感觉很累!甚至厌倦!C++的排版,个人是受了《高质量C++编程》的影响。
而Java,{一般会空一格,且放在句末。如附例。Java的排版,当受什么影响呢?大家不妨说说。package com.demo.android.bmi;import java.text.DecimalFormat;import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Bmi extends Activity {
private static final String TAG = "Bmi";
private static final String PREF = "BMI_PREF";
private static final String PREF_HEIGHT = "BMI_Height";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Log.v(TAG, "onCreate");
setContentView(R.layout.main);
findViews();
restorePrefs();
setListensers();
}
private Button button_calc;
private EditText field_height;
private EditText field_weight;
private TextView view_result;
private TextView view_suggest;
private void findViews() {
button_calc = (Button)findViewById(R.id.submit);
field_height = (EditText)findViewById(R.id.height);
field_weight = (EditText)findViewById(R.id.weight);
view_result = (TextView)findViewById(R.id.result);
view_suggest = (TextView)findViewById(R.id.suggest);
System.out.println(view_suggest.toString());
} private void setListensers() {
button_calc.setOnClickListener(calcBMI);
} private Button.OnClickListener calcBMI = new Button.OnClickListener() {
public void onClick(View arg0) {
DecimalFormat nf = new DecimalFormat("0.00");
try {
double height = Double.parseDouble(field_height.getText().toString())/100;
double weight = Double.parseDouble(field_weight.getText().toString());
double BMI = weight / (height * height);
view_result.setText(getText(R.string.bmi_result) + nf.format(BMI));
//Give health advice
if (BMI > 25) {
view_suggest.setText(R.string.advice_heavy);
}
else if (BMI < 20) {
view_suggest.setText(R.string.advice_light);
}
else {
view_suggest.setText(R.string.advice_average);
}
}
catch (Exception err) {
Toast.makeText(Bmi.this, R.string.input_error, Toast.LENGTH_SHORT).show();
}
}
};
private void openOptionsDialog() {
new AlertDialog.Builder(Bmi.this)
.setTitle(R.string.about_title)
.setMessage(R.string.about_message)
.setPositiveButton(R.string.ok_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton(R.string.homepage_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//go to url
Uri uri = Uri.parse(getString(R.string.homepage_uri));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
})
.show();
}
protected static final int MENU_ABOUT = Menu.FIRST;
protected static final int MENU_QUIT = Menu.FIRST + 1; @Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, MENU_ABOUT, Menu.NONE, "关于...");
menu.add(Menu.NONE, MENU_QUIT, Menu.NONE, "结束");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_ABOUT:
openOptionsDialog();
break;
case MENU_QUIT:
finish();
break;
}
return true;
}
@Override
public void onStart() {
super.onStart();
Log.v(TAG, "onStart");
} @Override
public void onResume() {
super.onResume();
Log.v(TAG, "onResume");
} @Override
public void onPause() {
super.onPause();
Log.v(TAG, "onPause");
} @Override
public void onStop() {
super.onStop();
Log.v(TAG, "onStop");
//save user preferences. use editor object to make change.
SharedPreferences settings = getSharedPreferences(PREF, 0);
settings.edit()
.putString(PREF_HEIGHT, field_height.getText().toString())
.commit();
} @Override
public void onRestart() {
super.onRestart();
Log.v(TAG, "onRestart");
} @Override
public void onDestroy() {
super.onDestroy();
Log.v(TAG, "onDestroy");
} //restore preferences
private void restorePrefs() {
SharedPreferences settings = getSharedPreferences(PREF, 0);
String pref_height = settings.getString(PREF_HEIGHT, "");
if (!pref_height.equals("")) {
field_height.setText(pref_height);
field_weight.requestFocus();
}
}
}
而Java,{一般会空一格,且放在句末。如附例。Java的排版,当受什么影响呢?大家不妨说说。package com.demo.android.bmi;import java.text.DecimalFormat;import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Bmi extends Activity {
private static final String TAG = "Bmi";
private static final String PREF = "BMI_PREF";
private static final String PREF_HEIGHT = "BMI_Height";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Log.v(TAG, "onCreate");
setContentView(R.layout.main);
findViews();
restorePrefs();
setListensers();
}
private Button button_calc;
private EditText field_height;
private EditText field_weight;
private TextView view_result;
private TextView view_suggest;
private void findViews() {
button_calc = (Button)findViewById(R.id.submit);
field_height = (EditText)findViewById(R.id.height);
field_weight = (EditText)findViewById(R.id.weight);
view_result = (TextView)findViewById(R.id.result);
view_suggest = (TextView)findViewById(R.id.suggest);
System.out.println(view_suggest.toString());
} private void setListensers() {
button_calc.setOnClickListener(calcBMI);
} private Button.OnClickListener calcBMI = new Button.OnClickListener() {
public void onClick(View arg0) {
DecimalFormat nf = new DecimalFormat("0.00");
try {
double height = Double.parseDouble(field_height.getText().toString())/100;
double weight = Double.parseDouble(field_weight.getText().toString());
double BMI = weight / (height * height);
view_result.setText(getText(R.string.bmi_result) + nf.format(BMI));
//Give health advice
if (BMI > 25) {
view_suggest.setText(R.string.advice_heavy);
}
else if (BMI < 20) {
view_suggest.setText(R.string.advice_light);
}
else {
view_suggest.setText(R.string.advice_average);
}
}
catch (Exception err) {
Toast.makeText(Bmi.this, R.string.input_error, Toast.LENGTH_SHORT).show();
}
}
};
private void openOptionsDialog() {
new AlertDialog.Builder(Bmi.this)
.setTitle(R.string.about_title)
.setMessage(R.string.about_message)
.setPositiveButton(R.string.ok_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton(R.string.homepage_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//go to url
Uri uri = Uri.parse(getString(R.string.homepage_uri));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
})
.show();
}
protected static final int MENU_ABOUT = Menu.FIRST;
protected static final int MENU_QUIT = Menu.FIRST + 1; @Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, MENU_ABOUT, Menu.NONE, "关于...");
menu.add(Menu.NONE, MENU_QUIT, Menu.NONE, "结束");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_ABOUT:
openOptionsDialog();
break;
case MENU_QUIT:
finish();
break;
}
return true;
}
@Override
public void onStart() {
super.onStart();
Log.v(TAG, "onStart");
} @Override
public void onResume() {
super.onResume();
Log.v(TAG, "onResume");
} @Override
public void onPause() {
super.onPause();
Log.v(TAG, "onPause");
} @Override
public void onStop() {
super.onStop();
Log.v(TAG, "onStop");
//save user preferences. use editor object to make change.
SharedPreferences settings = getSharedPreferences(PREF, 0);
settings.edit()
.putString(PREF_HEIGHT, field_height.getText().toString())
.commit();
} @Override
public void onRestart() {
super.onRestart();
Log.v(TAG, "onRestart");
} @Override
public void onDestroy() {
super.onDestroy();
Log.v(TAG, "onDestroy");
} //restore preferences
private void restorePrefs() {
SharedPreferences settings = getSharedPreferences(PREF, 0);
String pref_height = settings.getString(PREF_HEIGHT, "");
if (!pref_height.equals("")) {
field_height.setText(pref_height);
field_weight.requestFocus();
}
}
}
Java貌似比较提倡lz代码中的格式呢!
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;class WindowExit extends Frame implements ActionListener {
private static final long serialVersionUID = -3434095178930651913L;
MenuBar menubar;
Menu menu;
MenuItem itemExit;
WindowExit() {
menubar = new MenuBar();
menu = new Menu("文件");
itemExit = new MenuItem("退出");
itemExit.setShortcut(new MenuShortcut(KeyEvent.VK_E));
menu.add(itemExit);
menubar.add(menu);
setMenuBar(menubar);
itemExit.addActionListener(this);
setBounds(100, 100, 150, 150);
setVisible(true);
validate();
} @Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}public class Example7_8 {
public static void main(String args[]) {
new WindowExit();
}
}
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;class MyPanel extends Panel
implements ActionListener {
private static final long serialVersionUID = 8480471557418500457L;
Button button1, button2;
Color backColor;
MyPanel() {
button1 = new Button("确定");
button2 = new Button("取消");
add(button1);
add(button2);
setBackground(Color.pink);
backColor = getBackground();
button1.addActionListener(this);
button2.addActionListener(this);
} @Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
setBackground(Color.cyan);
}
else if (e.getSource() == button2) {
setBackground(backColor);
}
}
}class WindowPanel extends Frame
implements ActionListener {
private static final long serialVersionUID = 1255168790767490653L;
MyPanel panel1, panel2;
Button button;
WindowPanel() {
setLayout(new FlowLayout());
panel1 = new MyPanel();
panel2 = new MyPanel();
button = new Button("退出");
add(panel1);
add(panel2);
add(button);
button.addActionListener(this);
setBounds(60, 60, 200, 200);
setVisible(true);
validate();
} @Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}public class Example7_a {
public static void main(String args[]) {
new WindowPanel();
}
}