我最近開始使用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