JFrame类

它是屏幕上window的对象,能够最大化、最小化、关闭,解决将Swing三大基本构造块

(按钮、标签、文本字段)组织起来,并把它们呈现给用户——(容器)

JPanel类

是java图形用户界面(GUI)工具包swing中的面板容器类,是一种轻量级容器,可以加入到Fram窗体中,其自身可以嵌套组合,在不同子容器中包含其他组件(Button,TextArea等)功能是对窗体上的这些控制进进行组合。

以下是部分代码实现:

package mini.draw.Pad;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;/*** 画图工具类* 1.创建一个窗体* 2.创建面图花板* 3.实现随笔画功能* 4.扩充菜单栏* 5.扩充性状态栏(按钮)*/
public class MiniDrawPad {private JFrame jFrame = new JFrame("迷你画图板");//带有标题和边框的顶层窗口//定义画图区域的高度和宽度private final int WIDTH = 1000;private final int HEIGHT = 700;private MyPanel mp = new MyPanel();//定义一个BufferedImage对象,并获取image对象的Graphics-画笔BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);//指定一个宽和高,创建一个不带透明色的对象Graphics2D g = (Graphics2D) image.getGraphics(); //获得在图像上画图的工具-画笔//设置画笔的颜色private Color color = Color.black; //当前默认颜色黑色//画笔的粗细private float stroke = 1.0f;//上一次鼠标拖动事件的鼠标坐标private int prex = -1;private int prey = -1;int R, G, B; //用来存放当前的色彩值String style1; //用来存放当前字体JLabel statusBar;//当前鼠标状态//创建画板类public class MyPanel extends JPanel {//设置画板public MyPanel() {setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));setBounds(0, 0, WIDTH, HEIGHT);setBackground(Color.WHITE);}public void paint(Graphics g) {g.drawImage(image, 0, 0, null);//img-要绘制的指定图像,如果为null,该方法不执行任何操作}}//选择颜色public void chooseColor() {color = JColorChooser.showDialog(jFrame, "选择颜色", color);R = color.getRed();G = color.getGreen();B = color.getBlue();color = new Color(R, G, B);}//获取画笔//创建菜单条public void createBar() {JMenuBar bar = new JMenuBar();//定义菜单条JMenu fileMenu = new JMenu("file");//fileMenu.setMnemonic('F');//表示助记符的键代码JMenuItem newItem = new JMenuItem("New");//菜单项newItem.setMnemonic('N');JMenuItem saveItem = new JMenuItem("Save");//菜单项saveItem.setMnemonic('S');JMenuItem openItem = new JMenuItem("Open");//菜单项openItem.setMnemonic('O');//退出菜单项JMenuItem exitItem = new JMenuItem("Exit");//菜单项exitItem.setMnemonic('E');exitItem.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.exit(0);}});fileMenu.add(newItem);fileMenu.add(saveItem);fileMenu.add(openItem);fileMenu.addSeparator();fileMenu.add(exitItem);//颜色菜单JMenu colorMenu = new JMenu("Color");JMenuItem colorItem = new JMenuItem("Choose Color");colorItem.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//如果被触发,则调用选择颜色函数段chooseColor();}});colorMenu.add(colorItem);//设置线条粗细菜单JMenu strokeMenu = new JMenu("Stroke");JMenuItem strokeItem = new JMenuItem("Set Stroke");strokeItem.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//如果被触发,则调用设置线条粗细的函数段setStroke();}});strokeMenu.add(strokeItem);//设置提示菜单JMenu helpMenu = new JMenu("Help");JMenuItem aboutItem = new JMenuItem("About this Draw pad!");aboutItem.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {JOptionPane.showMessageDialog(null, "this is a mini draw pad Copy(c)2020","画图板程序说明", JOptionPane.INFORMATION_MESSAGE);}});helpMenu.add(aboutItem);bar.add(fileMenu);bar.add(colorMenu);bar.add(strokeMenu);bar.add(helpMenu);jFrame.setJMenuBar(bar);}//设置线条粗细public void setStroke() {String input = JOptionPane.showInputDialog("请输入一个float数值( >0 )");stroke = Float.parseFloat(input);}//创建按钮public void createButton() {JToolBar buttonPanel = new JToolBar(JToolBar.HORIZONTAL);String[] buttonNames = {"随笔", "直线", "空心圆", "空心矩形", "橡皮擦", "文字按钮"};for (int i = 0; i < buttonNames.length; i++) {JButton button = new JButton(buttonNames[i]);buttonPanel.add(button);}//设置字体风格的选择String[] styleNames = {"宋体", "隶书", "楷体", "仿宋", "华文行楷", "方正舒体", "仿宋_GB2312", "Serif"};JComboBox styles = new JComboBox<>(styleNames);styles.setMaximumRowCount(6);  //设置JComboBox显示的最大行数styles.addItemListener(new ItemListener() {@Overridepublic void itemStateChanged(ItemEvent e) {style1 = styleNames[styles.getSelectedIndex()];}});styles.setMinimumSize(new Dimension(50, 20));styles.setMaximumSize(new Dimension(120, 20));JCheckBox bold = new JCheckBox("BOLD"); //复选框JCheckBox italic = new JCheckBox("ITALIC"); //buttonPanel.add(bold);buttonPanel.add(italic);buttonPanel.add(styles);statusBar = new JLabel("当前的鼠标状态");statusBar.setText("welcome minidraw pad!!!");Container c = jFrame.getContentPane();//初始化一个容器c.add(buttonPanel, BorderLayout.NORTH);c.add(statusBar, BorderLayout.SOUTH);}public void init() {/**对于鼠标的移动和拖放,用鼠标运动监听器MouseMotionListener*/mp.addMouseMotionListener(new MouseMotionAdapter() {@Overridepublic void mouseDragged(MouseEvent e) {if (prex > 0 && prey > 0) {g.setColor(color);g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));g.drawLine(prex, prey, e.getX(), e.getY());//x1,x2是确定直线的起始点,分别是横纵坐标,同理x2,y2确定直线的终点}prex = e.getX();prey = e.getY();statusBar.setText("Mouse Press @[" + e.getX() + "," + e.getY() + "]");mp.repaint(); //重绘mp对象}});jFrame.setBounds(0, 0, WIDTH, HEIGHT);jFrame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});g.fillRect(0, 0, WIDTH, HEIGHT);jFrame.add(mp);createBar();createButton();jFrame.setVisible(true);}public static void main(String[] args) {MiniDrawPad miniDrawPad = new MiniDrawPad();miniDrawPad.init();}}

画图工具-mini画板-编程知识网

 由于时间关系,没能做完,只是半成。。。 以后有时间再继续完成把。