在我的平台上运行时,您的代码不会显示上面显示的任何故障.
确认您没有绘画问题,例如忽略此处super.paintComponent()
讨论的内容.
请确认您有没有驱动程序不兼容,如讨论在这里.
构造和修改事件派发线程上的所有GUI对象.
不要使用set[Preferred|Maximum|Minimum]Size()
,当你真的要覆盖get[Preferred|Maximum|Minimum]Size()
,为讨论在这里.下面的示例覆盖getPreferredSize()
滚动窗格,但可以实现Scrollable
,如讨论在这里.
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; /** @see /sf/ask/17360801/ */ public class MainFrame { private static final int H = 64; public static void main(String[] args) { EventQueue.invokeLater(() -> new MainFrame()); } public MainFrame() { JFrame frame = new JFrame("LOL"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTabbedPane tabbedPane = new JTabbedPane(); JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5)); for (int i = 0; i < 8; i++) { panel.add(new DownloadPanel()); } JScrollPane jsp = new JScrollPane(panel) { @Override public Dimension getPreferredSize() { return new Dimension(6 * H, 4 * H); } }; tabbedPane.addTab("Download", null, jsp, "Main Download Window."); tabbedPane.addTab("Options", null, null, "Options"); frame.add(tabbedPane); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } private static class DownloadPanel extends JPanel { JPanel jobPanel = new JPanel(); public DownloadPanel() { this.setLayout(new BorderLayout()); this.setBackground(Color.lightGray); JProgressBar jpb = new JProgressBar(); jpb.setIndeterminate(true); this.add(jpb); JPanel buttonPane = new JPanel(); JButton toggleTorConnectionButton = new JButton("Button"); toggleTorConnectionButton.setToolTipText("Toggles Tor Connection."); buttonPane.add(toggleTorConnectionButton); this.add(buttonPane, BorderLayout.WEST); } @Override public Dimension getPreferredSize() { return new Dimension(4 * H, H); } } }