gpt4 book ai didi

java - 显示具有两个面板的 GUI,每个面板包含三个按钮

转载 作者:行者123 更新时间:2023-12-02 06:32:54 25 4
gpt4 key购买 nike

我需要设计和实现具有多个操作组件的基本 GUI 方面的帮助。目标是创建两个面板和六个按钮。每个面板有三个按钮。

到目前为止,我是在 Java 虚拟机中编写代码的。我的代码看起来很正确,但输出不显示按钮。您能帮忙找出按钮不显示的原因吗?

顺便说一句,我有一张照片。我想要我的设计像这样。

enter image description here http://i1199.photobucket.com/albums/aa467/Jordan_Sanjaya/Picture1.png

我的代码:

import javax.swing.*;
import java.awt.*;

public class FlowlayoutExperiment extends JFrame {

FlowLayout experimentLayout = new FlowLayout();

private JButton firstButton = new JButton("Button 1");
private JButton secondButton = new JButton("Button 2");
private JButton thirdButton = new JButton("Button 3");
private JButton fourthButton = new JButton("Button 4");
private JButton fifthButton = new JButton("Button 5");
private JButton sixthButton = new JButton("Button 6");

public FlowlayoutExperiment ()
{

JPanel group1 = new JPanel();
setLayout(new GridLayout(3,1));

group1.add(firstButton);
group1.add(secondButton);
group1.add(thirdButton);

JPanel group2 = new JPanel();
setLayout(new GridLayout(3,1));
group2.add(fourthButton);
group2.add(fifthButton);
group2.add(sixthButton);

}
public static void main(String[] args)
{

FlowlayoutExperiment frame = new FlowlayoutExperiment();
frame.setTitle("Button Panel Example");
frame.setSize(600, 75);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}
}

最佳答案

您似乎忘记将组添加到任何内容

public FlowlayoutExperiment ()
{

JPanel group1 = new JPanel();
setLayout(new GridLayout(3,1));

group1.add(firstButton);
group1.add(secondButton);
group1.add(thirdButton);

JPanel group2 = new JPanel();
setLayout(new GridLayout(3,1));
group2.add(fourthButton);
group2.add(fifthButton);
group2.add(sixthButton);

// This is important ;)
add(group1);
add(group2);

}

您还设置框架的布局,而不是组的布局

你可能想要改变...

    JPanel group1 = new JPanel();
setLayout(new GridLayout(3,1));
//...
JPanel group2 = new JPanel();
setLayout(new GridLayout(3,1));

    JPanel group1 = new JPanel();
group1.setLayout(new GridLayout(3,1));
//...
JPanel group2 = new JPanel();
group2.setLayout(new GridLayout(3,1));

关于java - 显示具有两个面板的 GUI,每个面板包含三个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19919963/

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