我有一個桌面應用程序在擺動。我有一個JPanel,其中圖像作爲背景,並在其中有兩個按鈕和一個JScrollPane,如圖片Frame with JPanel所示。我有一個函數(showLabel()),當JScrollPane結束時,將JLabel添加透明圖像並消失幾秒鐘。問題是,當你添加JLabel時。 JLabel糟糕的表演如圖Bad shows所示。你能幫我解決我的問題嗎?Java錯誤視圖JPanel
public class MainWindow {
private JFrame frame;
private PanelPopis panelPopis = new PanelPopis(this);
private MyPaint myPaint;
public MainWindow {
setWindow():
BufferedImage image1 = ImageIO.read(getClass().getClassLoader().getResource("poz.png"));
this.myPaint = new MyPaint(image1);
this.frame.add(myPaint);
this.myPaint.add(panelPopis.setPanel());
}
private void setWindow() {
this.frame = new JFrame("DD");
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setSize(400, 680);
this.frame.setResizable(false);
this.frame.setLocationRelativeTo(null);
}
private void showLabel(){
JLabel label = new JLabel();
label.setIcon(new ImageIcon(new ImageIcon(getClass().getClassLoader().getResource("postEn.png")).getImage().getScaledInstance(395, 653, Image.SCALE_DEFAULT)));
label.setBackground(new Color(0, 0, 0, 10));
label.setOpaque(true);
this.frame.invalidate();
this.frame.add(label);
this.frame.revalidate();
int delay2 = 3000; // milliseconds
ActionListener taskPerformer2 = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
frame.remove(label);
frame.revalidate();
frame.repaint();
}
};
Timer myTimer2 = new Timer(delay2, taskPerformer2);
myTimer2.setRepeats(false);
myTimer2.start();
}
}
public class MyPaint extends JPanel {
private static final long serialVersionUID = 1L;
BufferedImage image;
public MyPaint(BufferedImage image) {
setOpaque(false);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, 395, 653, this);
}
}
public class PanelPopis extends JPanel {
private static final long serialVersionUID = 7676683627217636485L;
private JButton setLanguage;
private JButton cont;
private JScrollPane scrolPanel;
private JTextArea popis;
private MainWindow mainWindow;
public PanelPopis(MainWindow mainWindow) {
this.mainWindow = mainWindow;
}
public JPanel setPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setOpaque(false);
JPanel panel2 = new JPanel();
Border border = panel2.getBorder();
Border margin = new EmptyBorder(0, 0, 4, 0);
panel2.setBorder(new CompoundBorder(border, margin));
panel2.setOpaque(false);
panel2.add(this.scrolPanel = new JScrollPane(popis));
panel.add(this.setLanguage = new JButton("language settings"), BorderLayout.NORTH);
panel.add(this.cont = new JButton("CONTINUE"), BorderLayout.SOUTH);
panel.add(panel2, BorderLayout.CENTER);
return panel;
}
}
1)爲了更好地幫助更快,發佈[MCVE]或[短的,獨立的,正確的示例](http://www.sscce.org /)。 2)獲取圖像的一種方法是通過[本問答](http://stackoverflow.com/q/19209650/418556)中的圖像進行熱鏈接。 –
你在MyPaint一些其他的事情 - 在照片上不會是一個嚴肅的事情上面一個簡單的照片 - 顯示完整的代碼 – gpasch
@gpasch *「顯示完整的代碼」 *不同意。一個MCVE將是最優秀的代碼形式。請注意,評論中的[mcve]會自動展開爲[mcve]。 BranislavPažický--請不要將代碼放在評論中,而這些評論在很大程度上是不可讀的。它應該作爲編輯包含在問題中。 –