[英]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.