简体   繁体   English

具有画布和Jpanel布局的Jframe

[英]Jframe with canvas and Jpanel layout

I'm just beginning on making a GUI for a lwjgl engine. 我刚刚开始制作用于lwjgl引擎的GUI。 I have a canvas on the left hand side and I want to have a JPanel on the right for previewing textures. 我的左侧有一个画布,我想在右侧具有一个JPanel来预览纹理。 However as you can see, the image is barely visible. 但是,如您所见,该图像几乎不可见。 I've tried different Borderlayouts, trying to resize etc.. However I think I'm just doing something fundamentally wrong. 我尝试了不同的Borderlayouts,尝试调整大小等。但是,我认为我所做的只是根本上的错误。

http://i.imgur.com/iJtEkv6.png

My panel class 我的小组课

public class Panel extends JPanel {
BufferedImage image;

public Panel(){
try {
    image = ImageIO.read(new         
File("C:/Users/tom/Desktop/raj/Jtest/src/AWT/house.png"));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}
public void paintComponent(Graphics g){
 // super.paint(g);

g.drawImage(image,0, 0, null);

}

and here is how I'm trying to implement it. 这就是我试图实现它的方式。

 public static void main(String[] args) 
  {

     Panel panel = new Panel();
     UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
     JFrame frame = new JFrame("World Editor");
     frame.setLayout(new BorderLayout());
     final Canvas canvas = new Canvas();

    button1.setSize(100, 100);
    button1.setLocation(600, 10);
   button1.setText("Test");
    canvas.addComponentListener(new ComponentAdapter() {
        @Override

        public void componentResized(ComponentEvent e)
        {  canvas.setSize(800, 600);

            newCanvasSize.set(canvas.getSize());
            }
     });



     frame.setBackground(Color.black);
     frame.add(button1);
     frame.add(panel,BorderLayout.EAST);
     frame.getContentPane().add(canvas,BorderLayout.CENTER);

     try {
        Display.setParent(canvas);
        Display.setVSyncEnabled(true);

        frame.setPreferredSize(new Dimension(1600, 1400));
        frame.setMinimumSize(new Dimension(800, 600));
        frame.pack();
        frame.setVisible(true);

        Display.create();
        etc...

the setup should of been like this http://i.imgur.com/Zitizdx.png 设置应该像这样http://i.imgur.com/Zitizdx.png

      ePanel panel = new ePanel(0, 0);


      UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
      JFrame frame = new JFrame("World Editor");

      frame.setLayout(new BorderLayout());
     canvas = new Canvas();
     canvas.setSize(1200, 600);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setLayout(new BorderLayout());
     button1.setSize(100, 100);
     button1.setText("TEST");
     frame.add(button1);
     frame.add(canvas,BorderLayout.WEST);
     frame.add(panel, BorderLayout.CENTER);
     frame.pack();
     frame.setLocationRelativeTo(null);
     frame.setVisible(true);

Do you already tried to set the image for a background label and resize it? 您是否已经尝试为背景标签设置图像并调整其大小? Something like this: http://www.java2s.com/Code/Java/Swing-JFC/Labelbackgroundiconalign.htm 像这样的东西: http : //www.java2s.com/Code/Java/Swing-JFC/Labelbackgroundiconalign.htm

Don't use a Canvas. 不要使用画布。 That is an AWT class and you should not mix AWT components in a Swing application. 那是一个AWT类,您不应在Swing应用程序中混合使用AWT组件。 I'm not sure what the point of the Canvas is, but you should probably be using a JPanel. 我不确定Canvas的意义是什么,但是您可能应该使用JPanel。

Don't call you class "Panel". 不要称呼您为“ Panel”类。 There is an AWT class with that name already. 已经有一个具有该名称的AWT类。 Class names should be more descriptive. 类名应更具描述性。

In fact, don't even create a custom class. 实际上,甚至不要创建自定义类。 Just use a JLabel with an ImageIcon to display the image. 只需将JLabel与ImageIcon一起使用即可显示图像。 I'm guessing the problem is that you didn't override the getPreferredSize() method of the "Panel" class so the size is (10, 10) by default which is the size the panel will have because you are using a FlowLayout. 我猜问题是,你没有覆盖getPreferredSize()这样的尺寸为(10,10),默认情况下它的大小面板将有因为你使用的FlowLayout“小组”类的方法。 So either use a JLabel (easy solution) or implement the getPreferredSize() method to reflect the size of the image. 因此,无论使用的JLabel(简单的解决方案)或实现getPreferredSize()方法,以反映图像的大小。

Set a GridLayout to the JFrame new GridLayout(0, 2) http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html That way, the JFrame has left and right side. 将GridLayout设置为JFrame的新GridLayout(0,2) http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html这样,JFrame具有左右两侧。 Then create and add two JPanels to the Jframe. 然后创建两个JPanel并将其添加到Jframe。 Do not forget resize the panels and the frame. 不要忘记调整面板和框架的大小。 After that, create a label, set to it an icon image, resize it and add it to the right panel. 之后,创建一个标签,为其设置一个图标图像,调整其大小并将其添加到右侧面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM