public class Draw extends JPanel{
public Draw(){
super();
setLayout(null);
JLabel label=new JLabel("label");
label.setToolTipText("display");
label.setBounds(400,100,400,20);
add(label);
}
public paintComponent(Graphics g){ g.drawLine(30,30,40,20);
}} 在main()中:
JFrame showFrame=new JFrame("");
JScrollPane scrollPane=new JScrollPane();
showFrame.getContentPane().add(scrollPane);
Draw draw=new Draw();
scrollPane.setViewportView(draw);
showFrame.setBounds(200,200,200,30);//该窗体的宽度小于draw中label的宽度
showFrame.show();运行时出现2个问题:1 不显示滑动条;2 鼠标放置到label之后显示浮动的“diaplay”,鼠标移走后依旧显示。
求解决方案。
public Draw(){
super();
setLayout(null);
JLabel label=new JLabel("label");
label.setToolTipText("display");
label.setBounds(400,100,400,20);
add(label);
}
public paintComponent(Graphics g){ g.drawLine(30,30,40,20);
}} 在main()中:
JFrame showFrame=new JFrame("");
JScrollPane scrollPane=new JScrollPane();
showFrame.getContentPane().add(scrollPane);
Draw draw=new Draw();
scrollPane.setViewportView(draw);
showFrame.setBounds(200,200,200,30);//该窗体的宽度小于draw中label的宽度
showFrame.show();运行时出现2个问题:1 不显示滑动条;2 鼠标放置到label之后显示浮动的“diaplay”,鼠标移走后依旧显示。
求解决方案。
super.paintComponent(g); //******//
g.drawLine(30,30,40,20);
}
JFrame showFrame=new JFrame("");
JScrollPane scrollPane=new JScrollPane();
showFrame.getContentPane().add(scrollPane);
Draw draw=new Draw();
draw.setPreferredSize(new Dimension(900, 200)); //*****//
scrollPane.setViewportView(draw);
showFrame.setBounds(200,200,200,30);//该窗体的宽度小于draw中label的宽度
showFrame.show();
TooltipText可以显示了,但是滑动条没显示出来。我将Draw类构造函数中设置布局为setLayout(null),否则添加的标签无法定位。是不是因为布局管理器的原因而导致滑动条出不来?
public class Draw extends JPanel{
XYLayout xYLayout = new XYLayout();
public Draw(){
super();
setLayout(null);-->setLayout(xYLayout);
JLabel label=new JLabel("label");
label.setToolTipText("display");
label.setBounds(400,100,400,20);//删除
add(label);-->add(label,new XYConstraints(400,100,400,20))
}
public paintComponent(Graphics g){ g.drawLine(30,30,40,20);
}}
XYLayout是Jbuilder中特有的布局方式,大家以后在定位时可以考虑使用该布局
paintComponent需要调用super.paintComponent
/**
* @param args
*/
public static void main(String[] args)
{
JFrame showFrame = new JFrame("Draw"); JScrollPane scrollPane = new JScrollPane();
Draw draw = new Draw();
draw.setPreferredSize(new Dimension(500, 200));
scrollPane.setViewportView(draw);
showFrame.getContentPane().add(scrollPane); showFrame.setBounds(200, 200, 200, 100);
showFrame.setVisible(true);
} public Draw()
{
super(null);
JLabel label = new JLabel("label");
label.setToolTipText("display");
label.setBounds(400, 100, 400, 20);
add(label);
} @Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(30, 30, 40, 20);
}