【问题标题】:JScrollPane does not work on JPanelJScrollPane 在 JPanel 上不起作用
【发布时间】:2015-02-15 06:52:22
【问题描述】:

我有一个包含主面板的框架。最后一个将动态添加其他命令面板(每个都包含一个按钮和一个文本字段)。问题是即使 mainPanel 已满,JScrollPane 似乎也不允许我使用看不见的 commandPanels。 下图是我的情况。

为了初始化窗口,我写了下面的代码:

frame = new JFrame();
frame.setBounds(100, 100, 962, 639);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); 

mainPanel = new JPanel();
mainPanel.setBounds(264, 6, 692, 500);
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));    

scroll = new JScrollPane();
scroll.getViewport().add(mainPanel);
frame.getContentPane().add(scroll); 

动态添加新commandPanels的方法是:

public void loadCommandPanel(String commandName)
{
    CommandPanel newCommandPanel = new CommandPanel();
    newCommandPanel.getCommandBtn().setText(commandName);   
    mainPanel.add(newCommandPanel);

    scroll.getViewport().add( newCommandPanel );
    mainPanel.add( scroll, BorderLayout.EAST);
    frame.add( mainPanel);

    ...
}

对于获取 scrollPane 的任何帮助,我们将不胜感激。

【问题讨论】:

  • 我建议避免使用null 布局和setBounds 方法的用户。试试看它是否有效。另外,为什么要将面板添加到视口,而不是使用scroll = new JScrollPane(mainPanel);
  • 1) Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 2) 为了尽快获得更好的帮助,请发布MCVE(最小完整可验证示例)或SSCCE(简短、自包含、正确示例)。
  • "(每个都包含一个按钮和一个文本字段)" 考虑改为使用JList 来保存具有labelcommand 的对象的实例属性。使用渲染器在JLabel 中显示label,在JTextField 中显示command..
  • 或使用JListJTable...

标签: java swing jpanel jscrollpane layout-manager


【解决方案1】:

scroll.getViewport().add(mainPanel); 不是你使用JViewportJScrollPane 的方式;相反,你应该使用这样的东西: scroll.getViewport().setView(newCommandPanel);

scroll.setViewportView(newCommandPanel);

查看How to Use Scroll Panes了解更多详情。

另请注意,这没有意义:

CommandPanel newCommandPanel = new CommandPanel();
newCommandPanel.getCommandBtn().setText(commandName);   
mainPanel.add(newCommandPanel);

scroll.getViewport().add( newCommandPanel );

您将newCommandPanel 添加到mainPanel,然后立即将其添加到另一个容器(尽管不正确)。

一个组件只能驻留在一个父级上;当您将其添加到另一个容器时,它会自动从前一个容器中删除。

【讨论】:

    【解决方案2】:

    我进行了一些更改,现在可以完美运行。对于那些想要同样东西的人,这是我的代码:

    import ...
    
    public class mainUserInterface {
    
    private JFrame frame;
    private JPanel mainPanel;
    private  JPanel commandsPanel;
    private JScrollPane commandsScrollPane;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    mainUserInterface window = new mainUserInterface();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the application.
     */
    public mainUserInterface() {
        initialize();
    }
    
    private void initialize() {
    
        frame = new JFrame("CommandsExecutor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(1000, 700));
    
        mainPanel = new JPanel(new BorderLayout(5,5));
        mainPanel.setBorder( new TitledBorder("") );
    
    
        commandsPanel = new JPanel();
        commandsPanel.setLayout(new BoxLayout(commandsPanel, BoxLayout.Y_AXIS));
    
        for(int i=0; i<15;i++){
    
            commandsPanel.add(new CommandPanel());
        }
    
        commandsScrollPane = new JScrollPane(commandsPanel);
        mainPanel.add(commandsScrollPane,BorderLayout.CENTER);
    
    
        frame.setContentPane(mainPanel);
        frame.pack();
        frame.setVisible(true);
    }
    }
    

    这里是 commandPanel 类:

    import ...
    public class CommandPanel extends JPanel {
    
    private JTextField commandResult;
    private JButton commandBtn;
    
    public CommandPanel()
    {
        this.setLayout( new BorderLayout(10,10));
        this.setBorder( new TitledBorder("Command:") );
        this.setMaximumSize(new Dimension(692,60));
        this.setMinimumSize(new Dimension(692,60));
    
    
        commandBtn = new JButton("Execute");
        commandBtn.setMaximumSize(new Dimension(137, 34));
        commandBtn.setMinimumSize(new Dimension(137, 34));
        this.add(commandBtn, BorderLayout.WEST);
    
        commandResult = new JTextField();
        commandResult.setMaximumSize(new Dimension(518, 34));
        commandResult.setMinimumSize(new Dimension(518, 34));
        this.add(commandResult, BorderLayout.CENTER);
    }
    
    public JTextField getCommandResult() {
        return commandResult;
    }
    
    public JButton getCommandBtn() {
        return commandBtn;
    }
    
    public void setCommandResult(JTextField commandResult) {
        this.commandResult = commandResult;
    }
    
    public void setCommandBtn(JButton commandBtn) {
        this.commandBtn = commandBtn;
    }
    }
    

    感谢所有回答我问题的人,这真的很有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 2013-11-15
      相关资源
      最近更新 更多