在jpanel上画图,怎么不覆盖原有jpanel中的组件
先new一个jpanel,在里面添加一个JMenuBar组件(保存、新建之类的),然后再画图。可是老是覆盖掉原有的jpanel。...
先new一个jpanel,在里面添加一个JMenuBar组件(保存、新建之类的),然后再画图。可是老是覆盖掉原有的jpanel。
展开
2个回答
展开全部
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DrawPanel extends JFrame implements ActionListener,MouseListener,MouseMotionListener{
private static final long serialVersionUID = 6748004371807231054L;
public static final int COUNT = 5;
public static final int READY = 0;//准备好了
public static final int START = 1;// 开始
public static final int DRAWING = 2;// 画线中
public static final int COMPLETE = 3;// 完成
public JPopupMenu rightMenu;// 右键弹出菜单
public JPanel toolPanel;// 工具栏
public JPanel contentPanel;// 内容面板
protected int statue;
protected Point currentPoint;//检测点
protected Point mousePoint;//目前鼠标所在的店
protected ArrayList<Point> routes = new ArrayList<Point>(); // 鼠标的路径
JMenuItem newItem,compItem, continueItem,clearItem;
JMenuBar menuBar;
JMenu fileMenu;
JMenuItem createItem,saveItem,exitItem;
public DrawPanel() {
init();
}
private void init(){
statue = DrawPanel.READY;
this.setLayout(new BorderLayout());
//菜单栏
menuBar = new JMenuBar();
fileMenu = new JMenu("文件");
createItem = new JMenuItem("新建");
createItem.addActionListener(this);
saveItem = new JMenuItem("保存");
saveItem.addActionListener(this);
exitItem = new JMenuItem("退出");
exitItem.addActionListener(this);
fileMenu.add(createItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
menuBar.add(fileMenu);
this.setJMenuBar(menuBar);
//弹出菜单
rightMenu = new JPopupMenu();
currentPoint = new Point(-100, -100);
newItem = new JMenuItem("新建");
setJMenuItem(newItem);
compItem = new JMenuItem("完成");
setJMenuItem(compItem);
continueItem = new JMenuItem("继续");
setJMenuItem(continueItem);
clearItem = new JMenuItem("清屏");
setJMenuItem(clearItem);
rightMenu.add(compItem);
rightMenu.addSeparator();
rightMenu.add(newItem);
rightMenu.add(continueItem);
rightMenu.addSeparator();
rightMenu.add(clearItem);
//画线的面板
contentPanel = new JPanel();
this.getContentPane().add(contentPanel);
contentPanel.addMouseListener(this);
contentPanel.addMouseMotionListener(this);
}
public void paint(Graphics g) {
super.paint(g);
if (g instanceof Graphics2D) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
g.setColor(Color.white);
g.fillRect(0, this.getHeight()-contentPanel.getHeight(), contentPanel.getWidth(), contentPanel.getHeight());
g.setColor(Color.black);
if (routes.size() >= 2) {
//画线
for (int i = 0; i < routes.size() - 1; i++) {
g.drawLine(routes.get(i).x, routes.get(i).y,
routes.get(i + 1).x, routes.get(i + 1).y);
}
}
if(statue == DrawPanel.DRAWING){
if(mousePoint != null){
if(routes.size() == 1){
g.drawLine(routes.get(0).x, routes.get(0).y, mousePoint.x, mousePoint.y);
}else if(routes.size()>1){
g.drawLine(routes.get(routes.size()-1).x,routes.get(routes.size()-1).y, mousePoint.x, mousePoint.y);
}
}
}
if (statue == DrawPanel.COMPLETE) {
//画出所有点
for (int i = 0; i < routes.size(); i++) {
g.fillOval(routes.get(i).x, routes.get(i).y, 2, 2);
}
//画出当前点
g.setColor(Color.red);
g.drawOval(currentPoint.x - 1, currentPoint.y - 1, 2, 2);
g.drawOval(currentPoint.x - 2, currentPoint.y - 2, 4, 4);
g.drawString("当前点坐标:("+currentPoint.x+","+currentPoint.y+")",currentPoint.x,currentPoint.y);
}
}
//菜单项事件绑定
public void setJMenuItem(final JMenuItem item) {
item.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
if(item.equals(newItem)){
rightMenu.setVisible(false);
statue = DrawPanel.START;
routes.removeAll(routes);
repaint();
}else if (item.equals(compItem)) {
rightMenu.setVisible(false);
statue = DrawPanel.COMPLETE;
repaint();
}else if(item.equals(continueItem)){
rightMenu.setVisible(false);
statue = DrawPanel.DRAWING;
repaint();
}else if(item.equals(clearItem)){
rightMenu.setVisible(false);
statue = DrawPanel.START;
routes.removeAll(routes);
repaint();
}
}
public void mouseEntered(MouseEvent arg0) {
item.setArmed(true);
}
public void mouseExited(MouseEvent arg0) {
item.setArmed(false);
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
}
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3) {
rightMenu.setLocation(e.getLocationOnScreen());
rightMenu.setVisible(true);
}
if(statue == DrawPanel.READY){
return;
}
if (e.getButton() == MouseEvent.BUTTON1) {
Point curPoint = e.getPoint();
routes.add(new Point(curPoint.x,curPoint.y+this.getHeight()-contentPanel.getHeight()));
statue = DrawPanel.DRAWING;
repaint();
}
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseDragged(MouseEvent e) { }
public void mouseMoved(MouseEvent e) {
if(statue == DrawPanel.DRAWING){
mousePoint = new Point(e.getPoint().x,e.getPoint().y+this.getHeight()-contentPanel.getHeight());
repaint();
}
if (statue == DrawPanel.COMPLETE) {
Point np = e.getPoint();
np = new Point(np.x,np.y+this.getHeight()-contentPanel.getHeight());
for(Point p : routes){
if(checkInRect(p,np)){
currentPoint = p;
repaint();
break;
}else{
currentPoint = new Point(-100,-100);
}
}
}
}
/**
* 范围检测
* @param p1
* @param p2
* @return
*/
private boolean checkInRect(Point p1,Point p2){
if(((p2.getX()>p1.getX()-5)&&(p2.getX()<p1.getX()+5))&&
((p2.getY()>p1.getY()-5)&&(p2.getY()<p1.getY()+5)))
{
return true;
}
return false;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(createItem)){
statue = DrawPanel.START;
routes.removeAll(routes);
repaint();
}else if(e.getSource().equals(saveItem)){
statue = DrawPanel.COMPLETE;
repaint();
}else if(e.getSource().equals(exitItem)){
int select = JOptionPane.showConfirmDialog(null, "确定退出?", "提示", JOptionPane.YES_NO_OPTION);
if(select == JOptionPane.YES_OPTION){
System.exit(0);
}
repaint();
}
}
public static void main(String[] args) {
//Window风格
String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try {
UIManager.setLookAndFeel(lookAndFeel);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
DrawPanel drawPanel = new DrawPanel();
drawPanel.setTitle("画线Demo");
drawPanel.setDefaultCloseOperation(3);
drawPanel.setVisible(true);
drawPanel.setSize(300, 300);
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DrawPanel extends JFrame implements ActionListener,MouseListener,MouseMotionListener{
private static final long serialVersionUID = 6748004371807231054L;
public static final int COUNT = 5;
public static final int READY = 0;//准备好了
public static final int START = 1;// 开始
public static final int DRAWING = 2;// 画线中
public static final int COMPLETE = 3;// 完成
public JPopupMenu rightMenu;// 右键弹出菜单
public JPanel toolPanel;// 工具栏
public JPanel contentPanel;// 内容面板
protected int statue;
protected Point currentPoint;//检测点
protected Point mousePoint;//目前鼠标所在的店
protected ArrayList<Point> routes = new ArrayList<Point>(); // 鼠标的路径
JMenuItem newItem,compItem, continueItem,clearItem;
JMenuBar menuBar;
JMenu fileMenu;
JMenuItem createItem,saveItem,exitItem;
public DrawPanel() {
init();
}
private void init(){
statue = DrawPanel.READY;
this.setLayout(new BorderLayout());
//菜单栏
menuBar = new JMenuBar();
fileMenu = new JMenu("文件");
createItem = new JMenuItem("新建");
createItem.addActionListener(this);
saveItem = new JMenuItem("保存");
saveItem.addActionListener(this);
exitItem = new JMenuItem("退出");
exitItem.addActionListener(this);
fileMenu.add(createItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
menuBar.add(fileMenu);
this.setJMenuBar(menuBar);
//弹出菜单
rightMenu = new JPopupMenu();
currentPoint = new Point(-100, -100);
newItem = new JMenuItem("新建");
setJMenuItem(newItem);
compItem = new JMenuItem("完成");
setJMenuItem(compItem);
continueItem = new JMenuItem("继续");
setJMenuItem(continueItem);
clearItem = new JMenuItem("清屏");
setJMenuItem(clearItem);
rightMenu.add(compItem);
rightMenu.addSeparator();
rightMenu.add(newItem);
rightMenu.add(continueItem);
rightMenu.addSeparator();
rightMenu.add(clearItem);
//画线的面板
contentPanel = new JPanel();
this.getContentPane().add(contentPanel);
contentPanel.addMouseListener(this);
contentPanel.addMouseMotionListener(this);
}
public void paint(Graphics g) {
super.paint(g);
if (g instanceof Graphics2D) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
g.setColor(Color.white);
g.fillRect(0, this.getHeight()-contentPanel.getHeight(), contentPanel.getWidth(), contentPanel.getHeight());
g.setColor(Color.black);
if (routes.size() >= 2) {
//画线
for (int i = 0; i < routes.size() - 1; i++) {
g.drawLine(routes.get(i).x, routes.get(i).y,
routes.get(i + 1).x, routes.get(i + 1).y);
}
}
if(statue == DrawPanel.DRAWING){
if(mousePoint != null){
if(routes.size() == 1){
g.drawLine(routes.get(0).x, routes.get(0).y, mousePoint.x, mousePoint.y);
}else if(routes.size()>1){
g.drawLine(routes.get(routes.size()-1).x,routes.get(routes.size()-1).y, mousePoint.x, mousePoint.y);
}
}
}
if (statue == DrawPanel.COMPLETE) {
//画出所有点
for (int i = 0; i < routes.size(); i++) {
g.fillOval(routes.get(i).x, routes.get(i).y, 2, 2);
}
//画出当前点
g.setColor(Color.red);
g.drawOval(currentPoint.x - 1, currentPoint.y - 1, 2, 2);
g.drawOval(currentPoint.x - 2, currentPoint.y - 2, 4, 4);
g.drawString("当前点坐标:("+currentPoint.x+","+currentPoint.y+")",currentPoint.x,currentPoint.y);
}
}
//菜单项事件绑定
public void setJMenuItem(final JMenuItem item) {
item.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
if(item.equals(newItem)){
rightMenu.setVisible(false);
statue = DrawPanel.START;
routes.removeAll(routes);
repaint();
}else if (item.equals(compItem)) {
rightMenu.setVisible(false);
statue = DrawPanel.COMPLETE;
repaint();
}else if(item.equals(continueItem)){
rightMenu.setVisible(false);
statue = DrawPanel.DRAWING;
repaint();
}else if(item.equals(clearItem)){
rightMenu.setVisible(false);
statue = DrawPanel.START;
routes.removeAll(routes);
repaint();
}
}
public void mouseEntered(MouseEvent arg0) {
item.setArmed(true);
}
public void mouseExited(MouseEvent arg0) {
item.setArmed(false);
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
}
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3) {
rightMenu.setLocation(e.getLocationOnScreen());
rightMenu.setVisible(true);
}
if(statue == DrawPanel.READY){
return;
}
if (e.getButton() == MouseEvent.BUTTON1) {
Point curPoint = e.getPoint();
routes.add(new Point(curPoint.x,curPoint.y+this.getHeight()-contentPanel.getHeight()));
statue = DrawPanel.DRAWING;
repaint();
}
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseDragged(MouseEvent e) { }
public void mouseMoved(MouseEvent e) {
if(statue == DrawPanel.DRAWING){
mousePoint = new Point(e.getPoint().x,e.getPoint().y+this.getHeight()-contentPanel.getHeight());
repaint();
}
if (statue == DrawPanel.COMPLETE) {
Point np = e.getPoint();
np = new Point(np.x,np.y+this.getHeight()-contentPanel.getHeight());
for(Point p : routes){
if(checkInRect(p,np)){
currentPoint = p;
repaint();
break;
}else{
currentPoint = new Point(-100,-100);
}
}
}
}
/**
* 范围检测
* @param p1
* @param p2
* @return
*/
private boolean checkInRect(Point p1,Point p2){
if(((p2.getX()>p1.getX()-5)&&(p2.getX()<p1.getX()+5))&&
((p2.getY()>p1.getY()-5)&&(p2.getY()<p1.getY()+5)))
{
return true;
}
return false;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(createItem)){
statue = DrawPanel.START;
routes.removeAll(routes);
repaint();
}else if(e.getSource().equals(saveItem)){
statue = DrawPanel.COMPLETE;
repaint();
}else if(e.getSource().equals(exitItem)){
int select = JOptionPane.showConfirmDialog(null, "确定退出?", "提示", JOptionPane.YES_NO_OPTION);
if(select == JOptionPane.YES_OPTION){
System.exit(0);
}
repaint();
}
}
public static void main(String[] args) {
//Window风格
String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try {
UIManager.setLookAndFeel(lookAndFeel);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
DrawPanel drawPanel = new DrawPanel();
drawPanel.setTitle("画线Demo");
drawPanel.setDefaultCloseOperation(3);
drawPanel.setVisible(true);
drawPanel.setSize(300, 300);
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
分两个JPanel 面板来处理不就可以了,一个放 JMenuBar组件。另一个 画图
追问
那麻烦你看下这段简单代码,jpanel背景设置为红色,怎么在这红色背景上画出一段字符串。我开始设置的一个匿名内部类在add(jpanel)哪里,可以按预想的那样运行。可是当把画图单独编制到一个画图类以后,就把jpanel覆盖了
Dra dra=new Dra();//dra为画图类
add(dra);
jPanel.setBackground(Color.red);
add(jPanel); //不能显示出所画图形
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询