gpt4 book ai didi

java - Jpanel 放置在另一个面板的同一位置

转载 作者:行者123 更新时间:2023-12-01 10:50:59 26 4
gpt4 key购买 nike

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

最佳答案

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

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

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

enter image description here

关于java - Jpanel 放置在另一个面板的同一位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33909403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com