Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JLayeredPane格式问题_Java_Swing_Jlayeredpane - Fatal编程技术网

Java JLayeredPane格式问题

Java JLayeredPane格式问题,java,swing,jlayeredpane,Java,Swing,Jlayeredpane,我的JLayeredPane有问题,我可能正在做一些非常简单的事情,但我无法控制它。我遇到的问题是,所有组件都合并在一起,没有顺序。我不知道,你能纠正一下吗。我要做的是有一个这样的布局 输出 标签1(后面) 输入(前面) 还有主课 import java.awt.Container; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JLayeredPane; public class

我的
JLayeredPane
有问题,我可能正在做一些非常简单的事情,但我无法控制它。我遇到的问题是,所有组件都合并在一起,没有顺序。我不知道,你能纠正一下吗。我要做的是有一个这样的布局


输出


标签1(后面)
输入(前面)


还有主课

import java.awt.Container;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;


public class main {
public static int Winx, Winy;
private static JLayeredPane lpane = new JLayeredPane();
public static void main(String[] args) throws IOException{  
    Winx = window.WIDTH;
    Winy = window.HEIGHT;
    window Mth= new window();
    Mth.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Mth.setSize(1280,720);
    Mth.setVisible(true);
    lpane.add(window.label1);
    lpane.add(window.input);
    lpane.add(window.output);
    lpane.setLayer(window.label1, 2, -1);
    lpane.setLayer(window.input, 1, 0);
    lpane.setLayer(window.output, 3, 0);
    Mth.pack();
}
  }

感谢您抽出时间,我不希望代码是为我编写的,我想要的只是关于我哪里出错的提示。

我建议您不要使用JLayeredPane作为GUI的总体布局。使用BoxLayout或BorderLayout,然后仅在需要分层的地方使用JLayeredPane。此外,向JLayeredPane添加组件时,请使用接受组件和整数的add方法。不要调用
add(…)
然后调用
setLayer(…)

编辑:可以在执行操作时使用
setLayer(…)
。我以前从未使用过这个,但根据API,这是设置层的一种方法

e、 g


我建议您不要使用JLayeredPane作为GUI的总体布局。使用BoxLayout或BorderLayout,然后仅在需要分层的地方使用JLayeredPane。此外,向JLayeredPane添加组件时,请使用接受组件和整数的add方法。不要调用
add(…)
然后调用
setLayer(…)

编辑:可以在执行操作时使用
setLayer(…)
。我以前从未使用过这个,但根据API,这是设置层的一种方法

e、 g


