简体   繁体   English

我想按照图表制作一个用户界面。 我必须在 JFrame 中设置 JButtons,但我没有得到图中所示的 output。 我绑了我应该怎么办?

[英]I want to make a UI as per diagram. I have to set JButtons in a JFrame, but I don't get the output as shown in the diagram. I tied. What should I do?

用户界面在这里....

I tried like this...我试过这样...

class Calculator extends JFrame{
    // Here are the attributes
    Calculator(){
    // Here are the frame
    
    // here are the assign JPanels

    firstPanel.add(buttons[0]);
    secondPanel.add(buttons[1]);
        
    for (int i = 2; i < 6; i++){
        thirdPanel.add(buttons[i]);
    }
        
    fourthPanel.add(buttons[6]);
    fifthPanel.add(buttons[7]);
            
    add(firstPanel,BorderLayout.PAGE_START);
    add(secondPanel,BorderLayout.LINE_START);
    add(thirdPanel,BorderLayout.CENTER);
    add(fourthPanel,BorderLayout.LINE_END);
    add(fifthPanel,BorderLayout.PAGE_END);  `
    }
    
    
}

To get the output as shown in the diagram you can try the following要获得图中所示的 output,您可以尝试以下操作

 Calculator(){
        this.setSize(400, 400);
        this.setTitle("this");
        //create first panel with BorderLayout as layout
        JPanel firstPanel=new JPanel(new BorderLayout());
        //Create second Panel with GrdiLayout as layout
        JPanel secondPanel= new JPanel(new GridLayout(2,2));
        //add 4 Buttons to second Panel
        for(Integer i=0;i<4;i++){
            secondPanel.add(new JButton("Button"+i.toString()));
        }
        //Add wnorth,south,west,east buttons to first panel
        firstPanel.add(new JButton("north"),BorderLayout.NORTH);
        firstPanel.add(new JButton("south"),BorderLayout.SOUTH);
        firstPanel.add(new JButton("west"),BorderLayout.WEST);
        firstPanel.add(new JButton("east"),BorderLayout.EAST);
        //add the second panel (containing 4 buttons) to first panel
        firstPanel.add(secondPanel,BorderLayout.CENTER);
        //add first panel to the frame
        this.add(firstPanel);
        
    }

In main在主要

public static void main (String args[]){

 new Calculator().setVisible(true);

}

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM