对于您正在尝试做的事情,将 StartBar 的背景设置为黑色,然后您不需要覆盖paintComponet。
不要设置 StartBar 的边界,而是使用 set/getPreferredSize。这将允许父容器有机会计算组件的最佳大小(这可能会解释您的问题)
您应该认真考虑使用布局管理器。
我有两个子面板(内容和任务)。我会将所有应用程序图标放置在内容中,可能使用流布局和时钟作为/在任务中,再次,可能使用流布局。然后我会使用网格包布局或边框布局将它们添加到任务栏面板。
它可能看起来不像,但从长远来看,这会让你的生活变得更加轻松
更新
好的,那么请向我解释为什么我的工作原理:
并且看,无需重写paintComponent 或重新绘制视线。
public class TaskBarPane extends javax.swing.JPanel {
/**
* Creates new form TaskBarPane
*/
public TaskBarPane() {
initComponents();
setPreferredSize(new Dimension(800, 24));
pinTask(new File("C:/Program Files/BabyCounter/BabyCounter x64.exe"));
}
protected void pinTask(File task) {
pnlContent.add(new TaskPane(task));
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
pnlContent = new javax.swing.JPanel();
pnlClock = new test.ClockPane();
setBackground(new java.awt.Color(0, 0, 0));
setLayout(new java.awt.BorderLayout());
pnlContent.setOpaque(false);
java.awt.FlowLayout flowLayout1 = new java.awt.FlowLayout(java.awt.FlowLayout.LEFT);
flowLayout1.setAlignOnBaseline(true);
pnlContent.setLayout(flowLayout1);
add(pnlContent, java.awt.BorderLayout.CENTER);
pnlClock.setOpaque(false);
add(pnlClock, java.awt.BorderLayout.EAST);
}// </editor-fold>
// Variables declaration - do not modify
private test.ClockPane pnlClock;
private javax.swing.JPanel pnlContent;
// End of variables declaration
}
..
public class ClockPane extends javax.swing.JPanel {
/**
* Creates new form ClockPane
*/
public ClockPane() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jLabel1 = new javax.swing.JLabel();
setLayout(new java.awt.GridBagLayout());
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Hello World");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 100;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
add(jLabel1, gridBagConstraints);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
..
public class TaskPane extends javax.swing.JPanel {
/**
* Creates new form TaskPane
*/
public TaskPane() {
initComponents();
}
public TaskPane(File task) {
this();
Icon ico = FileSystemView.getFileSystemView().getSystemIcon(task);
lblIcon.setIcon(ico);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
lblIcon = new javax.swing.JLabel();
setOpaque(false);
setLayout(new java.awt.GridBagLayout());
lblIcon.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblIcon.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
lblIcon.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
add(lblIcon, new java.awt.GridBagConstraints());
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel lblIcon;
// End of variables declaration
}
...
public class TestFrame extends javax.swing.JFrame {
/**
* Creates new form TestFrame
*/
public TestFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
pnlTaskBar = new test.TaskBarPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(pnlTaskBar, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private test.TaskBarPane pnlTaskBar;
// End of variables declaration
}
我花了 10 分钟才完成(不得不喂我 11 周大的女儿,对不起)