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);
}
}
the region panel was placed at the same location as the weather panel when I already setbound for the region panel to be placed under the health panel. Why doesn't setbounds method work on the region panel while it does work on the health panel?
Upon inspection of your code I noticed why the panels were overlapping. You had not set a layout manager for your JFrame.
frame.setLayout(new GridLayout(3,1));
What did was I added a layout manager to your JFrame(Feel free to use whichever you like). I used GridLayout which displayed the components in 3 rows and 1 column since you added 3 JPanels in the JFrame. This was the result.