我的控制条有问题,不知道为什么,一出现的时候控制条是这样的:
然后稍微对边框的大小进行修改一下,控制条就成了这样:
我希望使控制条一直是下面的状况,怎么办呢?
这是代码::
package Appliction;
import javax.swing.JFrame ;
import javax.swing.JPanel ;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player ;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component ;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URL;
public class My_MP3Player
{
private JFrame JF1 ;
// private JPanel JP1 ;
private Player player ;
private Component com_contrl ;
private ControllerListener conLis ;
private Dimension dim ;
//初始化播放器
private void ini_player(URL url) throws IOException
{
try
{
player = Manager.createPlayer(url) ;
}
catch(NoPlayerException noE)
{
System.out.println("没有发现播放器存在");
noE.printStackTrace() ;
}
}
//初始化参数
private void ini_Field()
{
JF1 = new JFrame("播放器") ;
// JP1 = new JPanel() ;
dim = new Dimension() ;
}
//注册controllerListener
private void register()
{
conLis = new ControllerListener()
{
@Override
public void controllerUpdate(ControllerEvent ce)
{
if(ce instanceof RealizeCompleteEvent)
{
player.prefetch() ;
}
if(ce instanceof PrefetchCompleteEvent)
{
if(com_contrl != null)
{
return ;
}
else if( (com_contrl = player.getControlPanelComponent())!= null )
{
// JP1.add(com_contrl , BorderLayout.EAST) ;
JF1.add(com_contrl , BorderLayout.SOUTH) ;
dim = com_contrl.getPreferredSize();
}
else
{
System.out.println("添加组件失败");
}
}
}
} ;
player.addControllerListener(conLis) ;
}
//播放设置
private void player(URL url) throws IOException
{
ini_Field() ;
ini_player(url) ;
JF1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
if(player != null)
{
player.close();
}
System.exit(0);
}
});
register() ;
// JP1.setPreferredSize(new Dimension(300+dim.width, 50+dim.height)) ;
// JP1.setBackground(Color.BLUE) ;
JF1.setSize(300 + dim.width , 320+dim.height) ;
// JF1.add(JP1 , BorderLayout.SOUTH) ;
JF1.validate() ;
JF1.setVisible(true) ;
player.realize() ;
player.start() ;
}
//构造函数
public My_MP3Player(URL url) throws IOException
{
player(url) ;
}
//main函数
public static void main(String[] args)
{
try
{
URL url = new URL("file:///E:\\音乐\\Ours.mp3") ;
if(url.getFile() == null)
{
throw new IOException("未找到文件") ;
}
else
{
new My_MP3Player(url) ;
}
}
catch(IOException ioe)
{
ioe.printStackTrace() ;
}
}
}
大侠们谁有时间帮我看看吧,实在是改不了了,怎么办啊!?
------解决方案--------------------
是因为你是在frame显示之后才把组件加上去,
当改变窗口大小的时候窗口要重绘,这时组件才显示出来
把你加组件的代码放到JF1.setVisible(true);后面就行了这是我改的代码:
import javax.swing.JFrame ;
import javax.swing.JPanel ;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player ;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component ;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;