我最近开始使用JComponents创建GUI系统。一切正常,但JFrame的底部和右侧不会被涂刷并保持白色。运行GUI的Java JComponents在JFrame边缘被切断
截图:
在你可以看到 'drknBtn' 正确显示屏幕截图;这是因为我在拍照之前用鼠标盘旋了它。悬停在按钮上刷新它们,它们显示为正常。由于这个原因,我认为面板可以容纳它们,'bottomPnl'覆盖了这个空白区域,但是面板背景并没有显示在底部。任何想法可能会导致这种情况?我在调用pack()之前直接调用'bottomPnl.repaint()',但没有改变。
我的代码如下。 注意:对于每个JComponent,我创建了一个扩展该组件的类。通过这种方式,我可以为这些类的构造函数中的组件设置默认值,而不是单独执行每个类的构造函数。我将列出框架和面板的相关属性。 框架:setSize(width,height); setResizeable(假); setLocationRelativeTo(NULL); Panel:setLayoutManager(来自构造函数); setPreferredSize(new Dimension(width,height)); setMinimumSize和setMaximumSize相同。
public Display(String title, int w, int h){
width=w;
height=h;
frame = new FrameUI(title,w,h);
//parent panel
parentPnl= new PanelUI(width,height, new FlowLayout(FlowLayout.CENTER,0,0));
parentPnl.setBackground(new Color(100,175,175));
//top panel
topPnl= new PanelUI(width,(int)(height*.15), new FlowLayout(FlowLayout.CENTER,0,0));
topPnl.setBackground(new Color(100,175,175));
chooseFileBtn = new ButtonUI("Browse...",topPnl.getWidth()/4,(int)(topPnl.getHeight()*.9),new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
fc = new FileChooserUI();
fc.setFileFilter(new FileNameExtensionFilter("Image files", ImageIO.getReaderFileSuffixes()));
int result = fc.showOpenDialog(null);
try {
if (result == JFileChooser.APPROVE_OPTION) {
picture.setIcon(new ImageIcon(ImageIO.read(fc.getSelectedFile()).getScaledInstance(picture.getWidth(),picture.getHeight(), 0)));
}
} catch (Exception iOException) {
}
}
});
//middle panel
midPnl= new PanelUI((int)(width*.85),(int)(height*.7), new FlowLayout(FlowLayout.CENTER,0,0));
midPnl.setBackground(new Color(75,125,125));
picture = new LabelUI("",midPnl.getWidth(),midPnl.getHeight());
picture.setBackground(new Color(75,125,125));
picture.setVisible(true);
picture.setOpaque(true);
picture.setIcon(null);
//bottom panel
bottomPnl= new PanelUI(width,(int)(height*.15), new FlowLayout(FlowLayout.CENTER,0,0));
bottomPnl.setBackground(new Color(100,175,175));
ltnBtn = new ButtonUI("Lighten Picture",bottomPnl.getWidth()/3,(int)(bottomPnl.getHeight()*.9),new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
}
});
ltnBtn.setBackground(Color.LIGHT_GRAY);
ltnBtn.setForeground(Color.BLACK);
drknBtn = new ButtonUI("Darken Picture",bottomPnl.getWidth()/3,(int)(bottomPnl.getHeight()*.9),new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
}
});
drknBtn.setBackground(Color.DARK_GRAY);
drknBtn.setForeground(Color.WHITE);
//add UI Objects
topPnl.add(chooseFileBtn);
midPnl.add(picture);
bottomPnl.add(ltnBtn);
bottomPnl.add(drknBtn);
parentPnl.add(topPnl);
parentPnl.add(midPnl);
parentPnl.add(bottomPnl);
Container contentPane = frame.getContentPane();
contentPane.add(parentPnl);
frame.pack();
frame.setVisible(true);
}
}
我的第一个猜测是你使用'null'布局,我的第二个猜测是你重写了'paint'或'paintComponent',并且没有调用他们的'super'方法 – MadProgrammer