Java Hello World example and add button events and functions, helloworld
Create an android project and click "Next" by default;
2. Add Button
3. Add the following red code to src MainActivity. java:
Import android. support. v7.app. ActionBarActivity;
Import android. support. v7.app. ActionBar;
Import android. support. v4.app. Fragment;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. LayoutInflater;
Import android. view. Menu;
Import android. view. MenuItem;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. ViewGroup;
Import android. widget. Button;
Import android. widget. TextView;
Import android. widget. Toast;
Import android. OS. Build;
Public class MainActivity extends ActionBarActivity {
Private Button mButton;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
If (savedInstanceState = null ){
Getsuppfrfragmentmanager (). beginTransaction ()
. Add (R. id. container, new PlaceholderFragment (). commit ();
}
MButton = (Button) findViewById (R. id. button1 );
MButton. setOnClickListener (buttonOnClickListener );
}
Private OnClickListener buttonOnClickListener = new OnClickListener (){
@ Override
Public void onClick (View v ){
Toast. makeText (getApplicationContext (), "click button event ",
Toast. LENGTH_SHORT). show ();
}
};
Compile a java program and print "" Hello World "Information 10 times.
Public class Test {
Public static void main (String [] args ){
For (int I = 0; I <10; I ++ ){
System. out. println ("Hello World ");
}
}
}
How can I add a button in java to close the window when the button is clicked? For example
The system does not close, just hides the form! Obtain the form from the button listening event, and set the visible attribute of the form to false.
For example:
Public class window {
Public static void main (String [] arg0 ){
MyWindow mw = new myWindow ();
Mw. setVisible (true );
}
}
Class myWindow extends JFrame {
Private JButton jb = null;
Public myWindow (){
Jb = new JButton ("test ");
Jb. addActionListener (new mylistener (this ));
This. add (jb );
}
}
Class mylistener implements ActionListener {
Private myWindow mw = null;
Public mylistener (myWindow mw ){
This. mw = mw;
}
@ Override
Public void actionreceivmed (ActionEvent e ){
// TODO Auto-generated method stub
Mw. setVisible (false );
}
}