JScrollPane不适用于JPanel

时间:2015-02-15 06:52:22

标签: java swing jpanel jscrollpane layout-manager

我有一个包含mainPanel的框架。这最后将动态添加其他commandPanel(每个包含一个按钮和一个textField)。问题是,即使mainPanel已满,JScrollPane似乎也不允许我使用看不见的commandPanels。 下图显示了我的情况。

enter image description here

要初始化我在下面编写的窗口代码:

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); 

以及动态添加新命令面板的方法是:

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的任何帮助都将远远超过赞赏。

2 个答案:

答案 0 :(得分: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,然后立即将其添加到另一个容器中(虽然不正确)。

组件只能驻留在单个父组件上;将它添加到另一个容器的那一刻,它会自动从上一个容器中删除。

答案 1 :(得分:0)

我做了一些改动,现在效果很好。对于那些想要同样的事情的人来说,这是我的代码:

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;
}
}

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