6*17 Lattice window program, written in Java.

Source: Internet
Author: User

Package Com.wulala;

Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import java.awt.Dimension;
Import Java.awt.GridLayout;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;

Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.JTextArea;
Import Javax.swing.JTextField;

public class Swingcomponent {

public static int Total_dot = 6 * 17;
public Boolean ifpressed = false;

LIGHTBTN lightbtns[];
JFrame frame = new JFrame ("6x17 lattice");

JButton Clearbutton = new JButton ("Empty");
JButton Getresultbutton = new JButton ("computed result array");
JTextArea TextField = new JTextArea ();
Public JFrame JF = new JFrame ("BorderLayout");

public static void ShowMe (JFrame JF) {
Jf.setdefaultcloseoperation (Jframe.do_nothing_on_close);
Jf.setsize (300, 300);
Jf.setvisible (TRUE);
}

public void Setborderlayout () {

Jf.setlayout (New BorderLayout ());//BorderLayout is the default layout manager of the Swing container, which is meant to be laid out in 5 directions in the east and the south, where components can be placed on each of these orientations.
Jf.add (New JButton ("East"), Borderlayout.east);
Jf.add (New JButton ("South"), Borderlayout.south);
Jf.add (New JButton ("West"), Borderlayout.west);
Jf.add (New JButton ("North"), Borderlayout.north);
Jf.add (New JButton ("center"), Borderlayout.center);
SWINGCOMPONENT.SHOWME (JF);
}

public void init () {
Lightbtns = new Lightbtn[total_dot];
Bondlisterner4button ();
Textfield.setlinewrap (TRUE);
Frame.setlayout (New BorderLayout ());
JPanel Eastpanel = new JPanel ();
JPanel Westpanel = new JPanel ();
Eastpanel.setlayout (New BorderLayout ());
Westpanel.setlayout (New GridLayout (6, 17));
for (int i = 0; i < Total_dot; i++) {
lightbtn button = new lightbtn ();
Button.setbackground (Color.Black);
Lightbtns[i] = button;
Westpanel.add (button);
}

Button.setsize (100, 30);
Eastpanel.add (Clearbutton, Borderlayout.north);
Eastpanel.add (Getresultbutton, Borderlayout.south);
Eastpanel.add (New JButton ("Empty"));
Textfield.setsize (300,300);
Eastpanel.add (TextField, Borderlayout.center);
Eastpanel.setbackground (Color.Black);
Eastpanel.setpreferredsize (New Dimension (300, 150));
Frame.add (Eastpanel, borderlayout.east);
Frame.add (Westpanel, Borderlayout.center);
Frame.setsize (900, 300);
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
Frame.setlocationrelativeto (NULL);
Frame.setlocation (400, 400);
Frame.setresizable (FALSE);
Frame.pack ();
Frame.setvisible (TRUE);
}

private void Bondlisterner4button () {
Getresultbutton.addmouselistener (New MouseListener () {

@Override
public void mouseclicked (MouseEvent e) {
String resultstr = "";
String linestr = "[";
for (int i = 0; i < Total_dot; i++) {

int bit = 0;
if (lightbtns[i].ifwhite) {
bit = 1;
}
Linestr = linestr + bit + ",";
if ((i%) = = && (i > 0)) {
Linestr = linestr.substring (0, Linestr.length ()-1);
ResultStr = resultstr + linestr + "]\n";
LINESTR = "[";
}
}
Textfield.settext (RESULTSTR);
}

@Override
public void mouseentered (MouseEvent e) {
}

@Override
public void mouseexited (MouseEvent e) {
}

@Override
public void mousepressed (MouseEvent e) {
}

@Override
public void mousereleased (MouseEvent e) {
}

});

Clearbutton.addmouselistener (New MouseListener () {

@Override
public void mouseclicked (MouseEvent e) {
for (int i = 0; i < Total_dot; i++) {
Lightbtns[i].turnoff ();
}
Textfield.settext ("");
}

@Override
public void mouseentered (MouseEvent e) {
}

@Override
public void mouseexited (MouseEvent e) {
}

@Override
public void mousepressed (MouseEvent e) {
}

@Override
public void mousereleased (MouseEvent e) {
}

});
}

public static void Main (string[] args) {
Swingcomponent sc = new swingcomponent ();
Sc.init ();
Sc.setborderlayout ();
}

Class Lightbtn extends JButton implements MouseListener {

public Boolean ifwhite = false;

public void turnoff () {
Ifwhite = false;
This.setbackground (Color.Black);
}

public void Switchbackgroundcolor () {

if (Ifwhite = = True) {
This.setbackground (Color.Black);
Ifwhite = false;
} else {
This.setbackground (Color.White);
Ifwhite = true;
}
}

Public lightbtn () {
This.addmouselistener (this);
}

@Override
public void mouseclicked (MouseEvent arg0) {
System.out.println ("clicked");
Switchbackgroundcolor ();
}

@Override
public void mouseentered (MouseEvent arg0) {
if (ifpressed) {
Switchbackgroundcolor ();
}
}

@Override
public void mouseexited (MouseEvent arg0) {

}

@Override
public void mousepressed (MouseEvent arg0) {
Ifpressed = true;
}

@Override
public void mousereleased (MouseEvent arg0) {
ifpressed = false;
}
}
}

6*17 Lattice window program, written in Java.

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.