【问题标题】:3 panels ontop 1 frame1 个框架上的 3 个面板
【发布时间】:2012-11-26 18:48:47
【问题描述】:

我在将三个不同的面板放在框架顶部时遇到了真正的麻烦,因为我需要在每个面板上具有不同的布局。我似乎无法让它工作,我已经连续尝试了 4 天。我在这段代码中找不到我哪里出错了。

我这样做是最好的方式吗?任何想法或帮助将不胜感激!!!!!!

我的代码:

    public Mem() {
    super("3 panels on 1 frame");       

    JFrame frame = new JFrame();

    setSize(500, 500);      
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();

    //Adding three different panels with borderlayout
    frame.setLayout(new BorderLayout());  

    //Panel 1 is the Player Panel with labels
    JLabel ply1 = new JLabel("Player 1");
    JLabel ply2 = new JLabel("Player 2");
    JLabel ply3 = new JLabel("Player 3");
    JLabel ply4 = new JLabel("Player 4");
    p1.add(ply1); p1.add(ply2); p1.add(ply3); p1.add(ply4);

    //Panel 2 is the game panel with playingcards on it. (Clickable buttons)
    JButton card1 = new JButton("Card");
    p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1);
    p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1);

    //Panel 3 is the lower panel with the buttons new game & close on it. 
    JButton newGame = new JButton("New Game");
    newGame.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            startGame();
        }
    });
    JButton endGame = new JButton("Close");
    endGame.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            closeGame();
        }
    });
    p3.add(newGame);  
    p3.add(endGame);


    frame.add(p1, BorderLayout.EAST);  
    frame.add(p2, BorderLayout.CENTER);  
    frame.add(p3, BorderLayout.SOUTH);

    setVisible(true);   
}

【问题讨论】:

  • @user1501127 你还没有告诉我们它出了什么问题。您期望它做什么以及它对当前代码的作用是什么?使用 setLayout 在面板上放置布局

标签: java swing


【解决方案1】:

有几点:

1) 您多次添加摆动组件不会复制它 - 它将被删除并添加到新位置。所以这个:

JButton card1 = new JButton("Card");
p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1);
p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1);

和这个是一样的:

JButton card1 = new JButton("Card");
p2.add(card1);

2) 看来您的课程扩展了JFrame。如果是这样,请删除所有对JFrame frame = new JFrame(); 的引用。通过使用frame 变量,您正在创建一个框架,您正在向其中添加面板,但不显示。所以无论你在哪里看到 frame. 都删除它。

例子:

frame.setLayout(new BorderLayout());  

变成

setLayout(new BorderLayout());  

3) 我相信您之前提出的一些问题已经得到了可以接受的答案,或者您自己想出了一个可以接受的答案。您可以奖励那些提供帮助的人,或者您可以提供您找到的答案并接受它。那么更多的人会花时间为你提供一个好的答案。这对你更好,对我们更好,对和你有同样问题的随机人更好。

【讨论】:

  • +1:setLayout(new BorderLayout()); 行可以完全删除,因为这是JFrame 的默认布局。 :)
【解决方案2】:

您似乎将面板附加到JFrame,而该Mem 框架构造函数中永远不会显示该面板。您可以删除或评论该行:

JFrame frame = new JFrame();

并使用:

add(p1, BorderLayout.EAST);
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.SOUTH);

然后,您将能够看到组件在您应用的任何布局的面板中是如何排列的:

p1.setLayout(new GridLayout(2, 2));
// etc.

【讨论】:

  • +1 可以很好地回答这个问题。我只发布了我自己的答案,因为还有一些其他的小事情,直到我尝试解决 OP 的问题,它并没有融入你所说的内容。
  • add(p1, BorderLayout.EAST); .. add(p3, BorderLayout.SOUTH); 也可以考虑使用add(p1, BorderLayout.LINE_END); .. add(p3, BorderLayout.PAGE_END);,它可以更好地适应不同的文本方向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 2016-03-26
相关资源
最近更新 更多