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("6×17点阵");
JButton clearButton = new JButton("清空");
JButton getResultButton = new JButton("计算结果数组");
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是Swing容器的默认布局管理器,它的含义是采用东南西北中5个方位来进行布局,可以分别往这些方位上放置组件。
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("清空"));
// 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 % 17) == 16 && (i > 0)) {
lineStr = lineStr.substring(0, lineStr.length() - 1);
resultStr = resultStr + lineStr + "]
";
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;
}
}
}