十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
import java.awt.*;
成都创新互联是专业的潍城网站建设公司,潍城接单;提供成都网站建设、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行潍城网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
import java.awt.event.*;
import javax.swing.*;
public class Els extends JFrame implements KeyListener,Runnable
{
public Els()
{
setSize(240,320);
setUndecorated(true);
int H=(int) (this.getToolkit().getScreenSize().height);
int W=(int) (this.getToolkit().getScreenSize().width);
setLocation(W/2-120,H/2-160);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.darkGray.brighter().brighter());
//绘制小框格
for(int m=192;m=232;m+=10)
{
g2.drawLine(m, 52, m,92);
}
for(int m=52;m=92;m+=10)
{
g2.drawLine(192, m, 232, m);
}
//绘制大框格
for(int m=0;m=180;m+=15)
{
g2.drawLine(m, 0, m, 300);
}
for(int m=0;m=300;m+=15)
{
g2.drawLine(0, m, 180, m);
}
//将值不非0的格子涂黑
for(int i=1;i13;i++)
{
for(int j=0;j20;j++)
{
g2.setColor(Color.DARK_GRAY);
if (o[i][j]!=0) g2.fillRect(15*i-13, 15*j+2, 13, 13);
} }
//在小框格中绘制下一个的模型
switch(nextMODE)
{
case 0: g2.fillRect(204, 54, 8, 8);
g2.fillRect(204, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(204, 84, 8, 8);
break;
case 1: g2.fillRect(194, 64, 8, 8);
g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(224, 64, 8, 8);
break;
case 2: g2.fillRect(214, 64, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(214, 84, 8, 8);
g2.fillRect(204, 84, 8, 8);
break;
case 3: g2.fillRect(204, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(224, 74, 8, 8);
break;
case 4: g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(204, 84, 8, 8);
break;
case 5: g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(224, 64, 8, 8);
g2.fillRect(224, 74, 8, 8);
break;
case 6: g2.fillRect(204, 54, 8, 8);
g2.fillRect(204, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(214, 74, 8, 8);
break;
case 7: g2.fillRect(204, 74, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(224, 74, 8, 8);
g2.fillRect(224, 64, 8, 8);
break;
case 8: g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(214, 84, 8, 8);
break;
case 9: g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(224, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
break;
case 10: g2.fillRect(204, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(214, 84, 8, 8);
break;
case 11: g2.fillRect(204, 74, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(224, 64, 8, 8);
break;
case 12: g2.fillRect(214, 64, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(204, 84, 8, 8);
break;
case 13: g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(214, 74, 8, 8);
g2.fillRect(224, 74, 8, 8);
break;
case 14: g2.fillRect(204, 64, 8, 8);
g2.fillRect(214, 64, 8, 8);
g2.fillRect(204, 74, 8, 8);
g2.fillRect(214, 74, 8, 8);
break;
} g2.setColor(Color.black);
g2.drawLine(180, 0, 180, 300);
g2.setColor(Color.DARK_GRAY);
g2.drawString("LEVEL", 194, 10);
g2.drawString(""+LEVEL, 210, 28);
g2.drawString("NEXT", 196, 45);
g2.drawString("SCORE",193, 110);
g2.drawString(""+SCORE,205, 130);
g2.drawString("Xiong", 205, 160);
g2.drawString("Xuan", 205, 180);
}
//方块下落的方法
public void down()
{
Y++;
switch(MODE)
{
case 0: o[X+2][Y]=1;
o[X+2][Y+1]=1;
o[X+2][Y+2]=1;
o[X+2][Y+3]++;
o[X+2][Y-1]=0;
break;
case 1: o[X][Y+1]++;
o[X+1][Y+1]++;
o[X+2][Y+1]++;
o[X+3][Y+1]++;
o[X][Y]=0;
o[X+1][Y]=0;
o[X+2][Y]=0;
o[X+3][Y]=0;
break;
case 2: o[X+2][Y]=1;
o[X+2][Y+1]=1;
o[X+2][Y+2]++;
o[X+1][Y+2]++;
o[X+2][Y-1]=0;
o[X+1][Y+1]=0;
break;
case 3: o[X][Y]=1;
o[X][Y+1]++;
o[X+1][Y+1]++;
o[X+2][Y+1]++;
o[X][Y-1]=0;
//o[X][Y]=0;
o[X+1][Y]=0;
o[X+2][Y]=0;
break;
case 4: o[X+1][Y]=1;
o[X+2][Y]++;
o[X+1][Y+1]=1;
o[X+1][Y+2]++;
o[X+1][Y-1]=0;
o[X+2][Y-1]=0;
break;
case 5: o[X][Y]++;
o[X+1][Y]++;
o[X+2][Y]=1;
o[X+2][Y+1]++;
o[X][Y-1]=0;
o[X+1][Y-1]=0;
o[X+2][Y-1]=0;
break;
case 6: o[X+1][Y]=1;
o[X+1][Y+1]=1;
o[X+1][Y+2]++;
o[X+2][Y+2]++;
o[X+1][Y-1]=0;
//o[X+1][Y]=0;
//o[X+1][Y+1]=0;
o[X+2][Y+1]=0;
break;
case 7: o[X][Y+1]++;
o[X+1][Y+1]++;
o[X+2][Y+1]++;
o[X+2][Y]=1;
o[X][Y]=0;
o[X+1][Y]=0;
//o[X+2][Y]=0;
o[X+2][Y-1]=0;
break;
case 8: o[X+1][Y]++;
o[X+2][Y]=1;
o[X+2][Y+1]=1;
o[X+2][Y+2]++;
o[X+1][Y-1]=0;
o[X+2][Y-1]=0;
//o[X+2][Y]=0;
//o[X+2][Y+1]=0;
break;
case 9: o[X+1][Y]=1;
o[X+2][Y]++;
o[X+3][Y]++;
o[X+1][Y+1]++;
o[X+1][Y-1]=0;
o[X+2][Y-1]=0;
o[X+3][Y-1]=0;
//o[X+1][Y]=0;
break;
case 10:o[X+1][Y]=1;
o[X+1][Y+1]++;
o[X+2][Y+1]=1;
o[X+2][Y+2]++;
o[X+1][Y-1]=0;
//o[X+1][Y]=0;
o[X+2][Y]=0;
//o[X+2][Y+1]=0;
break;
case 11:o[X+2][Y]=1;
o[X+3][Y]++;
o[X+1][Y+1]++;
o[X+2][Y+1]++;
o[X+2][Y-1]=0;
o[X+3][Y-1]=0;
o[X+1][Y]=0;
//o[X+2][Y]=0;
break;
case 12:o[X+1][Y+1]=1;
o[X+2][Y+1]++;
o[X+1][Y+2]++;
o[X+2][Y]=1;
o[X+1][Y]=0;
//o[X+2][Y]=0;
//o[X+1][Y+1]=0;
o[X+2][Y-1]=0;
break;
case 13:o[X+1][Y]++;
o[X+2][Y]=1;
o[X+2][Y+1]++;
o[X+3][Y+1]++;
o[X+1][Y-1]=0;
o[X+2][Y-1]=0;
//o[X+2][Y]=0;
o[X+3][Y]=0;
break;
case 14:o[X+1][Y]=1;
o[X+2][Y]=1;
o[X+1][Y+1]++;
o[X+2][Y+1]++;
o[X+1][Y-1]=0;
o[X+2][Y-1]=0;
//o[X+1][Y]=0;
//o[X+2][Y]=0;
break;
}
//如果碰撞,则自动上升一格
for(int i=0;i13;i++)
{
for(int j=0;j21;j++)
{
if (o[i][j]==2) up();
}
}
}
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class CalculatorGUI {
private Frame f;
private Panel p1, p2;
private Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private Button bPoint, bAdd, bDec, bMul, bDiv, bCal;
private TextField tf;
private String s, op;
private Calculator cal = new Calculator();
private boolean ifOp;
public CalculatorGUI() {
f = new Frame("Calculator");
p1 = new Panel();
p2 = new Panel();
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bPoint = new Button(".");
bAdd = new Button("+");
bDec = new Button("-");
bMul = new Button("*");
bDiv = new Button("/");
bCal = new Button("=");
tf = new TextField(25);
tf.setEditable(false);
}
public void launchFrame() {
f.setSize(220, 160);
f.setResizable(false);
f.addWindowListener(new myWindowListener());
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(tf);
f.add(p1, BorderLayout.NORTH);
p2.setLayout(new GridLayout(4, 4));
b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
b2.addActionListener(new setLabelText_ActionListener());
b3.addActionListener(new setLabelText_ActionListener());
b4.addActionListener(new setLabelText_ActionListener());
b5.addActionListener(new setLabelText_ActionListener());
b6.addActionListener(new setLabelText_ActionListener());
b7.addActionListener(new setLabelText_ActionListener());
b8.addActionListener(new setLabelText_ActionListener());
b9.addActionListener(new setLabelText_ActionListener());
bPoint.addActionListener(new setLabelText_ActionListener());
bAdd.addActionListener(new setOperator_ActionListener());
bDec.addActionListener(new setOperator_ActionListener());
bMul.addActionListener(new setOperator_ActionListener());
bDiv.addActionListener(new setOperator_ActionListener());
bCal.addActionListener(new setOperator_ActionListener());
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bAdd);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bDec);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMul);
p2.add(b0);
p2.add(bPoint);
p2.add(bCal);
p2.add(bDiv);
f.add(p2, BorderLayout.SOUTH);
f.setVisible(true);
}
public void setTextFieldText_Temp() {
if (tf.getText().length() 15
(tf.getText().indexOf(".") == -1 || !s.equals("."))) {
tf.setText(tf.getText() + s);
} else {
tf.setText((tf.getText() + s).substring(0, 15));
}
}
public void setTextFieldText() {
if (ifOp) {
ifOp = false;
tf.setText("");
setTextFieldText_Temp();
} else {
setTextFieldText_Temp();
}
}
public static void main(String[] args) {
CalculatorGUI calculator = new CalculatorGUI();
calculator.launchFrame();
}
class myWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
class setLabelText_ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button tempB = (Button) e.getSource();
s = tempB.getLabel();
setTextFieldText();
}
}
class setOperator_ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button tempB = (Button) e.getSource();
op = tempB.getLabel();
if (op.equals("+")) {
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
} else if (op.equals("-")) {
tf.setText(cal.opSubtract(tf.getText()));
ifOp = true;
} else if (op.equals("*")) {
tf.setText(cal.opMultiply(tf.getText()));
ifOp = true;
} else if (op.equals("/")) {
tf.setText(cal.opDivide(tf.getText()));
ifOp = true;
} else if (op.equals("=")) {
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
}
}
}
}
class Calculator {
private String result = "0";
private int op = 0, add = 1, sub = 2, mul = 3, div = 4;
private double stringToDouble(String x) {
double y = Double.parseDouble(x);
return y;
}
private void operate(String x) {
double x1 = stringToDouble(x);
double y = stringToDouble(result);
switch (op) {
case 0:
result = x;
break;
case 1:
result = String.valueOf(y + x1);
break;
case 2:
result = String.valueOf(y - x1);
break;
case 3:
result = String.valueOf(y * x1);
break;
case 4:
if (x1 != 0) {
result = String.valueOf(y / x1);
} else {
result = "The divisor can't be zero!";
}
break;
}
}
public String opAdd(String x) {
operate(x);
op = add;
return result;
}
public String opSubtract(String x) {
operate(x);
op = sub;
return result;
}
public String opMultiply(String x) {
operate(x);
op = mul;
return result;
}
public String opDivide(String x) {
operate(x);
op = div;
return result;
}
public String opEquals(String x) {
operate(x);
op = 0;
return result;
}
public void opClean() {
op = 0;
result = "0";
}
}
直线 是 Line2D
矩形是 Rectangle2D
弧 Arc2D
椭圆 Ellipse2D
圆角矩形是 RoundRectangle2D
上面的都在 java.awt.geom包里
图片看起来很模糊,隐约看到需要一个登录窗口,那就分享一下以前练习的登录窗口demo吧。
先上效果图:
登录界面
源码如下:
AbsoluteLoginFrame.java
public class AbsoluteLoginFrame extends JFrame {
private static final int LOGIN_WIDTH = 600;
private static final int LOGIN_HEIGHT = 400;
private static final long serialVersionUID = -2381351968820980500L;
public AbsoluteLoginFrame(){
//设置窗口标题
setTitle("登录界面");
//设置一个初始面板,填充整个窗口
JPanel loginPanel = new JPanel();
//设置背景颜色
loginPanel.setBackground(new Color(204, 204, 204));//#CCC
loginPanel.setLayout(null);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.WHITE);
centerPanel.setBounds(114, 70, 360, 224);
centerPanel.setLayout(null);
JLabel jLabel = new JLabel("用户名:");
jLabel.setOpaque(true);
jLabel.setBackground(Color.YELLOW);
jLabel.setBounds(60, 60, 54, 20);
JLabel label = new JLabel("密 码:");
label.setOpaque(true);
label.setBackground(Color.CYAN);
label.setBounds(60, 90, 54, 20);
JTextField textField = new JTextField(15);
textField.setBounds(130, 60, 166, 21);
JPasswordField passwordField = new JPasswordField(15);
passwordField.setBounds(130, 90, 166, 21);
JButton jButton = new JButton("登录");
jButton.setBounds(148, 120, 62, 28);
centerPanel.add(jLabel);
centerPanel.add(label);
centerPanel.add(textField);
centerPanel.add(jButton);
centerPanel.add(passwordField);
loginPanel.add(centerPanel);
getContentPane().add(loginPanel);//将初始面板添加到窗口中
setSize(LOGIN_WIDTH, LOGIN_HEIGHT);//设置窗口大小
setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT));//设置窗口位置
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置窗口默认关闭方式
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new AbsoluteLoginFrame();
}
}
Screen.java
public class Screen {
private int width;
private int height;
public Screen(){
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
this.width = screenSize.width;
this.height = screenSize.height;
}
public static Point getCenterPosition(int width, int height){
Screen screen = new Screen();
int x = (screen.getWidth() - width) / 2;
int y = (screen.getHeight() - height) / 2;
return new Point(x, y);
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}