问题描述
- java swing组件上绘制直线同时添加组件后 的显示问题
-
说明:运行后,面板上自动画出14条直线,点击“添加”按钮,生成可以拖动的JTextField控件。问题(1)面板的背景色不显示,(2)拖动JTextField控件时有残影,(3)改变JFrame大小后,残影消失,但是只能显示JTextField控件的一部分。哪位高手指点下。import java.awt.*; import java.awt.event.*; import java.awt.Graphics; import static java.awt.BorderLayout.*; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.SwingUtilities; import javax.swing.plaf.ButtonUI; import javax.swing.event.*; class Win extends JFrame { JButton jb1,jb2,jb3,jb4,jb5;//编辑、添加、修改、删除、调度 JButton jb6,jb7,jb8,jb9,jb10;//调试用按钮 JPanel jp_button;//放置按钮的面板 MyPanel jp_xianlu;//放置线路图的面板 public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Win win=new Win(); //win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setLocationRelativeTo(null); win.setVisible(true); } }); } Win() { super("我的面板"); setExtendedState( Frame.MAXIMIZED_BOTH ); setLayout(new BorderLayout(30,5)); jp_button =new JPanel(); jp_button.setVisible(true); jp_button.setOpaque(true); jp_button.setBackground(Color.BLUE); add(jp_button,SOUTH); jp_xianlu =new MyPanel(); add(jp_xianlu,CENTER); //添加5个按钮并添加到面板jp_button上 jb1 = new JButton("编辑"); jb2 = new JButton("添加"); jb3 = new JButton("修改"); jb4 = new JButton("删除"); jb5 = new JButton("调度"); jp_button.add(jb1); jp_button.add(jb2); jp_button.add(jb3); jp_button.add(jb4); jp_button.add(jb5); //Frame上添加windows监听,使之关闭按钮可用 addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); //点击“添加”按钮时在jp_xianlu面板上生成JLabel jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setMyText(200,200); } }); } //自定的可以生成LTextField的方法 public void setMyText(int a,int b){ MyTextField mtf= new MyTextField(); //输入标签显示内容 mtf.setBounds(a,b,55,20); jp_xianlu.add(mtf); } } //自定义监听器类:转换坐标并实现自由拖动 class MyTextField extends JTextField implements MouseListener,MouseMotionListener { /** 坐标点 */ Point point = new Point(0, 0); MyTextField(){ super("0000000"); addMouseListener(this); addMouseMotionListener(this); setOpaque(true); //组件背景透明 setBackground(Color.RED); } /** * 当鼠标拖动时触发该事件记录下鼠标按下(开始拖动)的位置(摘自苏小喵) */ public void mouseDragged(MouseEvent e) { //转换坐标系统 Point newPoint = SwingUtilities.convertPoint(this, e.getPoint(), this.getParent()); //设置标签的新位置 this.setLocation(this.getX() + (newPoint.x - point.x), this.getY() + (newPoint.y - point.y)); //更改坐标点 point = newPoint; } public void mouseMoved(MouseEvent e){} /** * 当鼠标按下时触发该事件。 记录下鼠标按下(开始拖动)的位置。 */ public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e) { //得到当前坐标点 point = SwingUtilities.convertPoint(this, e.getPoint(), this.getParent()); } public void mouseReleased(MouseEvent e){} } //自定义面板,在其上实画线(未实现,不可用) class MyPanel extends JPanel{ public MyPanel(){ setBackground(Color.GREEN); setOpaque(true); setLayout(null); setVisible(true); } @Override //画14条直线 public void paint(Graphics g){ g.drawLine(10,50,1900,50); g.drawLine(10,115,1900,115); g.drawLine(10,180,1900,180); g.drawLine(10,245,1900,245); g.drawLine(10,310,1900,310); g.drawLine(10,375,1900,375); g.drawLine(10,440,1900,440); g.drawLine(10,505,1900,505); g.drawLine(10,570,1900,570); g.drawLine(10,635,1900,635); g.drawLine(10,700,1900,700); g.drawLine(10,765,1900,765); g.drawLine(10,830,1900,830); g.drawLine(10,895,1900,895); } }
时间: 2024-11-18 21:10:16