Jpanel在另一个小组的同一位置

时间:2015-11-25 05:56:20

标签: java jframe jpanel setbounds

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方法在区域面板上不起作用,而它在健康面板上有效?

enter image description here

1 个答案:

答案 0 :(得分:1)

检查完代码后,我注意到面板重叠的原因。您尚未为JFrame设置布局管理器。

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

我为你的JFrame添加了一个布局管理器(随意使用你喜欢的任何一个)。我使用了GridLayout,它在3行和1列中显示了组件,因为您在JFrame中添加了3个JPanel。这就是结果。

enter image description here