我希望做一个文字浮动的效果请问有什么方法
在JLabel里面有一句话,使它从一个方向移动到另一个方向
我想过TimerTask的方法,可是总是实现不了
2
还有如何自定义JButton,比如把图片做成JButton,
希望有改造JButton的简单方法
3
我希望在GirdLayout中改变组建的大小
就是我希望使用它的布局但是想自定义里面组建的大小
..........................
或者这一个可以帮到你的
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class floatTitle extends JFrame{
tools_JPanel tj;
float_Panel fp;
public floatTitle()
{
fp = new float_Panel();
tj = new tools_JPanel(fp);
new Thread(fp).start();
this.setLayout(new BorderLayout());
this.add(tj,BorderLayout.NORTH);
this.add(fp,BorderLayout.CENTER);
this.pack();
this.setSize(450,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String args[])
{
new floatTitle();
}
}
class tools_JPanel extends JPanel implements ActionListener
{
JButton btn;
ImageIcon ii;
JTextField jtf;
JSlider js;
JLabel label;
float_Panel fp;
public tools_JPanel(final float_Panel fp)
{
this.fp = fp;
ii = new ImageIcon("folder.gif");//这就可以实现在button里有一张图片嘛http://bbs.bc-cn.net/skins/Default/folder.gif把这一个图片放到同一目录下
btn = new JButton(ii);
btn.setText("OK");
btn.addActionListener(this);
label = new JLabel("输入:");
jtf = new JTextField(10);
js = new JSlider(1,10);
js.setToolTipText("文字移动的速度");
js.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
JSlider tempjs = ((JSlider)e.getSource());
int js_value = tempjs.getValue();
fp.sleepTime = js_value * 10;
}
});
this.add(btn);
this.add(label);
this.add(jtf);
this.add(js);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("OK"))
{
String text = jtf.getText();
if(text.equals("") || text == null)
{
JOptionPane.showConfirmDialog(this,"不能为空值", "提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.ERROR_MESSAGE);
}else
{
fp.title = text;
}
}
}
}
class float_Panel extends JPanel implements Runnable
{
int x,y;
int sleepTime;
String title;
boolean isRun,letf;
public float_Panel()
{
this.sleepTime = 500;
this.isRun = true;
this.letf = true;
this.title = "请输入文字";
this.x = (this.getWidth()/2);
this.y = 150;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawString(this.title , this.x, this.y);
//g.fillRect(0, 0, this.getWidth(), this.getHeight());
//System.out.println(this.title + " moving " + this.x +" "+this.y );
}
public void run()
{
while(this.isRun)
{
try{
Thread.sleep(this.sleepTime);
if(letf)
{
this.x=this.x - 3;
}else //还可以实现向左或者向右走
{
this.x=this.x + 3;
}
if(this.x <0)
{
this.x = this.getWidth();
}else
if(this.x > this.getWidth())
{
this.x = 0;
}
}catch(Exception e)
{
}
repaint();
}
}
}