Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.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 将JButton放在JPanel内的JLabel顶部_Java_Swing_User Interface_Jpanel_Jlabel - Fatal编程技术网

Java 将JButton放在JPanel内的JLabel顶部

Java 将JButton放在JPanel内的JLabel顶部,java,swing,user-interface,jpanel,jlabel,Java,Swing,User Interface,Jpanel,Jlabel,我的JPanel包含: JButton和 带图片集的JLabel 我想将按钮放在JLabel顶部的北中位置。目前它的设置彼此相邻,我不知道如何覆盖JLabel 我在这里尝试了许多解决方案,但是它似乎并没有完全按照我想要的方式工作。谢谢 JLabel picLabel = new JLabel(); // background of the label picLabel.setIcon(new ImageIcon(CoursesGUI.class.getResource("/images/grap

我的JPanel包含:

JButton和 带图片集的JLabel

我想将按钮放在JLabel顶部的北中位置。目前它的设置彼此相邻,我不知道如何覆盖JLabel

我在这里尝试了许多解决方案,但是它似乎并没有完全按照我想要的方式工作。谢谢

JLabel picLabel = new JLabel(); // background of the label
picLabel.setIcon(new ImageIcon(CoursesGUI.class.getResource("/images/graph_paper.jpg")));
Button en_course_btn = new JButton("English Course");
coursePanel.add(en_course_btn);
coursePanel.add(picLabel);

更新:按钮位于JLabel顶部,但不在其内部。

您应该为标签设置布局,并在其内部添加按钮:

JPanel setupPanel = new JPanel();
JPanel titlePanel = new JPanel();
JPanel contentPanel = new JPanel();
JPanel coursePanel = new JPanel();

JLabel picLabel = new JLabel(); // background of the label
picLabel.setLayout(new FlowLayout(FlowLayout.CENTER));

picLabel.setIcon(new ImageIcon(CoursesGUI.class.getResource("/images/graph_paper.jpg")));

// picLabel.setLayout(new BorderLayout()); // sets layout inside the label
JButton en_course_btn = new JButton("English Course");

contentPanel.setBackground(Color.GREEN);
titlePanel.setBackground(Color.YELLOW);

// picLabel.setPreferredSize(new Dimension(500, 470)); // dimensions of inner containers
contentPanel.setPreferredSize(new Dimension(1280, 670));
titlePanel.setPreferredSize(new Dimension(1280, 50));

picLabel.add(en_course_btn);
//coursePanel.add(en_course_btn);
coursePanel.add(picLabel);

contentPanel.add(coursePanel); // add coursePanel containing buttons and background
setupPanel.add(titlePanel, BorderLayout.NORTH);
setupPanel.add(contentPanel);
getContentPane().add(setupPanel);

您应该为标签设置布局,并在其中添加按钮:

JPanel setupPanel = new JPanel();
JPanel titlePanel = new JPanel();
JPanel contentPanel = new JPanel();
JPanel coursePanel = new JPanel();

JLabel picLabel = new JLabel(); // background of the label
picLabel.setLayout(new FlowLayout(FlowLayout.CENTER));

picLabel.setIcon(new ImageIcon(CoursesGUI.class.getResource("/images/graph_paper.jpg")));

// picLabel.setLayout(new BorderLayout()); // sets layout inside the label
JButton en_course_btn = new JButton("English Course");

contentPanel.setBackground(Color.GREEN);
titlePanel.setBackground(Color.YELLOW);

// picLabel.setPreferredSize(new Dimension(500, 470)); // dimensions of inner containers
contentPanel.setPreferredSize(new Dimension(1280, 670));
titlePanel.setPreferredSize(new Dimension(1280, 50));

picLabel.add(en_course_btn);
//coursePanel.add(en_course_btn);
coursePanel.add(picLabel);

contentPanel.add(coursePanel); // add coursePanel containing buttons and background
setupPanel.add(titlePanel, BorderLayout.NORTH);
setupPanel.add(contentPanel);
getContentPane().add(setupPanel);

您应该为标签设置布局,并在其中添加按钮:

JPanel setupPanel = new JPanel();
JPanel titlePanel = new JPanel();
JPanel contentPanel = new JPanel();
JPanel coursePanel = new JPanel();

JLabel picLabel = new JLabel(); // background of the label
picLabel.setLayout(new FlowLayout(FlowLayout.CENTER));

picLabel.setIcon(new ImageIcon(CoursesGUI.class.getResource("/images/graph_paper.jpg")));

// picLabel.setLayout(new BorderLayout()); // sets layout inside the label
JButton en_course_btn = new JButton("English Course");

contentPanel.setBackground(Color.GREEN);
titlePanel.setBackground(Color.YELLOW);

// picLabel.setPreferredSize(new Dimension(500, 470)); // dimensions of inner containers
contentPanel.setPreferredSize(new Dimension(1280, 670));
titlePanel.setPreferredSize(new Dimension(1280, 50));

picLabel.add(en_course_btn);
//coursePanel.add(en_course_btn);
coursePanel.add(picLabel);

contentPanel.add(coursePanel); // add coursePanel containing buttons and background
setupPanel.add(titlePanel, BorderLayout.NORTH);
setupPanel.add(contentPanel);
getContentPane().add(setupPanel);

您应该为标签设置布局,并在其中添加按钮:

JPanel setupPanel = new JPanel();
JPanel titlePanel = new JPanel();
JPanel contentPanel = new JPanel();
JPanel coursePanel = new JPanel();

