它是屏幕上window的对象,能够最大化、最小化、关闭,解决将Swing三大基本构造块
(按钮、标签、文本字段)组织起来,并把它们呈现给用户——(容器)
是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() {
@Override
public 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() {
@Override
public void actionPerformed(ActionEvent e) {
//如果被触发,则调用选择颜色函数段
chooseColor();
}
});
colorMenu.add(colorItem);
//设置线条粗细菜单
JMenu strokeMenu = new JMenu("Stroke");
JMenuItem strokeItem = new JMenuItem("Set Stroke");
strokeItem.addActionListener(new ActionListener() {
@Override
public 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() {
@Override
public 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() {
@Override
public 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() {
@Override
public 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() {
@Override
public 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();
}
}
由于时间关系,没能做完,只是半成。。。 以后有时间再继续完成把。