当前位置: 首页 > 知识库问答 >
问题:

我的JButton显示不一致,大小不一样,位置也不相同

慕阳平
2023-03-14

JButton两个显示器有两个位置和大小,有两个类

它与大小和位置不一致,请帮助,谢谢下面的代码分为两部分

最终公共类ChessMain{

JFrame frame; 

public static void main(String[] args) {
    new ChessMain().go();
}

private void go() {
    frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //  Container contentPane = frame.getContentPane();
    frame.setContentPane(new DrawPanelChess ());
    frame.setVisible(true);
    frame.setExtendedState(Frame.MAXIMIZED_BOTH);

    Icon warnIcon = new ImageIcon("white_King_Chess.png");
    JButton button2 = new JButton(warnIcon);  
    button2.setLayout(null);
    button2.setBounds(100,0, 100, 100); 
    frame.add(button2);


 }

}

类DrawPanelChess扩展JPanel{

private static final long serialVersionUID = 9166938856558245300L;

公共无效绘制组件(图形g){

        g.setColor(Color.BLACK);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());    


        g.setColor(Color.WHITE);

        {//row 1                        
        g.fillRect(0, 0, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 4, 0, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 2, 0, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 8 * 6, 0, this.getWidth() / 8, this.getHeight() / 8);                         
        }

        {//row 2                        
        g.fillRect(this.getWidth() / 8, this.getHeight() / 8, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 8 * 3, this.getHeight() / 8, this.getWidth() / 8, this.getHeight() / 8);                        
        g.fillRect(this.getWidth() / 8 * 5, this.getHeight() / 8, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 8 * 7, this.getHeight() / 8, this.getWidth() / 8, this.getHeight() / 8);                         
        }

        {//row 3                        
        g.fillRect(0, this.getHeight() / 4, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 4, this.getHeight() / 4, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 2, this.getHeight() / 4, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 8 * 6, this.getHeight() / 4, this.getWidth() / 8, this.getHeight() / 8);                         
        }

        {//row 4                        
        g.fillRect(this.getWidth() / 8, this.getHeight() / 8 * 3, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 8 * 3, this.getHeight() / 8 * 3, this.getWidth() / 8, this.getHeight() / 8);                        
        g.fillRect(this.getWidth() / 8 * 5, this.getHeight() / 8 * 3, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 8 * 7, this.getHeight() / 8 * 3, this.getWidth() / 8, this.getHeight() / 8);                         
        }

        {//row 5                        
        g.fillRect(0, this.getHeight() / 2, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 4, this.getHeight() / 2, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 2, this.getHeight() / 2, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 8 * 6, this.getHeight() / 2, this.getWidth() / 8, this.getHeight() / 8);                         
        }

        {//row 6                        
        g.fillRect(this.getWidth() / 8, this.getHeight() / 8 * 5, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 8 * 3, this.getHeight() / 8 * 5, this.getWidth() / 8, this.getHeight() / 8);                        
        g.fillRect(this.getWidth() / 8 * 5, this.getHeight() / 8 * 5, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 8 * 7, this.getHeight() / 8 * 5, this.getWidth() / 8, this.getHeight() / 8);                         
        }

        {//row 7                        
        g.fillRect(0, this.getHeight() / 8 * 6, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 4, this.getHeight() / 8 * 6, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 2, this.getHeight() / 8 * 6, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 8 * 6, this.getHeight() / 8 * 6, this.getWidth() / 8, this.getHeight() / 8);                         
        }

        {//row 8                        
        g.fillRect(this.getWidth() / 8, this.getHeight() / 8 * 7, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 8 * 3, this.getHeight() / 8 * 7, this.getWidth() / 8, this.getHeight() / 8);                        
        g.fillRect(this.getWidth() / 8 * 5, this.getHeight() / 8 * 7, this.getWidth() / 8, this.getHeight() / 8);                         
        g.fillRect(this.getWidth() / 8 * 7, this.getHeight() / 8 * 7, this.getWidth() / 8, this.getHeight() / 8);                         
        }

       // System.out.println("\u2654");

        repaint();

###

共有1个答案

子车俊哲
2023-03-14

在将DrawPanelChess JPanel添加为框架的内容窗格之后,似乎要将按钮添加到框架中。当您更改帧大小时,它会重新定位自身。如果您打算添加更多组件,但暂时避免添加,那么这不是一个很好的解决方案

frame.setResizable(false);

我还建议使用类似GridBagLayout的东西来指定您希望它与象棋面板一起定位的位置。

 类似资料:
  • 在调试我的应用程序时,我注意到我的RecycerView显示与提供的数据不一致,即。 > 如果我设置了一个闹钟(RecycerView中的TextView设置了日期),然后滚动我的RecycerView,日期显示在错误的位置。例如,如果我在第4项设置了日期,那么由于某种原因,第3项也设置了日期 我已经查看了文档,但不确定如何相应地修补。你能帮我吗? 我的OnBindViewholder:

  • 因此,我正在编写一个程序,在这个程序中,我最终希望一个按钮在按下时改变颜色。到目前为止,我的困境是: -尽管切换了setOpaque()和setContentAreaFilled()的真/假,但我无法获取当前设置为显示背景色的JButton。在尝试添加ActionListener之前,我希望能够做到这一点。 -我想用类似于JToggleButton()的东西来改变颜色,但是我想用背景而不是图标来改

  • 我有png图标,并将它们用作/中的图标 问题是,运行时显示的图像比原始图标大,而且由于这种大小调整,它非常难看。 下面是一个例子: 原始图标(左)以及它如何在中呈现(右) 这个简单示例的源代码如下: 这是预期的吗?如果没有,我如何避免这种情况?我尝试了很多方法来强制显示图像的大小、按钮等,但无法显示正确的图像。 我测试过各种尺寸的图标:16x16、17x17、18x18、19x19、20x20,每

  • 问题内容: 我正在测试我的网站,并且在所有浏览器上都能正常工作,但iphone浏览器(我认为它是移动Safari?)除外,该浏览器呈现的文字部分的字体比其他字体大。我已经手动检查了CSS,并在页面上使用了萤火虫,并且可以确认我对所有CSS都设置了相同的大小。我该如何解决? 问题答案: 为了稍微改善Lukasz的想法,请尝试在CSS中使用以下规则: 使用值“ 100%”而不是“ none”,使所有其

  • 设置position: absolute;宽度不一样 不设置position: absolute;宽度一样了 电脑显示配置:1.25物理像素=1逻辑像素 浏览器无缩放100%显示, 求大佬解释。。。。。。。。。。。。。

  • 我对具有“洞”的元组感兴趣。这些孔是空结构。所有孔都将具有相同的类型,在此处称为“空”。 为了说明起见,让 。正如我所料, 。然而, 我不明白为什么最后两种类型的大小不是2(在