2014-12-04 60 views
0

我试图使用布局管理器创建一个简单的卡片显示程序,但我仍然没有正确使用布局管理器的窍门。这是我的代码,它使用BorderLayout();作为contentPane的布局管理器,GridLayout();用于buttonPanel,CardLayout();用于cardPanel,FlowLayout();用于labelPanel。布局管理器的使用

public class TestClass // implements ActionListener 
{ 
    static JPanel content = new JPanel(new BorderLayout()); 
    static JPanel cardPanel = new JPanel(); 
    static CardLayout cardLayout; 
    static JPanel buttonPanel = new JPanel(new FlowLayout()); 
    static JPanel labelPanel = new JPanel(new GridLayout(9,1)); 
    static JButton jack, king, queen, ace; 
    static JButton first, last, prev, next; 

    public static void main(String[] args) { 
     JFrame window = new JFrame("Window"); 

     //------Creates the Label Panel --// 
     JLabel label = new JLabel("SELECT A CARD"); 
     label.setFont(new Font("Georgia",Font.BOLD,18)); 
     labelPanel.add(label, FlowLayout.LEFT); 

     //-----Creates the Button Panel --// 
     jack = new JButton("Jack"); 
     queen = new JButton("Queen"); 
     king = new JButton("King"); 
     ace = new JButton("Ace"); 
     first = new JButton("First"); 
     last = new JButton("Last"); 
     prev = new JButton("Previous"); 
     next = new JButton("Next"); 
     JLabel sep = new JLabel("-------"); 
     buttonPanel.add(jack); 
     buttonPanel.add(queen); 
     buttonPanel.add(king); 
     buttonPanel.add(ace); 
     buttonPanel.add(sep); 
     buttonPanel.add(first); 
     buttonPanel.add(last); 
     buttonPanel.add(prev); 
     buttonPanel.add(next); 

     //-------Creates the cardPanel --// 
     JLabel card1 = new JLabel("JACK"); 
     JLabel card2 = new JLabel("QUEEN"); 
     JLabel card3 = new JLabel("KING"); 
     JLabel card4 = new JLabel("ACE"); 
     cardPanel.add(card1, "jack"); 
     cardPanel.add(card2, "queen"); 
     cardPanel.add(card3, "king"); 
     cardPanel.add(card4, "ace"); 
     cardPanel.setLayout(cardLayout = new CardLayout()); 
     cardLayout.first(cardPanel); 

     // ----- Adds the panels to the contentPane ---// 
     content.add(labelPanel, BorderLayout.NORTH); 
     content.add(buttonPanel, BorderLayout.WEST); 
     content.add(cardPanel, BorderLayout.CENTER); 

     window.setContentPane(content); 
     window.setLocation(300,100); 
     window.pack(); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setVisible(true); 
     window.setResizable(true); 
    } 
} 

但输出并不如预期。我想要左上角的标签,左侧的按钮(并使用网格布局),然后是中心的cardLabel。

编辑:问题解决了,我犯了一个愚蠢的错误

static JPanel buttonPanel = new JPanel(new GridLayout(9,1)); //previously FlowLayout 
    static JPanel labelPanel = new JPanel(new FlowLayout()); //previously GridLayout 

回答

1

在网格布局(labelPanel)的唯一事情是这样的:

JLabel label = new JLabel("SELECT A CARD"); 

其他的都是在buttonPanel,这是一个流程布局。

检查了这一点:https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

+0

感谢您的输入,但我得到它....在这里做了一个愚蠢的错误静态的JPanel buttonPanel =新JPanel(新的GridLayout(9,1)); //以前的FlowLayout(); static JPanel labelPanel = new JPanel(new FlowLayout()); //先前GridLayout(9,1) – 2014-12-04 07:19:28

+0

我们都犯了愚蠢的错误,我的男人。你把它修好了吗? – 2014-12-04 07:20:16

+0

确实没有先生,我使用的按钮面板而不是gridlayout的flowlayout,因为我最初写在我的psedocode。它有趣,我没有发现它。谢谢 – 2014-12-04 07:23:11