![](/img/trans.png)
[英]Can I change font type without changing size of JPanels and JFrame?
[英]How can I change the size of JPanels in a JFrame?
我有一個 JFrame,目前包含 4 個面板。 其中一個面板是鍵盤,其他 3 個框架是鍵盤的控制面板,如下圖所示:
我一直試圖找到一種方法來使用 GridLayout 縮小控制面板的寬度,這樣它們就不會占據框架的整個寬度,但不幸的是我的代碼無法正常工作。 另外我想推動屏幕底部的鋼琴卷。 到目前為止,這是我的代碼:
JFrame frame = new JFrame();
JPanel container = new JPanel();
container.setSize(1000, 1000);
container.setLayout(new GridLayout(5,5));
// add control panels to JPanel
int y = 0;
for(int i =0; i < oscillators.length; ++i) {
JPanel panel = new JPanel();
panel.setSize(289, 100);
panel.setBorder(Utils.WindowDesign.LINE_BORDER);
panel.setLayout(null);
container.add(oscillators[i]);
y+=105;
}
// add keyboard to panel
keyboard = new PianoKeys(this);
keyboard.setFocusable(true);
container.add(keyboard);
// make frame visible
frame.setSize(1000, 1000);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setBackground(Color.WHITE);
frame.setLayout(new BorderLayout());
frame.add(container);
frame.setVisible(true);
有人可以向我解釋為什么這不能按預期工作嗎? 我將控制面板的寬度設置為 289,我的網格包含 5 列和 5 行,所以我不確定為什么控制面板最終會擴展到整個寬度。 感謝您提供的任何幫助!
SetPreferredSize
通常用於布局協商。 但是在您的情況下,面板上的SetMaxSize
。
通常不要使用空布局。
發生的事情是 JFrame 的默認布局管理器是 BorderLayout。 這會將內容放大到 JFrame 的內容窗格。
我不確定 GridLayout 是否是您想要的。 GridBagLayout 還允許合並網格單元格。 有些布局允許您將元素錨定到底部。 在 GridBagLayout 中,可以使用約束將小於單元格的組件與 SOUTH(底部)對齊。 那將是有差距的。
一句話:
我贊成另一種編碼風格,有一個 MyJFrame 類,里面有組件。 並做:
public static void main(String[] args) {
MyJFrame frame = new MyJFrame();
SwingUtilities.invokeLater(() -> frame.setVisible());
}
這允許使用 GUI 設計器(=代碼生成器),即使對於有經驗的程序員,它也可以節省大量實驗。
有趣的問題。 因此我做了一些簡短的研究。
要解決的一個想法:從 GridLayout 切換到 BoxLayout
引用自文檔:“BoxLayout 類將組件放在單行或單列中。它尊重組件請求的最大尺寸,還允許您對齊組件。有關更多詳細信息,請參閱如何使用 BoxLayout。” 布局管理器的視覺指南
簡短示例:
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class GridLayoutExample extends JFrame {
GridLayoutExample(){
JPanel listPane = new JPanel();
//listPane.setLayout(new GridLayout(5,0));
listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
this.add(listPane);
for(int i = 0; i < 5; i++){
JLabel imgLabel = new JLabel(generateImage(20 + 20 * i, 10 + 10 * i));
listPane.add(imgLabel);
}
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
new GridLayoutExample();
}
private static ImageIcon generateImage(int width, int height){
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
//create random image pixel by pixel
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
int a = (int)(Math.random()*256); //alpha
int r = (int)(Math.random()*256); //red
int g = (int)(Math.random()*256); //green
int b = (int)(Math.random()*256); //blue
int p = (a<<24) | (r<<16) | (g<<8) | b; //pixel
img.setRGB(x, y, p);
}
}
return new ImageIcon(img);
}
}
您可以通過更改以下幾行來比較 GridLayout 和 BoxLayout:
//listPane.setLayout(new GridLayout(5,0));
listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
顯示差異 - GridLayout:
框布局:
我希望這是有幫助的。
最好的祝福,馬蒂亞斯
dyclassroom.com隨機圖片生成的積分
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.