2015-04-11 2 views
0

Мне нужно добавить 2 JLists внутри JPanel для кроссворда. JPanel расположен SOUTH, и я использую BorderLayout в конструкторе, чтобы найти JPanel.Добавление 2 JLists в JPanel с использованием BorderLayout

Проблема заключается в том, что я не могу увидеть 2 JLists внутри JPanel

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); 
} 

}

ответ

0

Его не MCVE, но на основе ограниченного кода, публикуемую: в методе улик JPanel построен, но к нему ничего не добавляется. Скорее, вы добавляете JLists непосредственно к объекту, который содержит метод (который, как я полагаю, расширяет JFrame). Возможно, попробуйте добавить компоненты в JPanel

public JPanel clues(){ 
    JPanel clues = new JPanel(); 
    ... 
    clues.add(horizontalList); 
    .... 
    clues.add(verticalList);   
} 
+0

Нет, это не сработало. – fredy21

+0

Как я уже сказал выше, это основано на ограниченном информационном фрагменте кода, отличном от MCVE. Если вам нужна дополнительная помощь, вам придется оставить гадание в стороне: опубликуйте MCVE, который воспроизводит проблему (и четко описывает проблему) – copeg

+0

Я добавил весь свой код – fredy21

Смежные вопросы