使用BorderLayout将2个JLists添加到JPanel

时间:2015-04-11 19:33:29

标签: java

我需要在JPanel中为填字游戏添加2个JLists。 JPanel位于SOUTH,我在构造函数中使用BorderLayout来定位JPanel。

问题是,我无法在JPanel中看到2个JLists

package crossword;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main extends JFrame implements MouseListener {

Container container;

//this is the constructor
public Main(){
    super("Crossword");
    container = getContentPane();
    container.setLayout(new BorderLayout(1, 1));
    container.setBackground(new Color(91,119,162));
    container.add(login(),BorderLayout.NORTH);
    container.add(buttons(),BorderLayout.WEST);
    container.add(clues(),BorderLayout.SOUTH);
    container.add(createMatrix(crosswordPanel()),BorderLayout.CENTER);
    container.add(eastPanel(),BorderLayout.EAST);

}


public JPanel login(){

    JPanel loginPanel = new JPanel();
    loginPanel.setLayout(new FlowLayout(1, 15, 5));
    loginPanel.setPreferredSize(new Dimension(8, 40));
    loginPanel.setBackground(new Color(47,47,47));
    loginPanel.setVisible(true);

    JTextField inputUserName = new JTextField(15);
    loginPanel.add(inputUserName);

    JButton btnEnter = new JButton("Play!");
    btnEnter.setPreferredSize(new Dimension(80,25));
    loginPanel.add(btnEnter);

    return loginPanel;
}

public JPanel buttons(){

    JPanel buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new FlowLayout(5, 45, 15));
    buttonsPanel.setPreferredSize(new Dimension(250, 200));
    buttonsPanel.setBackground(new Color(91,119,162));
    buttonsPanel.setVisible(true);

    JButton newGame = new JButton("New game");
    newGame.setPreferredSize(new Dimension(130,27));
    newGame.setFont(new Font("Tahoma", Font.PLAIN, 11));
    buttonsPanel.add(newGame);

    JButton showChar = new JButton("Reveal letter");
    showChar.setPreferredSize(new Dimension(130,27));
    showChar.setFont(new Font("Tahoma", Font.PLAIN, 11));
    buttonsPanel.add(showChar);

    JButton showWord = new JButton("Reveal word");
    showWord.setPreferredSize(new Dimension(130,27));
    showWord.setFont(new Font("Tahoma", Font.PLAIN, 11));
    buttonsPanel.add(showWord);

    JButton verifyWord = new JButton("Verify");
    verifyWord.setPreferredSize(new Dimension(130,27));
    verifyWord.setFont(new Font("Tahoma", Font.PLAIN, 11));
    buttonsPanel.add(verifyWord);

    JButton revealAll = new JButton("Solution");
    revealAll.setPreferredSize(new Dimension(130,27));
    revealAll.setFont(new Font("Tahoma", Font.PLAIN, 11));
    buttonsPanel.add(revealAll);

    JLabel labelScore = new JLabel("Score:");
    labelScore.setFont(new Font("Arial", Font.BOLD, 13));
    buttonsPanel.add(labelScore);

    return buttonsPanel;
}

public JPanel clues(){

    JPanel clues = new JPanel();
    clues.setLayout(new FlowLayout(3, 1, 1));
    clues.setPreferredSize(new Dimension(200, 200));
    clues.setBackground(new Color(91,119,162));
    clues.setVisible(true);

    JList horizontalList = new JList();
    horizontalList.setBackground(Color.BLUE);
    horizontalList.setBorder(BorderFactory.createEtchedBorder(1));
    clues.add(horizontalList);

    JList verticalList = new JList();
    verticalList.setBackground(Color.ORANGE);
    verticalList.setBorder(BorderFactory.createEtchedBorder(1));
    clues.add(verticalList);

    return clues;
}

public JPanel eastPanel(){

    JPanel fill = new JPanel();
    fill.setLayout(new FlowLayout(5, 25, 15));
    fill.setPreferredSize(new Dimension(50, 200));
    fill.setBackground(new Color(91,119,162));
    fill.setVisible(true);

    return fill;
}



public JPanel crosswordPanel(){

    JPanel crosswordPanel = new JPanel();
    crosswordPanel.setLayout(new GridLayout(10,10,0,0));
    crosswordPanel.setBackground(Color.white);
    //crosswordPanel.setPreferredSize(new Dimension(200, 200));
    crosswordPanel.setBackground(new Color(91,119,162));
    crosswordPanel.setVisible(true);

    return crosswordPanel;
}

public JPanel createMatrix(JPanel crosswordPanel){

JPanel crosswordMatrix [][] = new JPanel [10][10]; 

    for (int i = 0; i < crosswordMatrix.length ; i++) {
        for (int j = 0; j < crosswordMatrix[0].length; j++) {

            crosswordMatrix[i][j] = new JPanel();
            crosswordMatrix[i][j].setBackground(Color.lightGray);
            crosswordMatrix[i][j].setBorder(BorderFactory.createLineBorder(Color.black, 1));
            crosswordMatrix[i][j].addMouseListener(this);
            //crosswordMatrix[i][j].add(label);

            crosswordPanel.add(crosswordMatrix[i][j]);
        }
    }

return crosswordPanel;
}


public static void main(String[] args) {

    Main guiWindow = new Main();


    Toolkit screenSize = Toolkit.getDefaultToolkit();
    int width = screenSize.getScreenSize().width * 4/5;
    int height = screenSize.getScreenSize().height * 4/5;
    guiWindow.setSize(width, height);
    guiWindow.setLocationRelativeTo(null);

    guiWindow.setVisible(true);
    guiWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

1 个答案:

答案 0 :(得分:0)

它不是MCVE,而是基于您发布的有限代码:在线索方法中构建了JPanel,但是没有添加任何内容。相反,您将JLists直接添加到包含该方法的对象(我认为这扩展了JFrame)。也许尝试将组件添加到JPanel

public JPanel clues(){
    JPanel clues = new JPanel();
    ...
    clues.add(horizontalList);
    ....
    clues.add(verticalList);        
}
相关问题