2016.3.16 (Java graphical user interface)

Source: Internet
Author: User
Tags gettext

Boundary layout

public class Borderlayouttest extends jframe{

Public Borderlayouttest () {
This.setsize (900,600);
This.setdefaultcloseoperation (Jframe.exit_on_close);
This.settitle ("Border layout");
This.setlocationrelativeto (null);//Set the form to center

This.addcontent ();

This.setvisible (TRUE);
}


public void Addcontent () {
Container con = This.getcontentpane ();
Con.setlayout (New BorderLayout ());

JButton btn1 = new JButton ("OK 1");
JButton btn2 = new JButton ("OK 2");
JButton btn3 = new JButton ("OK 3");
JButton btn4 = new JButton ("OK 4");
JButton btn5 = new JButton ("OK 5");

Con.add (BTN1, borderlayout.east);
Con.add (BTN2, borderlayout.west);
Con.add (Btn3, Borderlayout.north);
Con.add (Btn4, Borderlayout.south);
Con.add (Btn5, Borderlayout.center);
}


public static void Main (string[] args) {
New Borderlayouttest ();
}
}

Flow layout

public class Flowlayouttest extends jframe{

Public Flowlayouttest () {
This.setsize (900,600);
This.setdefaultcloseoperation (Jframe.exit_on_close);
This.settitle ("Flow Layout");
This.setlocationrelativeto (null);//Set the form to center

This.addcontent ();

This.setvisible (TRUE);
}


public void Addcontent () {
Container con = This.getcontentpane ();

Con.setlayout (New FlowLayout ());

JLabel label = new JLabel ("Test");
JButton btn = new JButton ("determines size based on element content");
JButton btn2 = new JButton ("OK");
JButton btn3 = new JButton ("OK");
JButton btn4 = new JButton ("OK");
JButton btn5 = new JButton ("OK 5");

Con.add (label);
Con.add (BTN);
Con.add (BTN2);
Con.add (BTN3);
Con.add (BTN4);
Con.add (BTN5);
}


public static void Main (string[] args) {
New Flowlayouttest ();
}

}
Network layout

public class Gridlayouttest extends JFrame {

Public Gridlayouttest () {
This.setsize (900,600);
This.setdefaultcloseoperation (Jframe.exit_on_close);
This.settitle ("Grid layout");
This.setlocationrelativeto (null);//Set the form to center

This.addcontent ();

This.setvisible (TRUE);
}


public void Addcontent () {
Container con = This.getcontentpane ();
Con.setlayout (new GridLayout (2,3));

JButton btn1 = new JButton ("OK 1");
JButton btn2 = new JButton ("OK 2");
JButton btn3 = new JButton ("OK 3");
JButton btn4 = new JButton ("OK 4");
JButton btn5 = new JButton ("OK 5");
JButton btn6 = new JButton ("OK 6");
JButton btn7 = new JButton ("OK 7");
JButton btn8 = new JButton ("OK 8");
JButton btn9 = new JButton ("OK 9");

Con.add (BTN1);
Con.add (BTN2);
Con.add (BTN3);
Con.add (BTN4);
Con.add (BTN5);
Con.add (BTN6);
Con.add (BTN7);
Con.add (BTN8);
Con.add (BTN9);
}


public static void Main (string[] args) {
New Gridlayouttest ();
}

}

Event:
public class Actionlistenertest extends JFrame {

Public Actionlistenertest () {
This.setsize (900,600);
This.setdefaultcloseoperation (Jframe.exit_on_close);
This.settitle ("Border layout");
This.setlocationrelativeto (null);//Set the form to center

This.addcontent ();

This.setvisible (TRUE);
}


private void Addcontent () {
Container con = This.getcontentpane ();
Con.setlayout (New FlowLayout ());

JButton btn1 = new JButton ("Red");
JButton btn2 = new JButton ("green");

Buttonlistener Btlis = new Buttonlistener (this);
Btn1.addactionlistener (Btlis);
Btn2.addactionlistener (Btlis);


Btn1.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent e) {
Getcontentpane (). SetBackground (color.red);
}});

Btn2.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent e) {
Getcontentpane (). SetBackground (Color.green);
}});

Con.add (BTN1);
Con.add (BTN2);
}


@Override
public void actionperformed (ActionEvent e) {
System.out.println ("enter");
Object obj = E.getsource ();
JButton btn = (JButton) obj;
String text = Btn.gettext ();
//
if (Text.equals ("Red")) {
This.getcontentpane (). SetBackground (color.red);
//}
//
if (Text.equals ("green")) {
This.getcontentpane (). SetBackground (Color.green);
//}
//}


public static void Main (string[] args) {
New Actionlistenertest ();

}

}

Another way to add an event implementation ActionListener

public class Buttonlistener implements ActionListener {

Private JFrame JF;

Public Buttonlistener () {

}

Public Buttonlistener (JFrame JF) {
THIS.JF = JF;
}

@Override
public void actionperformed (ActionEvent e) {
System.out.println ("Enter 123");
Object obj = E.getsource ();
JButton btn = (JButton) obj;
String text = Btn.gettext ();

if (Text.equals ("Red")) {
This.jf.getContentPane (). SetBackground (color.red);
}

if (Text.equals ("green")) {
This.jf.getContentPane (). SetBackground (Color.green);
}
}

}

2016.3.16 (Java graphical user interface)

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.