繁体   English   中英

JFrame中JPanel上的JPanel不会显示

[英]JPanel on a JPanel in a JFrame will not show

您好我是一名业余爱好者,他试图通过为射箭写一张记分卡来学习/提高我对Java的理解。 我正在尝试制作一个GUI,到目前为止已经成功地在JPanel上制作了一排18个不同大小和颜色的标签,适合评分十几个。
然后我尝试将这些“标签面板”中的五个添加到另一个面板以构建网格,并且在某些情况下无需创建和添加多达150个标签。 迄今为止,原始标签面板不会显示,因此没有成功。 所有面板都显示在JFrame上

我已经尝试了许多不同的方法来使代码工作,使用Java教程和谷歌搜索互联网并研究这个网站上的类似问题,但我会围成一圈。 我一定错过了某个地方,希望你能帮忙。

我使用Java 6和JGrasp v1.8.8_01作为IDE

标签面板的以下代码已被删除,因为它的重复性很高。

import javax.swing.*;  
import java.awt.*;  
public class ArrowScoreLabels extends JPanel{
  public  JPanel createContentPane(){
    JPanel panelForLabels = new JPanel();
    panelForLabels.setLayout(null);
//Code creates 18 labels, sets the size, position, background colours, border and    
//font and adds the labels to the’panelForLabels
    JLabel scorelabel1;
     scorelabel1 = new JLabel("",JLabel.CENTER);
     scorelabel1.setBorder(BorderFactory.createLineBorder(Color.black)); 
     scorelabel1.setFont(new Font("Arial", Font.ITALIC, 26));  
     scorelabel1.setLocation(0, 0);//first value differs for each label
     scorelabel1.setSize(35, 35);
     scorelabel1.setOpaque(true);

        panelForLabels.add(scorelabel1);
        panelForLabels.setOpaque(true);
    return panelForLabels;
  }
}  

运行以下类将在面板上显示18个标签

import javax.swing.*;
import java.awt.*; 
public class TestArrowScoreLabels {

  private static void createAndShowArrowLabels() {
 //Create and set up the window.
  JFrame frame = new JFrame("To score one dozen");
 //Create and set up the content pane.
  ArrowScoreLabels asl = new ArrowScoreLabels();
  frame.setContentPane(asl.createContentPane()); 
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  frame.setSize(676, 73);
  frame.setVisible(true);
  }
//Main method to show the GUI/
  public static void main(String[] args) {
    SwingUtilities.invokeLater(
     new Runnable() {
      public void run() {
      createAndShowArrowLabels();
      }
    });
  }
}

第二个面板的以下代码类似,编译但仅显示第二个绿色JPanel而不是带有标签的面板。

import javax.swing.*;
import java.awt.*;


public class FiveDozenScorePanel{
 public JPanel createContentPane(){
  //A bottom JPanel on which to place five dozenpanels.
  JPanel fivedozenpanel = new JPanel();
  fivedozenpanel.setLayout(null); //requires absolute spacing
  fivedozenpanel.setSize(676,185);  
  fivedozenpanel.setBackground(Color.green);
  //Label panels for five dozen
  ArrowScoreLabels dozenscorepanel1, dozenscorepanel2,
         dozenscorepanel3,dozenscorepanel4,dozenscorepanel5;   
  //Create the 5 dozenscorelabels.
  dozenscorepanel1 = new ArrowScoreLabels();         
  dozenscorepanel1.setLocation(5,5);//y value changes for each panel

  fivedozenpanel.add(dozenscorepanel1);//plus the other 4
  fivedozenpanel.setOpaque(true);
  return fivedozenpanel;
}

private static void createAndShowDozenPanels() {
 JFrame frame = new JFrame("To score five dozen");
  FiveDozenScorePanel fdsp = new FiveDozenScorePanel();
  frame.setContentPane(fdsp.createContentPane());   
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  //Display the window  
  frame.setSize(700, 233);
  frame.setVisible(true);
 }   

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

我也尝试过frame.getContentPane()。add(fdsp); - frame.pack(); 并且阅读了很多关于绘画方法的内容,我完全感到困惑。 我可以将ArrowScoreLabels图像直接显示在JFrame而不是JPanel上,但只显示其中一个而不是五个。

我希望被指向正确的方向。 感谢您的时间。

更新 - 2010年12月14日我已经设法在JFrame上的另一个Jpanel上显示panelForLabels Jpanel。 这是通过将以下代码添加到ArrowScoreLabels类来完成的。 原始createContentPane()方法重命名为createRowOne()。 panelForLabels的颜色为红色,五个面板为黄色,以确定哪个显示。 然而,尽管进行了大量的实验和研究,我仍然只能说服该程序显示一行标签。

public  static JPanel createContentPane(){
   //Bottom panelto hold rows of labels
     JPanel fivedozenscorepanel = new JPanel();      
     fivedozenscorepanel.setLayout(null);//requires absolute spacing
     fivedozenscorepanel.setSize(660,180);
     fivedozenscorepanel.setBackground(Color.yellow);
     fivedozenscorepanel.add(createRowOne());

     fivedozenscorepanel.setOpaque(true);
     return fivedozenscorepanel;
  }

我显示5行18个标签的唯一方法是在ArrowScoreLabels类中创建所有90个,然后使用绝对间距将它们添加到一个JPanel,然后再添加到JFrame。 我注意到了pstantons的建议 - 谢谢你 - 我正在研究使用MigLayout Manager。

简单的回答: 使用布局管理器 不要使用绝对定位。

只是注释掉你对setLocationsetLayout所有调用,swing将使用默认的FlowLayout

要更好地控制显示,请使用其他布局管理器。

另外,如果你使用多个面板,你将无法在不同的面板中对齐东西,除非它们包含完全相同大小的相同数量的组件,因此考虑使用一个面板用于所有标签。

您可以使用MigLayout实现所需的任何布局。

编辑:在你的榜样,没有必要ArrowScoreLabels需要延长JPanel ,因为你正在做的工作createContentPane构建一个单独的JPanel。 稍后在你的代码中你调用new ArrowScoreLabels()将只返回一个空白的JPanel,而你需要调用new ArrowScoreLabels().createContentPane()

如果您希望ArrowScoreLabels扩展JPanel,请实现public ArrowScoreLabels()即构造函数而不是createContentPane

我的印象是你没有为你的dozenscorepanel1设置尺寸。 所以,设置一个大小:-)

小心布局null,因为它很痛苦; 你总是忘记一些事情。 自己写,或使用现有的。

暂无
暂无

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

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