JPanel panel2=new JPanel();
JButton buttondel=new JButton("Delete");
JButton buttonclear=new JButton("Clear");
panel2.add(buttondel);
panel2.add(buttonclear);
panel2.setSize(200,60);
panel2.setLayout(new GridLayout(0,2,10,10));
panel2.setBorder(BorderFactory.createEmptyBorder(10,0,10,0));
JPanel buttonpanel=new JPanel();
JButton b1=new JButton("9");
JButton b2=new JButton("8");
JButton b3=new JButton("7");
JButton b4=new JButton("/");
JButton b5=new JButton("6");
JButton b6=new JButton("5");
JButton b7=new JButton("4");
JButton b8=new JButton("*");
JButton b9=new JButton("3");
JButton b10=new JButton("2");
JButton b11=new JButton("1");
JButton b12=new JButton("-");
JButton b13=new JButton("0");
JButton b14=new JButton(".");
JButton b15=new JButton("=");
JButton b16=new JButton("+");
buttonpanel.add(b1);
buttonpanel.add(b2);
buttonpanel.add(b3);
buttonpanel.add(b4);
buttonpanel.add(b5);
buttonpanel.add(b6);
buttonpanel.add(b7);
buttonpanel.add(b8);
buttonpanel.add(b9);
buttonpanel.add(b10);
buttonpanel.add(b11);
buttonpanel.add(b12);
buttonpanel.add(b13);
buttonpanel.add(b14);
buttonpanel.add(b15);
buttonpanel.add(b16);
buttonpanel.setSize(200,220);
buttonpanel.setLayout(new GridLayout(0,4,10,10));
mainpanel.add(tfpanel);
mainpanel.add(panel2);
mainpanel.add(buttonpanel);
mainpanel.setLayout(new GridLayout(0,1));
mainpanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
frame.add(mainpanel);
frame.setSize(300,300);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
这是第二个面板,它包含'删除'按钮和'清除'按钮
<table class="controls rating">
<tr>
<td>
<span>
<input type="radio" name="review-rating" id="1" value="1" />
<label for="1">1</label>
</span>
</td>
<td>
<span>
<input type="radio" name="review-rating" value="2" id="2" />
<label for="2">2</label>
</span>
</td>
<td>
<span>
<input type="radio" name="review-rating" value="3" id="3" />
<label for="3">3</label>
</span>
</td>
<td>
<span>
<input type="radio" name="review-rating" value="4" id="4" />
<label for="4">4</label>
</span>
</td>
<td>
<span>
<input type="radio" name="review-rating" value="5" id="5" />
<label for="5">5</label>
</span>
</td>
</tr>
</table>
在上面的例子中,我创建了三个面板,并且我已经为它们分配了不同的高度。但是这三个小组都超过了指定的高度。
如何将不同的高度分配给不同的面板?
答案 0 :(得分:0)
首先:代替setSize()
更好地使用setPreferredSize()
作为面板
tfpanel.setPreferredSize(new Dimension(200,40));
panel2.setPreferredSize(new Dimension(200,60));
buttonpanel.setPreferredSize(new Dimension(200,120));
其次,不要将GridLayout
用于mainpanel
,因为它会使所有组件的大小相同。你可以简单地离开这条线
//mainpanel.setLayout(new GridLayout(0,1));