将 JPanel 添加到 JFrame 会失去组织组件的能力 - Java
Adding JPanel to JFrame losing ability to organize components - Java
描述:我一直致力于这个小项目,我需要根据传递的参数 (1-4) 从 class 向主 JFrame 发送一个特定的 JPanel。在我的主 class 中,我设置了一个 JFrame,因此我可以直观地检查正在传递的面板。
What doesn't work
在 "Accessor Class" 中,我似乎无法将 JComboBox 定位在面板的中间。另外,我不太确定我能做任何类型的定位。我之前使用完全相同的代码实现了一个按钮(我替换了 JComboBox),但我也无法调整按钮的大小。但是......我可以改变它的颜色。
GridBagLayout 应默认居中。为什么这会被覆盖?如果您查看提供的 picture/link,它会自行移动到顶部中心。简直搬不动了
这个问题是我从 class 接收 JPanel 的方式造成的吗?有没有更好的方法可以调用面板。
抱歉不够清晰。努力理解 Java 中的一些基本概念。
感谢任何帮助。
This is the main class
public static void main(String[] args) {
Accessor accessorOne = new Accessor(1); //Creates the Panel with param 1
JFrame frame = new JFrame("Testing");
frame.setLayout(new GridLayout(2,2));
frame.add(new JButton("Button 2"));
frame.add(new JButton("Button 3"));
frame.add(new JButton("Button 4"));
frame.add(accessorOne); //Adds the Panel to the last spot in the JFrame
frame.setSize(650, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
This is the "Accessor" class that defines the panel
public class Accessor extends JPanel{
JPanel panel = new JPanel();
public Accessor(int num){
if(num == 1){
panel.setLayout(new GridBagLayout());
String[] choice1 = {"Testing One", "Testing two" };
JComboBox choiceBoxOne = new JComboBox(choice1);
choiceBoxOne.setBackground(Color.red); //These changes are correctly reflected!
panel.add(choiceBoxOne);
choiceBoxOne.setLocation(300,300); //ERROR -> Setting this value changes nothing!
add(panel);
}
// Other num options
}
}
This is the photo of the Jframe
问题是您在 class Accessor
(也是默认布局为 FlowLayout 的面板)中使用 panel
。因此,与其创建另一个面板实例,不如将 GridBagLayout 用于 Accessor
的实例,并将控件直接添加到 Accessor
而不是新的 panel
.
绝对定位同样适用于 null
布局(不推荐)。
另外不要忘记将 GridBagConstraints
与 GridBagLayout
一起使用。
描述:我一直致力于这个小项目,我需要根据传递的参数 (1-4) 从 class 向主 JFrame 发送一个特定的 JPanel。在我的主 class 中,我设置了一个 JFrame,因此我可以直观地检查正在传递的面板。
What doesn't work
在 "Accessor Class" 中,我似乎无法将 JComboBox 定位在面板的中间。另外,我不太确定我能做任何类型的定位。我之前使用完全相同的代码实现了一个按钮(我替换了 JComboBox),但我也无法调整按钮的大小。但是......我可以改变它的颜色。
GridBagLayout 应默认居中。为什么这会被覆盖?如果您查看提供的 picture/link,它会自行移动到顶部中心。简直搬不动了
这个问题是我从 class 接收 JPanel 的方式造成的吗?有没有更好的方法可以调用面板。
抱歉不够清晰。努力理解 Java 中的一些基本概念。
感谢任何帮助。
This is the main class
public static void main(String[] args) {
Accessor accessorOne = new Accessor(1); //Creates the Panel with param 1
JFrame frame = new JFrame("Testing");
frame.setLayout(new GridLayout(2,2));
frame.add(new JButton("Button 2"));
frame.add(new JButton("Button 3"));
frame.add(new JButton("Button 4"));
frame.add(accessorOne); //Adds the Panel to the last spot in the JFrame
frame.setSize(650, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
This is the "Accessor" class that defines the panel
public class Accessor extends JPanel{
JPanel panel = new JPanel();
public Accessor(int num){
if(num == 1){
panel.setLayout(new GridBagLayout());
String[] choice1 = {"Testing One", "Testing two" };
JComboBox choiceBoxOne = new JComboBox(choice1);
choiceBoxOne.setBackground(Color.red); //These changes are correctly reflected!
panel.add(choiceBoxOne);
choiceBoxOne.setLocation(300,300); //ERROR -> Setting this value changes nothing!
add(panel);
}
// Other num options
}
}
This is the photo of the Jframe
问题是您在 class Accessor
(也是默认布局为 FlowLayout 的面板)中使用 panel
。因此,与其创建另一个面板实例,不如将 GridBagLayout 用于 Accessor
的实例,并将控件直接添加到 Accessor
而不是新的 panel
.
绝对定位同样适用于 null
布局(不推荐)。
另外不要忘记将 GridBagConstraints
与 GridBagLayout
一起使用。