簡體   English   中英

Jpanel放在另一個面板的相同位置

[英]Jpanel placed on the same location at one other panel

public class DataGUI {  
    private static int option;

    public static void main(String args[]) {


        JFrame frame = new JFrame();
        frame.setSize(900, 700);
        JPanel weatherPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel healthPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel regionPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

        //set panel size
        weatherPanel.setSize(900, 100);
        healthPanel.setBounds(0, 100, 900, 100);
        regionPanel.setBounds(0, 200, 900, 100);
        // create labels
        JLabel cityLabel = new JLabel("Enter city name");
        JLabel yearLabel = new JLabel("Enter the year");
        JLabel monthLabel = new JLabel("Enter the month");
        JLabel monthlyMaxLabel = new JLabel("Enter Monthly max");
        JLabel monthlyMinLabel = new JLabel("Enter Monthly min");
        JLabel monthlyNorLabel = new JLabel("Enter Monthly norm");
        JLabel zipcodeLabel = new JLabel("Enter the zipcode");
        JLabel countyLabel = new JLabel("Enter the county");
        JLabel yearHealthLabel = new JLabel("Enter the year");
        JLabel agegroupLabel = new JLabel("Enter the age group");
        JLabel numberOfVisitLabel= new JLabel("Enter the number of visit");
        JLabel stateLabel = new JLabel("Enter the state");
        JLabel cityRegionLabel = new JLabel("Enter city name");
        JLabel countyRegionLabel = new JLabel("Enter county name");
        JLabel zipcodeRegionLabel = new JLabel("Enter the zipcode");



        // create text field
        JTextField yearWeatherText = new JTextField();
        JTextField cityWeatherText = new JTextField();
        JTextField monthText = new JTextField();
        JTextField monthlyMaxText = new JTextField();
        JTextField monthlyMinText = new JTextField();
        JTextField monthlyNorText = new JTextField();
        JTextField zipcodeHealthText = new JTextField();
        JTextField countyHealthText = new JTextField();
        JTextField yearHealthText = new JTextField();
        JTextField ageGroupText = new JTextField();
        JTextField numVisitText = new JTextField();
        JTextField countyRegionText = new JTextField();
        JTextField zipcodeRegionText = new JTextField();
        JTextField cityRegionText = new JTextField();
        JTextField stateText = new JTextField();


        // set textfields size
        cityWeatherText.setPreferredSize(new Dimension(100,20));
        yearWeatherText.setPreferredSize(new Dimension(100,20));
        monthText.setPreferredSize(new Dimension(100,20));
        monthlyMaxText.setPreferredSize(new Dimension(100,20));
        monthlyMinText.setPreferredSize(new Dimension(100,20));
        monthlyNorText.setPreferredSize(new Dimension(100,20));
        zipcodeHealthText.setPreferredSize(new Dimension(100,20));
        countyHealthText.setPreferredSize(new Dimension(100,20));
        yearHealthText.setPreferredSize(new Dimension(100,20));
        ageGroupText.setPreferredSize(new Dimension(100,20));
        numVisitText.setPreferredSize(new Dimension(100,20));
        countyRegionText.setPreferredSize(new Dimension(100,20));
        zipcodeRegionText.setPreferredSize(new Dimension(100,20));
        cityRegionText.setPreferredSize(new Dimension(100,20));
        stateText.setPreferredSize(new Dimension(100,20));


        // add to weatherPanel
        weatherPanel.add(cityLabel);
        weatherPanel.add(cityWeatherText);
        weatherPanel.add(yearLabel);
        weatherPanel.add(yearWeatherText);
        weatherPanel.add(monthLabel);
        weatherPanel.add(monthText);
        weatherPanel.add(monthlyMaxLabel);
        weatherPanel.add(monthlyMaxText);
        weatherPanel.add(monthlyMinLabel);
        weatherPanel.add(monthlyMinText);
        weatherPanel.add(monthlyNorLabel);
        weatherPanel.add(monthlyNorText);


        // add to healthPanel
        healthPanel.add(zipcodeLabel);
        healthPanel.add(zipcodeHealthText);
        healthPanel.add(countyLabel);
        healthPanel.add(countyHealthText);
        healthPanel.add(yearHealthLabel);
        healthPanel.add(yearHealthText);
        healthPanel.add(agegroupLabel);
        healthPanel.add(ageGroupText);
        healthPanel.add(numberOfVisitLabel);
        healthPanel.add(numVisitText);

        // add to regionPanel
        regionPanel.add(countyRegionLabel);
        regionPanel.add(countyRegionText);
        regionPanel.add(zipcodeRegionLabel);
        regionPanel.add(zipcodeRegionText);
        regionPanel.add(cityRegionLabel);
        regionPanel.add(cityRegionText);
        regionPanel.add(stateLabel);
        regionPanel.add(stateText);



        //add to Jframe
        frame.add(weatherPanel);
        frame.add(healthPanel);
        frame.add(regionPanel);





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

當我已經開始將區域面板放置在運行狀況面板下方時,區域面板與天氣面板位於相同的位置。 為什么setbounds方法在區域面板上不起作用,而在健康面板上卻起作用?

在此處輸入圖片說明

檢查您的代碼后,我注意到面板為何重疊。 您尚未為JFrame設置布局管理器。

frame.setLayout(new GridLayout(3,1));

我做了什么,就是在您的JFrame中添加了一個布局管理器(可以隨意使用任意一個)。 我使用了GridLayout,它在JFrame中添加了3個JPanel,從而在3行1列中顯示了組件。 這就是結果。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM