【发布时间】:2012-05-31 14:36:49
【问题描述】:
这个问题我已经有一段时间了,搜索了很多论坛和网站(包括这个),但仍然没有找到我的问题的答案。
这是我的问题: 我正在建立一个视觉日历。我有一个包含多个面板的父面板。我重新绘制父面板,并使 3 覆盖不透明(假)。 在我调整框架大小之前不会显示父面板的绘制(或使用覆盖 3 个按钮之一的按钮,但在此示例中省略了这些按钮,因为它会使代码更长) p>
不管怎样,这里是代码,我把它简化为问题部分:
public class Calendar extends JPanel{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(1600,150);
frame.add(new Calendar());
frame.setVisible(true);
}
public Calendar(){
setLayout(new GridBagLayout());
GridBagConstraints cc = new GridBagConstraints();
cc.weightx = 1;
cc.weighty = 1;
cc.gridx = 0;
cc.fill = GridBagConstraints.BOTH;
//Initiate Panels
JPanel yearpanel = new JPanel();
JPanel monthpanel = new JPanel();
JPanel daypanel = new JPanel();
yearpanel.setLayout(new GridBagLayout());
monthpanel.setLayout(new GridBagLayout());
daypanel.setLayout(new GridBagLayout());
// Set sizes
int width = (int) this.getPreferredSize().getWidth();
int height = (int) (this.getPreferredSize().getHeight() / 3);
yearpanel.setSize(width,height);
daypanel.setSize(width,height);
monthpanel.setSize(width,height);
//make transparent
yearpanel.setOpaque(false);
monthpanel.setOpaque(false);
daypanel.setOpaque(false);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Image image = Toolkit.getDefaultToolkit().getImage("Images/CalendarBackground.jpg");
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
}
}
我不知道为什么会这样 + 我在网上找不到答案,只有遇到相同问题的人的问题被放弃了:/
谁能帮帮我?
【问题讨论】:
-
您想修改您的SSCCE - 目前它不起作用。您正在创建空的
JPanels,它们永远不会添加到顶级JPanel- 即Calendar。
标签: java resize jpanel paint paintcomponent