JLabel.setText()重定位mousewheelevent中的所有标签

时间:2014-05-13 19:23:46

标签: java swing location jlabel settext

编辑:留下显示问题的短代码: 然后调用label.setText("")并且它必须改变文本,即缩放器克服50阻止标签消失为mousewheelevent - >重新出现在下一个事件上。也是装载时标签不存在的问题 在实际的程序标签中,如果它们中的任何一个具有.setText()更改值或.setVisible更改(这两种方法我需要能够使用而不影响标签定位),则将它们叠加在一起。

import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
public class brokenLabel extends JFrame
{
private JLayeredPane layerPane;
private JPanel breakingLabelPanel = new JPanel();
private JLabel label;
private int scaler = 50;
private int xstart = 200;
private int ystart = 200;

public static void main(String[] args) 
{   
@SuppressWarnings("unused")
brokenLabel program = new brokenLabel();
}
public brokenLabel() 
{
// Frame setup used on the program
final JFrame frame = new JFrame("This is a slideshow");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
layerPane = new JLayeredPane();
frame.setContentPane(layerPane);        
// Panel used (it's similar to what's added on the actual program)
breakingLabelPanel.setOpaque(true);
breakingLabelPanel.setLayout(new OverlayLayout(breakingLabelPanel));
breakingLabelPanel.setVisible(true);
breakingLabelPanel.setSize(getMaximumSize());
breakingLabelPanel.addMouseWheelListener(new MouseWheelListener(){
    @Override
public void mouseWheelMoved(MouseWheelEvent arg0) 
{       
    if (scaler > 5 || arg0.getWheelRotation() < 0)
        scaler = scaler - 5*arg0.getWheelRotation();
    setTexts();
}
});
//The testing label
label = new JLabel(new String("1"));
label.setLocation(xstart , ystart);
breakingLabelPanel.add(label);
frame.add(breakingLabelPanel,layerPane);        
frame.setVisible(true);
//revalidate and repaint don't help make label visible on load-up
frame.revalidate();
frame.repaint();
}

public void setTexts(){
label.setLocation(xstart + scaler,ystart + 25);
if(scaler < 50)
{
    label.setText("1");
}
else 
{
    label.setText("2");
}
}
    }

0 个答案:

没有答案