快乐打码 2023-04-25 19:10 采纳率: 70.6%
浏览 15
已结题

想做成图二的效果但是做成了图一的效果,可以怎样挽救一下吗


package chap;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.LayoutManager;

import javax.swing.*;

public class test8 extends JFrame {

    public test8() {
        JPanel northPanel = new JPanel();
        JPanel southPanel = new JPanel();
        JPanel westPanel = new JPanel();
        JPanel eastPanel = new JPanel();
        JPanel centerPanel = new JPanel();
        JButton button1=new JButton("北方参战者");
        JButton button2=new JButton("西方观察团");
        JButton button3=new JButton("东方观察团");
        JButton button4=new JButton("南方参与者");
        northPanel.add(button1,BorderLayout.NORTH);
        southPanel.add(button2,BorderLayout.WEST);
        westPanel.add(button3,BorderLayout.EAST);
        eastPanel.add(button4,BorderLayout.SOUTH);
        centerPanel.setLayout(new GridLayout(12, 12));

        for (int i = 0; i < 144; i++) {
            JPanel tempPanel = new JPanel();
            if ((i / 12) % 2 == 0) {
                if (i % 2 == 0) {
                    tempPanel.setBackground(Color.WHITE);
                } else {
                    tempPanel.setBackground(Color.BLACK);
                }
            } else {
                if (i % 2 == 0) {
                    tempPanel.setBackground(Color.BLACK);
                } else {
                    tempPanel.setBackground(Color.WHITE);
                }
            }
            centerPanel.add(tempPanel);
        }
        getContentPane().add(northPanel, "North");
        getContentPane().add(southPanel, "South");
        getContentPane().add(westPanel, "West");
        getContentPane().add(eastPanel, "East");
        getContentPane().add(centerPanel, "Center");

        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String[] args) {
        new test8();
    }
}

img

img

  • 写回答

1条回答 默认 最新

  • pzzhao 2023-04-25 19:48
    关注

    除了中间的设置面板,四周的不要使用面板,直接把按钮添加过去就可以。
    代码如下

    public class test8 extends JFrame {
     
        public test8() {
    //        JPanel northPanel = new JPanel();
    //        JPanel southPanel = new JPanel();
    //        JPanel westPanel = new JPanel();
    //        JPanel eastPanel = new JPanel();
            JPanel centerPanel = new JPanel();
            JButton button1=new JButton("北方参战者");
            JButton button2=new JButton("西方观察团");
            JButton button3=new JButton("东方观察团");
            JButton button4=new JButton("南方参与者");
            getContentPane().add(button1,BorderLayout.NORTH);
            getContentPane().add(button2,BorderLayout.WEST);
            getContentPane().add(button3,BorderLayout.EAST);
            getContentPane().add(button4,BorderLayout.SOUTH);
            centerPanel.setLayout(new GridLayout(12, 12));
     
            for (int i = 0; i < 144; i++) {
                JPanel tempPanel = new JPanel();
                if ((i / 12) % 2 == 0) {
                    if (i % 2 == 0) {
                        tempPanel.setBackground(Color.WHITE);
                    } else {
                        tempPanel.setBackground(Color.BLACK);
                    }
                } else {
                    if (i % 2 == 0) {
                        tempPanel.setBackground(Color.BLACK);
                    } else {
                        tempPanel.setBackground(Color.WHITE);
                    }
                }
                centerPanel.add(tempPanel);
            }
    //        getContentPane().add(northPanel, "North");
    //        getContentPane().add(southPanel, "South");
    //        getContentPane().add(westPanel, "West");
    //        getContentPane().add(eastPanel, "East");
            getContentPane().add(centerPanel, "Center");
     
            setSize(300, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
        public static void main(String[] args) {
            new test8();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月3日
  • 已采纳回答 4月25日
  • 创建了问题 4月25日

悬赏问题

  • ¥15 暴雪战网api相关问题
  • ¥15 而使用UE5引擎的 工具选项里 打开c++ visual studio 就会有部分显示加载失败 如图 加载失败的这张图 请问是什么原因
  • ¥15 mysql 对多个字段模糊查询,返回第一个匹配的字段
  • ¥15 the testing results of the whole dataset is empty
  • ¥15 can问题,往哥解决
  • ¥15 FFmpeg 成功推流到 Nginx RTMP 服务器但无法用 ffplay 或 VLC 播放
  • ¥15 请修改以下C语言代码使其能正确输出最短路径
  • ¥20 抖音商城拉码器安卓报错求解决办法或者有新的拉码脚本也可以介绍一下
  • ¥15 MPLAB IDE V2.35 报错make[2]: *** [build/default/production/_ext/1472/MSSP_I2C.p1] Error 1
  • ¥15 在国外文献网站里点击view pdf 加载异常缓慢甚至加载不出来。