JLabel picLabel = new JLabel(); // background of the label
picLabel.setLayout(new FlowLayout(FlowLayout.CENTER));

picLabel.setIcon(new ImageIcon(CoursesGUI.class.getResource("/images/graph_paper.jpg")));

// picLabel.setLayout(new BorderLayout()); // sets layout inside the label
JButton en_course_btn = new JButton("English Course");

contentPanel.setBackground(Color.GREEN);
titlePanel.setBackground(Color.YELLOW);

// picLabel.setPreferredSize(new Dimension(500, 470)); // dimensions of inner containers
contentPanel.setPreferredSize(new Dimension(1280, 670));
titlePanel.setPreferredSize(new Dimension(1280, 50));

picLabel.add(en_course_btn);
//coursePanel.add(en_course_btn);
coursePanel.add(picLabel);

contentPanel.add(coursePanel); // add coursePanel containing buttons and background
setupPanel.add(titlePanel, BorderLayout.NORTH);
setupPanel.add(contentPanel);
getContentPane().add(setupPanel);


另外,不要在代码中引用
src
目录,它在运行时不存在,请使用
Class@getResoruce
并将路径传递给
/images/graph\u paper.jpg
为什么要这样做?这看起来像一个。我想把图像作为特定JPanel的背景,然后放置一些按钮。这是我设计的程序,我可以在Photoshop中使用图层轻松制作类似的东西,但是我在java中很难做到这一点。@Radiodef关于XY问题,它看起来很相似,但是它与实际代码(非常具体的信息)不同,但不是描述它所做事情的短文句子。如果解决方案不起作用,并且有人试图改进他们的解决方案,这不是问题。当您确实需要图像作为背景时,您曾询问在JLabel上放置一个JButton(解决方案是将按钮放在JLabel内或使用JPanel)。这是一个XY问题。所以我们真的不知道你想做什么。此外,我回滚了您的编辑,您不应该使用答案中的代码“更新”问题中的代码。这样做会让未来的读者感到困惑。另外,不要在代码中引用
src
目录,它在运行时不存在,而是使用
Class@getResoruce
并将路径传递给
/images/graph\u paper.jpg
为什么要这样做?这看起来像一个。我想把图像作为特定JPanel的背景,然后放置一些按钮。这是我设计的程序,我可以在Photoshop中使用图层轻松制作类似的东西,但是我在java中很难做到这一点。@Radiodef关于XY问题,它看起来很相似,但是它与实际代码(非常具体的信息)不同,但不是描述它所做事情的短文句子。如果解决方案不起作用,并且有人试图改进他们的解决方案,这不是问题。当您确实需要图像作为背景时,您曾询问在JLabel上放置一个JButton(解决方案是将按钮放在JLabel内或使用JPanel)。这是一个XY问题。所以我们真的不知道你想做什么。此外,我回滚了您的编辑,您不应该使用答案中的代码“更新”问题中的代码。这样做会让未来的读者感到困惑。另外,不要在代码中引用
src
目录,它在运行时不存在,而是使用
Class@getResoruce
并将路径传递给
/images/graph\u paper.jpg
为什么要这样做?这看起来像一个。我想把图像作为特定JPanel的背景,然后放置一些按钮。这是我设计的程序,我可以在Photoshop中使用图层轻松制作类似的东西,但是我在java中很难做到这一点。@Radiodef关于XY问题,它看起来很相似,但是它与实际代码(非常具体的信息)不同,但不是描述它所做事情的短文句子。如果解决方案不起作用,并且有人试图改进他们的解决方案,这不是问题。当您确实需要图像作为背景时,您曾询问在JLabel上放置一个JButton(解决方案是将按钮放在JLabel内或使用JPanel)。这是一个XY问题。所以我们真的不知道你想做什么。此外,我回滚了您的编辑,您不应该使用答案中的代码“更新”问题中的代码。这样做会让未来的读者感到困惑。另外,不要在代码中引用
src
目录,它在运行时不存在,而是使用
Class@getResoruce
并将路径传递给
/images/graph\u paper.jpg
为什么要这样做?这看起来像一个。我想把图像作为特定JPanel的背景,然后放置一些按钮。这是我设计的程序,我可以在Photoshop中使用图层轻松制作类似的东西,但是我在java中很难做到这一点。@Radiodef关于XY问题,它看起来很相似,但是它与实际代码(非常具体的信息)不同,但不是描述它所做事情的短文句子。如果解决方案不起作用,并且有人试图改进他们的解决方案,这不是问题。当您确实需要图像作为背景时,您曾询问在JLabel上放置一个JButton(解决方案是将按钮放在JLabel内或使用JPanel)。这是一个XY问题。所以我们真的不知道你想做什么。此外,我回滚了您的编辑,您不应该使用答案中的代码“更新”问题中的代码。这样做会让未来的读者感到困惑。@ProgLearner尝试将FlowLayout构造函数与FlowLayout.CENTER参数一起使用(请参阅我的更新答案)。@Natuto Biju Mode,我已经上传了我的全部代码,希望能有所帮助。更新后的问题是相同的:我不能将JButton放在JLabel的顶部。@ProgLearner您应该对coursePanel.add(en_course_btn)行进行注释;并添加行picLabel.add(en_course_btn);(请参阅更新的代码)@ProgLearner尝试使用FlowLayout.CENTER参数的FlowLayout构造函数(请参阅我的更新答案)。@Natuto Biju Mode,我已上载了我的全部代码,希望它可以