我正在尝试将一个OpenGL上下文添加到jFrame。
当我这样做时它会起作用:
jframe.setVisible( true );
jframe.getContentPane().add( glcanvas, BorderLayout.CENTER );
jframe.revalidate();
jframe.repaint();
但是,如果我尝试在按钮上创建上下文,请单击:
JButton startButton = new JButton("Start");
startButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
jframe.getContentPane().add( glcanvas, BorderLayout.CENTER );
jframe.revalidate();
jframe.repaint();
}
});
jframe.add(startButton);
然后没有任何反应。如果我尝试调试它,程序将运行revalidate()和repaint()命令但没有任何变化。
有人可以告诉我为什么会这样 - 当我尝试按下按钮时,为什么会有所不同?你也可以建议一种方法来解决这个问题吗?我需要能够从菜单系统中打开上下文。
- 编辑:这是我的测试程序的完整代码
/* -- OpenGL -- */
import javax.media.opengl.GL2ES2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.awt.GLJPanel;
import javax.swing.JComponent;
/* -- Swing -- */
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
/* -- jFrame Layouts -- */
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Menu
{
public static void main(String[] args)
{
GLProfile glprofile = GLProfile.getDefault();
GLCapabilities glcapabilities = new GLCapabilities( glprofile );
final GLCanvas glcanvas = new GLCanvas( glcapabilities );
glcanvas.addGLEventListener( new GLEventListener()
{
@Override
public void init( GLAutoDrawable glautodrawable )
{
System.out.println("INIT");
}
@Override
public void reshape( GLAutoDrawable glautodrawable, int x, int y, int width, int height )
{
GL2ES2 gl = glautodrawable.getGL().getGL2ES2(); //Get OpenGL
gl.glViewport( 0, 0, width, height ); //Set the viewport
}
@Override
public void display( GLAutoDrawable glautodrawable )
{
GL2ES2 gl = glautodrawable.getGL().getGL2ES2();
gl.glClearColor(+0.0f, +0.2f, +0.9f, +0.0f); // Blue background
//Clear the screen so that we can draw the new one
gl.glClear(
GL2ES2.GL_STENCIL_BUFFER_BIT |
GL2ES2.GL_COLOR_BUFFER_BIT |
GL2ES2.GL_DEPTH_BUFFER_BIT
);
}
@Override
public void dispose( GLAutoDrawable glautodrawable )
{
}
}); /* Add GLEventListner to glCanvas */
final JFrame jframe = new JFrame( "One Triangle Swing GLCanvas" );
jframe.addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent windowevent )
{
jframe.dispose();
System.exit( 0 );
}
});
jframe.setSize( 800, 600 );
jframe.setVisible( true );
/* --- IF I ADD THE ContentPane HERE AND REVALIDATE/REPAINT IT WORKS --- */
//jframe.getContentPane().add( glcanvas, BorderLayout.CENTER );
//jframe.revalidate();
//jframe.repaint();
jframe.setLayout(new FlowLayout());
JButton startButton = new JButton("Start");
startButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
/* --- I NEED TO ADD THE ContentPane HERE, BUT IF I DO THIS THE REPAINT DOESN'T WORK --- */
jframe.getContentPane().add( glcanvas, BorderLayout.CENTER );
jframe.revalidate();
jframe.repaint();
/* -- I added this because I was told elsewhere that repainting
* in a different thread would make it work, however I haven't
* been successful in getting this to work. Included for information
*/
//repaintThread(jframe);
}
});
jframe.add(startButton);
}
private static void repaintThread(final JFrame jframe)
{
Thread thread = new Thread(new Runnable()
{
public void run()
{
for( int i = 0; i < 200; i++)
{
jframe.revalidate();
jframe.repaint();
try
{
Thread.currentThread().sleep( 50 );
}
catch( Exception ex )
{
break;
}
}
}
});
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
}
答案 0 :(得分:1)
这是让一切混乱的路线:
jframe.setLayout(new FlowLayout());
您正在将布局更改为FlowLayout
,然后在glcanvas
旁边添加startButton
。 glcanvas
的首选大小为0,因此不可见,但单击按钮时可以看到startButton
的轻微移动。
我认为设置FlowLayout
并非故意,因为您使用BorderLayout.CENTER
约束来添加glcanvas
。所以这是一个简单的修复:
评论:
//jframe.setLayout(new FlowLayout());
将startButton
添加到面板的NORTH
而不是CENTER
,因为glcanvas
将转到CENTER
。变化:
jframe.add(startButton);
到:
jframe.add(startButton, BorderLayout.NORTH);
修复了重新验证问题,至少在Windows 7 Java 7上。
另外,请参阅A Visual Guide to Layout Managers以获取有关布局的更多详细信息和示例。