为什么不直接使用GUI底部的JLayeredPane,即需要覆盖的部分?另外,为什么您两次向JLayeredPane添加组件?我从来没有使用过
setLayer(…)
方法,也不知道为什么会这样。为什么不简单地调用
add(Component,Integer)
重载呢?我明白你的意思,但是我如何分割它?你的意思是什么?你如何分割它?您可以对整个GUI使用不同的布局,例如BoxLayout,将输出添加到topp,将JLayeredPane添加到底部,然后将组件添加到JLayeredPane一次,而不是像您看起来那样添加两次。布局的关键是要进行试验、播放,与他们一起工作,直到他们像你希望的那样工作。
ImageIcon icon=new ImageIcon(“C:\\Users\\”+System.getProperty(“user.name”)+“\\AppData\\Roaming\\\.Satine\\img\\textbox.png”,“这是描述”)
1)
“C:\\Users\\\”+System.getProperty(“user.name”)
可能等同于
System.getProperty(“user.home”)
但是。。2) 如果这是一个“应用程序资源”,它通常位于应用程序的运行时类路径上,并且可以通过从
class.getResource(String)
获取的URL访问,为什么不使用GUI底部的JLayeredPane,在需要覆盖的部分?另外,为什么您两次向JLayeredPane添加组件?我从来没有使用过
setLayer(…)
方法,也不知道为什么会这样。为什么不简单地调用
add(Component,Integer)
重载呢?我明白你的意思,但是我如何分割它?你的意思是什么?你如何分割它?您可以对整个GUI使用不同的布局,例如BoxLayout,将输出添加到topp,将JLayeredPane添加到底部,然后将组件添加到JLayeredPane一次,而不是像您看起来那样添加两次。布局的关键是要进行试验、播放,与他们一起工作,直到他们像你希望的那样工作。
ImageIcon icon=new ImageIcon(“C:\\Users\\”+System.getProperty(“user.name”)+“\\AppData\\Roaming\\\.Satine\\img\\textbox.png”,“这是描述”)
1)
“C:\\Users\\\”+System.getProperty(“user.name”)
可能等同于
System.getProperty(“user.home”)
但是。。2) 如果这是一个“应用程序资源”,它通常位于应用程序的运行时类路径上,并且可以通过从
class.getResource(String)
获取的URL访问。好吧,你让我希望我在你发布源代码之前没有投票(它被标记为正确&)。我可以再试一次吗?我想(EeerrRight Jeanette又是正确的)是时候使用JXLayer了(更好、最安全的是-->),或者它的简化实现是JLayer到Java7Well,你让我希望我没有在你发布源代码之前投票(它被标记为正确&)。我可以再试一次吗?我想(EeerrRight Jeanette又是正确的)是时候使用JXLayer了(更好、最安全的是-->),或者它的简化实现是JLayer到Java7
import java.awt.Container;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;


public class main {
public static int Winx, Winy;
private static JLayeredPane lpane = new JLayeredPane();
public static void main(String[] args) throws IOException{  
    Winx = window.WIDTH;
    Winy = window.HEIGHT;
    window Mth= new window();
    Mth.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Mth.setSize(1280,720);
    Mth.setVisible(true);
    lpane.add(window.label1);
    lpane.add(window.input);
    lpane.add(window.output);
    lpane.setLayer(window.label1, 2, -1);
    lpane.setLayer(window.input, 1, 0);
    lpane.setLayer(window.output, 3, 0);
    Mth.pack();
}
  }
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class LayeredPaneFun extends JPanel {
   public static final String IMAGE_PATH = "http://duke.kenai.com/" +
        "misc/Bullfight.jpg";

   public LayeredPaneFun() {
      try {
         BufferedImage img = ImageIO.read(new URL(IMAGE_PATH));
         ImageIcon icon = new ImageIcon(img);
         JLabel backgrndLabel = new JLabel(icon);
         backgrndLabel.setSize(backgrndLabel.getPreferredSize());

         JPanel forgroundPanel = new JPanel(new GridBagLayout());
         forgroundPanel.setOpaque(false);

         JLabel fooLabel = new JLabel("Foo");
         fooLabel.setFont(fooLabel.getFont().deriveFont(Font.BOLD, 32));
         fooLabel.setForeground(Color.cyan);
         forgroundPanel.add(fooLabel);
         forgroundPanel.add(Box.createRigidArea(new Dimension(50, 50)));
         forgroundPanel.add(new JButton("bar"));
         forgroundPanel.add(Box.createRigidArea(new Dimension(50, 50)));
         forgroundPanel.add(new JTextField(10));
         forgroundPanel.setSize(backgrndLabel.getPreferredSize());

         JLayeredPane layeredPane = new JLayeredPane();
         layeredPane.setPreferredSize(backgrndLabel.getPreferredSize());
         layeredPane.add(backgrndLabel, JLayeredPane.DEFAULT_LAYER);
         layeredPane.add(forgroundPanel, JLayeredPane.PALETTE_LAYER);

         setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
         add(new JScrollPane(new JTextArea("Output", 10, 40)));
         add(layeredPane);

      } catch (MalformedURLException e) {
         e.printStackTrace();
         System.exit(1);
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(1);
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("LayeredPaneFun");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new LayeredPaneFun());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}