0
所以我一直在試圖爲牙醫創建這個簡單的用戶界面,當我添加頂部菜單下的標題時,我注意到「setPreferredSize」不工作預期。基本上我想菜單水平擴展,所以我使用GridBagLayout。我還將包含按鈕的JPanel的大小設置爲我的contentPane的全寬度大小。setPreferredSize不能按預期工作,JPanel不是「打包」
奇怪的是,如果我調整窗口(JFrame)的大小並使其水平變大,那麼JPanel也會調整爲首選大小。創建時
的JFrame:
這是JFrame中一旦其創建時我手動調整(水平更大通過拖動邊):
正如你所看到的一旦我使窗口變大,JPanel的大小就會得到糾正。另外,我確定窗口有足夠的空間用於頂層菜單(甚至比頂層菜單大50px),但當設置爲可見時,它仍會像第一個圖像一樣被繪製。
主窗口中的代碼是:
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
/**
* Created by Matheus on 27/10/2016.
*/
public class WindowTest extends JFrame {
private JPanel contentPane;
private JPanel topMenu;
private JPanel header;
private JPanel dummy;
private JButton home, appointments, healthCare,
patients, contact;
public WindowTest(final int w,final int h) {
super();
// Colours we'll need to paint the UI (RGB format)
final Color lightBlue = new Color(200, 200, 255);
final Color bgBlue = new Color(112, 205, 255);
final Color grey = new Color(128, 128, 128, 40);
final Color white = new Color(255,255,255);
final Color transWhite = new Color(255,255,255, 100);
// Gradient drawing in this area
JPanel contentPane = new JPanel(new GridBagLayout()) {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D gb = (Graphics2D) g;
gb.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
GradientPaint gp = new GradientPaint(0, 100, bgBlue, 0, h, white);
gb.setPaint(gp);
gb.fillRect(0, 0, w, h);
}
};
contentPane.setPreferredSize(new Dimension(w, h));
// Grid bag contraints here - Initial settings
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
// Create the header stuff here.
gbc.gridy = 0;
gbc.gridx = 0;
gbc.weighty = 0;
gbc.weightx = 0;
try{
File imgFile = new File(getClass().getResource("header.jpg").toURI());
Image headerImg = ImageIO.read(imgFile);
header = new ImagePanel(headerImg);
contentPane.add(header, gbc);
}
catch (Exception e){
System.out.println("Failed to load image");
}
// Top menu stuff here
topMenu = new JPanel(new FlowLayout());
topMenu.setBackground(new Color(0,0,0));
topMenu.setPreferredSize(new Dimension(w, 60));
System.out.println(topMenu.getSize());
// Creating menu items
home = new MenuButton(300, 75, "Home", transWhite);
appointments = new MenuButton(300, 75, "Appointments", transWhite);
patients = new MenuButton(300, 75, "Patients", transWhite);
healthCare = new MenuButton(300, 75, "Health Care", transWhite);
contact = new MenuButton(300, 75, "Contact", transWhite);
home = new JButton("Home");
appointments = new JButton("Appointments");
patients = new JButton("Patients");
healthCare = new JButton("Health Care Plan");
contact = new JButton("Contact");
topMenu.add(home);
topMenu.add(appointments);
topMenu.add(patients);
topMenu.add(healthCare);
topMenu.add(contact);
topMenu.revalidate();
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 0;
contentPane.add(topMenu, gbc);
// Dummy here for the rest of the screen
JPanel dummy = new JPanel();
gbc.gridy = 1;
gbc.weighty = 1;
gbc.weightx = 1;
contentPane.add(dummy, gbc);
// Main window settings
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(contentPane);
this.pack();
this.revalidate();
this.setVisible(true);
}
}
我還試圖用不同的佈局管理器,但他們給出相同的結果。有人能幫我嗎?
爲什麼不讓佈局經理爲您決定尺寸?這是推薦的方式。 – RealSkeptic
1)爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 2)獲取圖像的一種方法是通過[本問答](http://stackoverflow.com/q/19209650/418556)中的圖像進行熱鏈接。 –
@RealSkeptic我以前試過。刪除了所有preferredSizes,並且JPanel仍然不水平擴展。 –