Java笔记:边框布局,功能面板,窗口内容面板颜色的控制方法,线条的颜色及宽度控制,窗口多个JPanel线条偏移问题的解决方法,鼠标运动监听器的使用
概述
本文旨在记录Java代码初学者的学习历程并表达本人对代码的理解。
本文的内容包括:边框布局的使用方法,功能面板的设置方法,窗口内容面板颜色的控制方法,Graphics类数据颜色及宽度的控制,窗口多个JPanel线条偏移问题的解决方法。
1.边框布局
边框布局的具体形式如上,且NORTH和SOUTH无法控制宽度,WEST和EAST无法控制长度,CENTER无法控制大小,并且每一块区域仅能放置一个组件,放置多个组件仅显示最后一个。
JFrame的默认布局即是边框布局。设置边框布局的方法和将组件添加到要求位置的方法如下:
jframe.setLayout(new BorderLayout()); jframe.add(jpanel,BorderLayout.NORTH);//这里的NORTH也可换为其它位置2.功能面板
我们需要让线条不遮挡按钮,因此需要用JPanel将窗口分为两个区域,同时,JPanel也相当于容器,可以将多个组件放在边框布局的一个位置。这两个区域都可以叫功能面板。
JPanel jpanel=new JPanel(); jpanel.setBackground(Color.BLUE);//设置面板的颜色 jpanel.setPreferredSize(new Dimension(0,40));//设置面板的大小 jframe.add(jpanel,BorderLayout.NORTH);//将这个面板添加到窗口的NORTH部分。 JButton jbutton=new JButton("直线"); JButton jbutton2=new JButton("矩形"); JButton jbutton3=new JButton("等腰三角形"); JButton jbutton4=new JButton("任意三角形"); JButton jbutton5=new JButton("多边形"); JButton jbutton6=new JButton("曲线"); JButton jbutton7=new JButton("橡皮擦"); JButton Red =new JButton("红色"); jpanel.add(jbutton); jpanel.add(jbutton2); jpanel.add(jbutton3); jpanel.add(jbutton4); jpanel.add(jbutton5); jpanel.add(jbutton6); jpanel.add(jbutton7); jpanel.add(Red);//将多个按钮添加到这个面板 JPanel jpanel2=new JPanel(); jpanel2.setBackground(Color.WHITE); jframe.add(jpanel2,BorderLayout.CENTER); jpanel2.setPreferredSize(new Dimension(600,560));//第二个面板用于画线条 jframe.setVisible(true);因为我们要在第二个组件上画线,所以我们的画笔需要改为从第二个面板取得。
3.窗口内容面板的颜色设置
代码如下:
jframe.getContentPane().setBackground(new Color(255,255,255));//若写成setBackground则仅会改变边框颜色4.线条颜色及宽度的控制
代码如下:
gr.setColor(Color.BLACK);//设置线条颜色 Graphics2D gr2=(Graphics2D) gr;//将Graphics类的gr强制转变为Graphics2D gr2.setStroke(new BasicStroke(2));//对于Graphics2D的gr2可设置线条宽度5.窗口多个JPanel线条偏移问题的解决方法
本质上是因为坐标不对导致的,我们仅需要将鼠标监听器和鼠标运动监听器都绑定在第二个面板,即可让e.getX()所得到的坐标正确。
jpanel2.addMouseListener(drawListener); jpanel2.addMouseMotionListener(drawListener);6.鼠标运动监听器的使用
其接口名为MouseMotionListener,其中有两个方法体
public void mouseDragged(MouseEvent e){} public void mouseMoved(MouseEvent e){}第一个是鼠标按住并移动时生效,第二个是鼠标移动时触发。
7.代码展示
import javax.swing.*; import java.awt.*; public class DrawSoftWare { public void drawer(){ JFrame jframe=new JFrame(); jframe.setSize(700,600);//窗口大了按钮会消失 jframe.setTitle("Draw SoftWare"); jframe.setDefaultCloseOperation(3); jframe.setLocationRelativeTo(null); jframe.getContentPane().setBackground(new Color(255,255,255)); JPanel jpanel=new JPanel(); jframe.setLayout(new BorderLayout()); jpanel.setBackground(Color.BLUE); jpanel.setPreferredSize(new Dimension(0,40)); jframe.add(jpanel,BorderLayout.NORTH); JButton jbutton=new JButton("直线"); JButton jbutton2=new JButton("矩形"); JButton jbutton3=new JButton("等腰三角形"); JButton jbutton4=new JButton("任意三角形"); JButton jbutton5=new JButton("多边形"); JButton jbutton6=new JButton("曲线"); JButton jbutton7=new JButton("橡皮擦"); JButton Red =new JButton("红色"); jpanel.add(jbutton); jpanel.add(jbutton2); jpanel.add(jbutton3); jpanel.add(jbutton4); jpanel.add(jbutton5); jpanel.add(jbutton6); jpanel.add(jbutton7); jpanel.add(Red); jframe.setVisible(true); JPanel jpanel2=new JPanel(); jpanel2.setBackground(Color.WHITE); jframe.add(jpanel2,BorderLayout.CENTER); jpanel2.setPreferredSize(new Dimension(600,560)); jframe.setVisible(true); Graphics g=jpanel2.getGraphics(); DrawListener drawListener=new DrawListener(); jpanel2.addMouseListener(drawListener); jpanel2.addMouseMotionListener(drawListener); drawListener.gr=g; jbutton.addActionListener(drawListener); jbutton2.addActionListener(drawListener); jbutton3.addActionListener(drawListener); jbutton4.addActionListener(drawListener); jbutton5.addActionListener(drawListener); jbutton6.addActionListener(drawListener); jbutton7.addActionListener(drawListener); Red.addActionListener(drawListener); } public static void main(String[] args){ DrawSoftWare d=new DrawSoftWare(); d.drawer(); } }import java.awt.*; import java.awt.event.*; public class DrawListener implements MouseListener, ActionListener, MouseMotionListener{ public int x1,y1,x2,y2,x3,y3,x4,y4; public static Graphics gr ; public String Command; public String color; public boolean flag; public void actionPerformed(ActionEvent e) { color=e.getActionCommand(); if(!color.equals("红色")&&!color.equals("橡皮擦")){ Command = e.getActionCommand(); } if(color.equals("红色")){ gr.setColor(Color.RED); } if(color.equals("橡皮擦")){ gr.setColor(Color.WHITE); Graphics2D gr2=(Graphics2D) gr; gr2.setStroke(new BasicStroke(8)); } flag = true; System.out.println(Command); System.out.println(color); } public void mousePressed(MouseEvent e){ if(flag==true){ x1=e.getX(); y1=e.getY(); } if(!color.equals("红色")&&!color.equals("橡皮擦")){ gr.setColor(Color.BLACK); Graphics2D gr2=(Graphics2D) gr; gr2.setStroke(new BasicStroke(2)); } } public void mouseReleased(MouseEvent e) { if (flag == true) { x2 = e.getX(); y2 = e.getY(); } if (Command.equals("直线")) { gr.drawLine(x1, y1, x2, y2);//直线 System.out.println("x1"+x1+"y1"+y1+"x2"+x2+"y2"+y2); } if (Command.equals("矩形")) { int wid = Math.abs(x2 - x1); int hei = Math.abs(y2 - y1); int xMin = Math.min(x1, x2); int yMin = Math.min(y1, y2); gr.drawRect(xMin, yMin, wid, hei);//矩形 } if (Command.equals("等腰三角形")) { gr.drawLine(x1, y1, x2, y1); int xMid = Math.abs((x2 + x1) / 2); gr.drawLine(x1, y1, xMid, y2); gr.drawLine(x2, y1, xMid, y2); } if (Command.equals("任意三角形")) { gr.drawLine(x1, y1, x2, y2); flag = false; } if (Command.equals("多边形")&&flag) { gr.drawLine(x1, y1, x2, y2); flag = false; } } public void mouseClicked(MouseEvent e){ x3=e.getX(); y3=e.getY(); if(Command.equals("任意三角形")){ gr.drawLine(x1, y1, x3, y3); gr.drawLine(x2, y2, x3, y3); flag=true; } if(Command.equals("多边形")){ gr.drawLine(x2, y2, x3, y3); x2=x3; y2=y3; int times=e.getClickCount(); if (times==2){ gr.drawLine(x1, y1, x3, y3); flag=true; } } } public void mouseDragged(MouseEvent e){ if(Command.equals("曲线")){ x4=e.getX(); y4=e.getY(); gr.drawLine(x1, y1, x4, y4); x1=x4; y1=y4; } if(color.equals("橡皮擦")){ x4=e.getX(); y4=e.getY(); gr.drawLine(x1, y1, x4, y4); x1=x4; y1=y4; } } public void mouseMoved(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } }除前面提到的需求,我们还额外增加了橡皮擦和曲线功能。
