简体   繁体   English

为什么 setVisibleRowCount() 在这种情况下不起作用?

[英]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.我刚刚学习 Swing。我正在尽力为营养价值计算器创建一个菜单。 I don't understand why the setVisibleRowCount() method does not work.我不明白为什么setVisibleRowCount()方法不起作用。 I set the value to 1, but the rows are still visible.我将该值设置为 1,但行仍然可见。 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();注意:添加对frame.pack(); after everything else in the constructor would make the components in the GUI appear more reliably.在构造函数中的所有其他内容之后会使 GUI 中的组件看起来更可靠。 There are other problems with the code as well, but the important things are the basics covered above.代码还有其他问题,但重要的是上面介绍的基础知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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