【问题标题】:JLayeredPane formatting issueJLayeredPane 格式问题
【发布时间】:2012-05-11 10:01:04
【问题描述】:

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


输出


label1(后面)
输入(在前面)


import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class window extends JFrame implements KeyListener {
/**
 * 
 */
private static final long serialVersionUID = 7092006413113558324L;
private static int NewSize;
public static String MainInput;
public static JLabel label1 = new JLabel();
public static JTextField input = new JTextField(10);
public static JTextArea output = new JTextArea(main.Winx, NewSize);

public window() {
    super("Satine. /InDev-01/");
    JLabel label1;
    NewSize = main.Winy - 20;
    setLayout(new BorderLayout());
    output.setToolTipText("");
    add(input, BorderLayout.PAGE_END);
    add(output, BorderLayout.CENTER);
    input.addKeyListener(this);
    input.requestFocus();
    ImageIcon icon = new ImageIcon("C:\\Users\\" + System.getProperty("user.name") + "\\AppData\\Roaming\\.Satine\\img\\textbox.png", "This is the desc");
    label1 = new JLabel(icon);
    add(label1, BorderLayout.PAGE_END);
}  
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_ENTER) {
        try {
            MainMenu.start();
        } catch (IOException e1) {
            System.out.print(e1.getCause());
        }
    }
}


@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void keyTyped(KeyEvent e) {
}

 }

还有主类。

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();
}
  }

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

【问题讨论】:

  • 为什么不直接使用 GUI 底部的 JLayeredPane,在需要覆盖的部分?另外,为什么您似乎两次向 JLayeredPane 添加组件?我从来没有使用过setLayer(...) 方法,我不确定你为什么会这样。为什么不简单地调用add(Component, Integer) 重载?
  • 我明白你的意思,但我将如何拆分它?
  • ?你的意思是你会如何分割它?您可以为整个 GUI 使用不同的布局,例如 BoxLayout,将输出添加到顶部并将 JLayeredPane 添加到底部,然后将组件添加到 JLayeredPane 一次,而不是您看起来的两次。
  • 布局的关键是试验、玩耍、使用它们,直到它们按照您的意愿工作。
  • ImageIcon icon = new ImageIcon("C:\\Users\\" + System.getProperty("user.name") + "\\AppData\\Roaming\\.Satine\\img\\textbox.png", "This is the desc"); 1) "C:\\Users\\" + System.getProperty("user.name") 可能等同于 System.getProperty("user.home") 但是.. 2) 如果这是一个“应用程序资源”,它通常位于应用程序,可通过从Class.getResource(String)获取的 URL 访问

标签: java swing jlayeredpane


【解决方案1】:

我建议您不要使用 JLayeredPane 作为 GUI 的整体布局。使用 BoxLayout 或 BorderLayout,然后只在需要分层的地方使用 JLayeredPane。此外,在将组件添加到 JLayeredPane 时,请使用带有 Component 和 Integer 的 add 方法。不要拨打add(...),然后拨打setLayer(...)

编辑:你可以使用setLayer(...)。我以前从未使用过它,但根据 API,这是设置层的一种方式。

例如,

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();
         }
      });
   }
}

【讨论】:

  • 哎呀,你让我希望我之前没有对此进行过投票(它被标记为正确&)你发布了源代码。我可以再来一次吗?
  • 我认为(eeeerrrght Jeanette 又是 rigt)是时候使用 JXLayer(更好和最安全的 --->)或其简化的实现,如 JLayer 到 Java7
猜你喜欢
  • 2012-07-16
  • 2013-07-13
  • 2016-12-14
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
  • 2011-08-16
  • 2011-02-12
  • 2011-11-25
相关资源
最近更新 更多