[JAVA窗][作] 小家-第一段布局@Morris' Blog|PChome Online 人新台
2012-09-30 14:12:39| 人20,822| 回0 | 上一篇 | 下一篇

[JAVA窗][作] 小家-第一段布局

0 收藏 0 0 站台



1、功能描述:

1.      Layout(工具、布、列)

2.      利用Combo Box做工具(刷、直、形、矩形、角矩形)

3.      利用Radio Button做刷大小(大、中、小)

4.      利用Check Box 做是否填

5.      Button(前景色、背景色、清除面

尤       6.    游位置(在列示X, Y座)

     7.      做工具、刷大小、是否填、按的Action Listener

(後跳出息窗,示被物件的名字





室友作, 拿玩玩也不 ?!

善用息窗
JOptionPane.showMessageDialog(null, "你了:" + s, "息",     JOptionPane.INFORMATION_MESSAGE);
傻傻的跑去一介面出, 我真的傻了

Demo 影片


先前的版本好像太零了, 因此我改一下

package paintFrame;

import javax.swing.*;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class pFrame extends JFrame {
    JLabel stateText = new JLabel("游位置 : 布外");
    JPanel toolPanel = new JPanel();
    Color[][] board = new Color[700][750];
    Color using = Color.black;
    MsgListener listener = new MsgListener();

    public pFrame() {
        this.setTitle("小家");
        this.setSize(700, 760);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.setResizable(false);

        stateText.setFont(new Font("", Font.PLAIN, 16));
        toolPanel.setLayout(new GridLayout(11, 1));
        add(stateText, BorderLayout.SOUTH);
        add(toolPanel, BorderLayout.WEST);

        JLabel drawTool = new JLabel("[工具]");
        JLabel brushSize = new JLabel("[刷大小]");
        String[] items = { "刷", "直", "形", "矩形", "角矩形" };
        JComboBox toolBox = new JComboBox(items);
        toolBox.addItemListener(new BoxListener());
        toolPanel.add(drawTool);
        toolPanel.add(toolBox);
        toolPanel.add(brushSize);

        JRadioButton smallSize = new JRadioButton("小");
        JRadioButton mediumSize = new JRadioButton("中");
        JRadioButton largerSize = new JRadioButton("大");
        ButtonGroup sizeGroup = new ButtonGroup();
        smallSize.addActionListener(listener);
        mediumSize.addActionListener(listener);
        largerSize.addActionListener(listener);
        smallSize.setSelected(true);
        toolPanel.add(smallSize);
        toolPanel.add(mediumSize);
        toolPanel.add(largerSize);
        sizeGroup.add(smallSize);
        sizeGroup.add(mediumSize);
        sizeGroup.add(largerSize);

        JCheckBox fullBox = new JCheckBox("填");
        fullBox.addActionListener(listener);
        JButton foreground = new JButton("前景色");
        foreground.setBackground(Color.white);
        JButton background = new JButton("背景色");
        background.setBackground(Color.black);
        JButton clearScr = new JButton("清除面");
        clearScr.addActionListener(listener);
        clearScr.setActionCommand("clear");
        toolPanel.add(fullBox);
        toolPanel.add(foreground);
        toolPanel.add(background);
        toolPanel.add(clearScr);
        addMouseMotionListener(new MouseMotion());
        Ing = 0;
        Init = 0;
        penSize = 3;
    }

    int mox, moy, nex, ney;
    int Ing, Init, penSize;

    public void paint(Graphics g) {
        int x, y;
        stateText.setText("游位置 : (" + (mox - 100) + "," + (moy - 30)
                + ") (拖曳)");
        if (Init == 0) {
            stateText.paint(stateText.getGraphics());
            toolPanel.paint(toolPanel.getGraphics());
            for (x = 110; x < 700; x++) {
                for (y = 0; y < 730; y++) {
                    board[x][y] = Color.white;
                }
                g.setColor(Color.white);
                g.drawLine(x, 0, x, 730);
            }
            Init = 1;
        } else {
            g.setColor(board[mox][moy]);
            g.drawLine(mox, moy, nex, ney);
            g.drawLine(mox + 1, moy + 1, nex + 1, ney + 1);
            g.drawLine(mox + 1, moy, nex + 1, ney);
            mox = nex;
            moy = ney;
        }
    }

    class MsgListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {           
            if(e.getActionCommand() == "clear") {
                Init = 0;
                repaint();
                return;
            }
 nbsp;          JOptionPane.showMessageDialog(null, "你了  :  " + e.getActionCommand(),
                    "息", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    class BoxListener implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            String s = (String) e.getItem();
            if (e.getStateChange() == ItemEvent.SELECTED) {
                JOptionPane.showMessageDialog(null, "你了  :  " + s, "息",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }

    class MouseMotion extends MouseMotionAdapter {
        public void mouseDragged(MouseEvent e) {
            nex = e.getX();
            ney = e.getY();
            if (nex > 110 && ney < 730 && nex < 700) {
                if(mox <= 110 || moy >= 730 || mox >= 700) {
                    mox = nex; moy = ney;
                }
                board[nex][ney] = using;
                repaint();
            } else {
                stateText.setText("游位置  : 布外");
            }
        }

        public void mouseMoved(MouseEvent e) {
            mox = e.getX();
            moy = e.getY();
            if (mox > 110 && mox < 730 && mox < 700) {
                stateText.setText("游位置 : (" + (mox - 100) + "," + (moy - 30)
                        + ") (停留)");
            } else {
                stateText.setText("游位置  : 布外");
            }
        }
    }

    public static void main(String[] args) {
        pFrame gui = new pFrame();
        gui.setVisible(true);
    }
}

台: Morris
人(20,822) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: []Java |
此分下一篇:[] 打包 JAR 案
此分上一篇:[JAVA][Eclipse] Diamond Dash 外

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86