gpt4 book ai didi

java - setPreferredSize 未按预期工作,JPanel 不是 "packing"

转载 作者:行者123 更新时间:2023-12-02 03:14:54 29 4
gpt4 key购买 nike

所以我一直在尝试为牙医创建这个简单的用户界面,当我在标题下添加顶部菜单时,我注意到“setPreferredSize”没有按预期工作。基本上我希望菜单水平扩展,所以我使用 GridBagLayout 来实现。我还将包含按钮的 JPanel 的大小设置为 contentPane 的全宽大小。

奇怪的是,如果我调整窗口 (JFrame) 的大小并使其水平变大,那么 JPanel 的大小也会调整为首选大小。

创建 JFrame 时:

Top menu's size is not correct (black panel)

这是我在创建后手动调整大小的 JFrame(通过拖动侧面横向放大):

Top menu's size gets corrected (black JPanel)

正如您所看到的,一旦我将窗口变大,JPanel 的大小就会得到纠正。另外,我确保窗口有足够的空间容纳顶部菜单(即使制作得比顶部菜单大 50px),但当设置为 visivble 时,它​​仍然会像第一张图像中那样被绘制。

主窗口的代码是:

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);
}
}

我也尝试使用不同的布局管理器,但它们给出了相同的结果。有人可以帮助我吗?

最佳答案

强制组件填充所有空间的最简单方法是使用 BorderLayout 。在您的情况下,应该使用两次。

// Initialize topMenu as before

// Create a panel for image and buttons
JPanel head = new JPanel(new BorderLayout());
// Image will be centered and its height will be preserved
head.add(new JLabel(new ImageIcon(getClass().getResource("header.jpg"))), BorderLayout.PAGE_START);
// Buttons also will be centered and their height will be preserved
head.add(topMenu, BorderLayout.PAGE_END);

// Gradient drawing in this area
JPanel contentPane = new JPanel(new BorderLayout()) {
// paintComponent ...
};

// The height of the head will be preserver,
// but the width will be equal to the window width
contentPane.add(head, BorderLayout.PAGE_START);

// Main component
// Just for the example
final JButton main = new JButton("Test");
main.setOpaque(false);
main.setBackground(Color.MAGENTA);

// Main component will be stretched both vertically and horizontally
contentPane.add(main, BorderLayout.CENTER);

关于java - setPreferredSize 未按预期工作,JPanel 不是 "packing",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40463800/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com