谢谢

解决方案 »

  1.   

    楼主的问题我也碰到过 困惑了好久. 后来把Canvas换成JLabel就没这个问题了,呵呵
      

  2.   

    而且Canvas能做的事JLabel都能做
      

  3.   

    因为我要在Canvas上作图
    所以JLable不行的
      

  4.   

    JLabel也可以重载public void paint(Graphics e)方法
      

  5.   

    只要把 extends Canvas 换成 extends JLabel 其他的什么都不用管,一样用
      

  6.   

    同意使用JLable,使用JPanel也行。
    Swing和AWT的部件在Z-Order的布置上有出人,所以你的JMenu显示不出来。
      

  7.   

    package mypack;import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class Score extends JFrame implements ActionListener { /**
     * @param args
     */
    jpanel[] j;
    Container contentPane;
    int Count;
    Score(String title)
    {
    super(title);
    contentPane=this.getContentPane();
    contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
    addPane(6);
    initMenu();
    }

    void addPane(int n)
    {
    j=new jpanel[n];
    Count=n;
    for(int i=0;i<n;i++)
    {
    j[i]=new jpanel();
    contentPane.add(j[i]);
    }
    }

    void initMenu()
    {
    JMenuBar mb=new JMenuBar();
    JMenu jmStart=new JMenu("开始");
    JMenuItem jmiStart=new JMenuItem("开始");
    JMenuItem jmiEnd  =new JMenuItem("结束");
    JMenuItem jmiExit =new JMenuItem("退出");
    jmStart.add(jmiStart);
    jmStart.add(jmiEnd);
    jmStart.add(jmiExit);
    jmiStart.addActionListener(this);
    jmiEnd.addActionListener(this);
    jmiExit.addActionListener(this);

    JMenu jmAdd=new JMenu("增加候选人");
    JMenuItem jmiAdd=new JMenuItem("增加");
    JMenuItem jmiScore=new JMenuItem("得分");
    jmAdd.add(jmiAdd);
    jmAdd.add(jmiScore);
    jmiAdd.addActionListener(this);
    jmiScore.addActionListener(this);

    mb.add(jmStart);
    mb.add(jmAdd);
    setJMenuBar(mb);
    }

    public void actionPerformed(ActionEvent e)
    {
    JMenuItem jmi=(JMenuItem)e.getSource();
    if(jmi.getActionCommand().equals("退出"))
    {
    System.exit(0);
    }
    else if(jmi.getActionCommand().equals("增加"))
    { }
    }

    public static void main(String[] args) {
    // TODO 自动生成方法存根
    final Score s=new Score("演讲记分系统");
    s.setBounds(100,100,800,400);
    //s.setExtendedState(s.MAXIMIZED_BOTH);
    s.setDefaultCloseOperation(s.DISPOSE_ON_CLOSE);
    s.addWindowListener(new WindowAdapter(){
    public void  windowClosed(WindowEvent e)
    {
    System.exit(0);
    }
    });
    s.setVisible(true);
    }}class jpanel extends JPanel implements ActionListener
    {
    JButton jb;
    Rectangle rect;
    jpanel()
    {
    this.setBackground(Color.WHITE);
    this.setLayout(new BorderLayout());
    jb=new JButton("Vote");
    Dimension dm=jb.getPreferredSize();
    rect=new Rectangle(dm.width,1,0,0,Color.BLUE);
    rect.setBounds(0,0,dm.width,300);
    rect.setRectY(260);
    jb.addActionListener(this);
    //rect.setRectHeight(100);
    add(rect,BorderLayout.NORTH);
    add(jb,BorderLayout.SOUTH);
    }

    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    }

    public void actionPerformed(ActionEvent e)
    {
    rect.setRectY(rect.getRectY()-2);
    rect.setRectHeight(rect.getRectHeight()+2);
    }
    }class Rectangle extends Canvas
    {
    private int width,height,x,y;
    private Color c;

    Rectangle(int width,int height,int x,int y)
    {
    this.width=width;
    this.height=height;
    this.x=x;
    this.y=y;
    c=Color.RED;
    }

    Rectangle(int width,int height,int x,int y,Color c)
    {
    this.width=width;
    this.height=height;
    this.x=x;
    this.y=y;
    this.c=c;
    }

    public int getRectY()
    {
    return this.y;
    }
    public void setRectY(int y)
    {
    this.y=y;
    }
    public void setRectHeight(int height)
    {
    this.height=height;
    repaint();
    }

    public int getRectHeight()
    {
    return this.height;
    }

    public void paint(Graphics g)
    {
    g.setColor(c);
    g.fillRect(x,y,width,height);
    }
    }帮我看一下这个,用JLabel显示不出来。
      

  8.   

    这的确很奇怪 我刚才调试了下 根本没调用到paint这个函数
      

  9.   

    package mypack;import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;import javax.swing.Box;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;public class Score extends JFrame implements ActionListener { /**
     * @param args
     */
    jpanel[] j;
    Container contentPane;
    int Count;
    Score(String title)
    {
    super(title);
    contentPane=this.getContentPane();
    contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
    addPane(6);
    initMenu();
    }

    void addPane(int n)
    {
    j=new jpanel[n];
    Count=n;
    for(int i=0;i<n;i++)
    {
    j[i]=new jpanel();
    contentPane.add(j[i]);
    }
    }

    void initMenu()
    {
    JMenuBar mb=new JMenuBar();
    JMenu jmStart=new JMenu("开始");
    JMenuItem jmiStart=new JMenuItem("开始");
    JMenuItem jmiEnd  =new JMenuItem("结束");
    JMenuItem jmiExit =new JMenuItem("退出");
    jmStart.add(jmiStart);
    jmStart.add(jmiEnd);
    jmStart.add(jmiExit);
    jmiStart.addActionListener(this);
    jmiEnd.addActionListener(this);
    jmiExit.addActionListener(this);

    JMenu jmAdd=new JMenu("增加候选人");
    JMenuItem jmiAdd=new JMenuItem("增加");
    JMenuItem jmiScore=new JMenuItem("得分");
    jmAdd.add(jmiAdd);
    jmAdd.add(jmiScore);
    jmiAdd.addActionListener(this);
    jmiScore.addActionListener(this);

    mb.add(jmStart);
    mb.add(jmAdd);
    setJMenuBar(mb);
    }

    public void actionPerformed(ActionEvent e)
    {
    JMenuItem jmi=(JMenuItem)e.getSource();
    if(jmi.getActionCommand().equals("退出"))
    {
    System.exit(0);
    }
    else if(jmi.getActionCommand().equals("增加"))
    { }
    }

    public static void main(String[] args) {
    // TODO 自动生成方法存根
    final Score s=new Score("演讲记分系统");
    s.setBounds(100,100,800,400);
    //s.setExtendedState(s.MAXIMIZED_BOTH);
    s.setDefaultCloseOperation(s.DISPOSE_ON_CLOSE);
    s.addWindowListener(new WindowAdapter(){
    public void  windowClosed(WindowEvent e)
    {
    System.exit(0);
    }
    });
    s.setVisible(true);
    }}class jpanel extends JPanel implements ActionListener
    {
    JButton jb;
    Rectangle rect;
    jpanel()
    {
    this.setBackground(Color.WHITE);
    this.setLayout(new BorderLayout());
    jb=new JButton("Vote");
    Dimension dm=jb.getPreferredSize();
    rect=new Rectangle(dm.width,1,0,0,Color.BLUE);
    rect.setBounds(0,0,dm.width,300);
    rect.setRectY(260);
    jb.addActionListener(this);
    //rect.setRectHeight(100);
    add(Box.createRigidArea(new Dimension(0, 300)), BorderLayout.WEST);
    add(rect,BorderLayout.CENTER);
    add(jb,BorderLayout.SOUTH);
    }

    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    }

    public void actionPerformed(ActionEvent e)
    {
    rect.setRectY(rect.getRectY()-2);
    rect.setRectHeight(rect.getRectHeight()+2);
    }
    }//class Rectangle extends Canvas
    class Rectangle extends JLabel
    {
    //private int width,height,x,y;
    private int w,h,x,y;
    private Color c;

    Rectangle(int width,int height,int x,int y)
    {
    this.w=width;
    this.h=height;
    this.x=x;
    this.y=y;
    c=Color.RED;
    }

    Rectangle(int width,int height,int x,int y,Color c)
    {
    this.w=width;
    this.h=height;
    this.x=x;
    this.y=y;
    this.c=c;
    }

    public int getRectY()
    {
    return this.y;
    }
    public void setRectY(int y)
    {
    this.y=y;
    }
    public void setRectHeight(int height)
    {
    this.h=height;
    repaint();
    }

    public int getRectHeight()
    {
    return this.h;
    }

    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.setColor(c);
    g.fillRect(x,y,w,h);
    }
    }
      

  10.   

    楼上解释下add(Box.createRigidArea(new Dimension(0, 300)), BorderLayout.WEST);
    这句起了什么作用? 怎么这么神奇?呵呵
      

  11.   

    简单点可以这么写
    add(Box.createVerticalStrut(300), BorderLayout.WEST);没有text的JLabel默认情况下尺寸为0,所以会被BorderLayout压遍。Box.createVerticalStrut(300)
    或者
    Box.createRigidArea(new Dimension(0, 300))的作用是建立一个垂直方向的支撑。