简体   繁体   中英

Why setVisibleRowCount() does not work in this situation?

I am just learning Swing. I am trying my best to create a menu for a nutritional value calculator. I don't understand why the setVisibleRowCount() method does not work. I set the value to 1, but the rows are still visible. I would love a good explanation on the problem.

package inFine;

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

public class Main {

    public static void main(String[] args) {
        JFrame frame=new JFrame("Calculator Valori nutritive");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.setLocation(500,100);
        frame.setLayout(new BorderLayout());
        String []MicDejun= {"Cereale","Ou fiert","Ou prajit","Omleta","Salata rosii","Salata cu bacon ","Ovaz","Sandwitch"};
        String []Pranz= {"Spanac","Cartofi prajiti","Linte","Piure","Orez Simplu", "Orez Legume","Brocoli","Piept de pui "
                ,"Macrou","Somon","Dorada","Ceafa de porc"};
        String []Cina= {"Lasagna","Paste","Pizza","Fasole carnati","Encilada","Tigaie picanta","Tigaie tiganeasca","Tocanita"};
        String []Snack= {"Chips Lays","Chips Lidl","Chips Carefur","Ciocolata","Corn","Inghetata","Placinta"};
        
        JPanel P_Centru,P_Nord = new JPanel(),P_South=new JPanel();
        JPanel P_MicDejun,P_Pranz,P_Cina,P_Snack;
        JLabel T_MicDejun=new JLabel("Mic Dejun: ");
        JLabel T_Pranz=new JLabel("Pranz: ");
        JLabel T_Cina=new JLabel("Cina: ");
        JLabel T_Snack=new JLabel("Snack: ");
        
        JList <String>L_MicDejun=new JList<>(MicDejun);
        L_MicDejun.setVisibleRowCount(1);
        JList <String>L_Pranz=new JList<>(Pranz);
        L_Pranz.setVisibleRowCount(1);
        JList <String>L_Cina=new JList<>(Cina);
        L_Cina.setVisibleRowCount(1);
        JList <String>L_Snacks=new JList<>(Snack);
        L_Snacks.setVisibleRowCount(1);
        
        JScrollPane scr1=new JScrollPane(L_MicDejun);
        JScrollPane scr2=new JScrollPane(L_Pranz);
        JScrollPane scr3=new JScrollPane(L_Cina);
        JScrollPane scr4=new JScrollPane(L_Snacks);
            
        P_MicDejun=new JPanel();
        set(T_MicDejun);
        P_MicDejun.add(T_MicDejun);
        P_MicDejun.add(L_MicDejun);
        P_MicDejun.add(scr1);
        
        P_Pranz=new JPanel();
        set(T_Pranz);
        P_Pranz.add(T_Pranz);
        P_Pranz.add(L_Pranz);
        P_Pranz.add(scr2);
        
        P_Cina=new JPanel();
        set(T_Cina);
        P_Cina.add(T_Cina);
        P_Cina.add(L_Cina);
        P_Cina.add(scr3);
        
        P_Snack=new JPanel();
        set(T_Snack);
        P_Snack.add(T_Snack);
        P_Snack.add(L_Snacks);
        P_Snack.add(scr4);
        
        P_Centru=new JPanel();
        P_Centru.setLayout(new GridLayout(4,1));
        P_Centru.add(P_MicDejun);
        P_Centru.add(P_Pranz);
        P_Centru.add(P_Cina);
        P_Centru.add(P_Snack);
        
        frame.add(BorderLayout.NORTH,P_Nord);
        frame.add(BorderLayout.CENTER,P_Centru);
        frame.add(BorderLayout.SOUTH,P_South);          
    }

    public static void set (JLabel a)
    {
        a.setFont(new java.awt.Font("Arial",Font.PLAIN,12));
        a.setForeground(Color.black);
    }
        
} 

The fundamental problem is that a component can only appear in one container. The tables are added to the scroll panes in their constructor. They do not need to be separately added to.. anything else. Here is the immediate effect of doing that.

在此处输入图像描述

Note: Adding a call to frame.pack(); after everything else in the constructor would make the components in the GUI appear more reliably. There are other problems with the code as well, but the important things are the basics covered above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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