Java Hello World example and add button events and functions, helloworld

Source: Internet
Author: User

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 );
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.