添加menuBar时,JScrollPane不起作用

时间:2013-11-28 11:26:10

标签: java swing jframe jscrollpane jmenubar

几个月前我问了一个类似于这个的问题: here问题解决了但是只要我将menuBar添加到此代码中,scrollPane就会再次消失。 这是我的新代码:

public class Question {

int count = 0;
ArrayList<JTextField>[] jt;

public Question() {
    final JFrame f = new JFrame();
    f.setLayout(new BorderLayout());
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JMenuBar mnuBar = new JMenuBar();

    JMenu mnu1 = new JMenu("ثبت");
    mnu1.setMnemonic(KeyEvent.VK_E);

    JMenuItem menuItem = new JMenuItem("Insert Places", KeyEvent.VK_T);
    menuItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

            count = 0;
            jt = new ArrayList[4];
            for (int i = 0; i < 4; i++)
                jt[i] = new ArrayList<JTextField>();


    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createLineBorder(Color.red));
    panel.setLayout(new BoxLayout(panel, 1));
    for (int i = 0; i < 100; i++) {
        panel.add(new JButton("kjdh"));
    }

    JScrollPane scrollPane = new JScrollPane(panel);
    f.getContentPane().add(scrollPane);
        }
    });

    mnu1.add(menuItem);
    mnuBar.add(mnu1);
    f.setJMenuBar(mnuBar);
    f.pack();
    f.setExtendedState(JFrame.MAXIMIZED_BOTH);
    f.setVisible(true);
}

public static void main(String[] args) {
    Runnable r = new Runnable() {
        @Override
        public void run() {
            new Question();
        }
    };
    SwingUtilities.invokeLater(r);
} 
}

感谢

2 个答案:

答案 0 :(得分:1)

  

但是只要我将menuBar添加到此代码的scrollPane中   又消失了。这是我的新代码:

因为在JScrollPane上执行任何操作之前,您没有将MenuItem添加到内容窗格。

  1. 在类上下文中而不是在函数中声明JScrollPaneJPanel
  2. 添加JScrollPaneJPanel以包含您的JButtons
  3. MenuItem上执行操作事件时,将按钮添加到JPanel并在其上调用revalidate()
  4. 我不建议在validate()/revalidate()的内容窗格中调用JFrame。仅在布局发生变化的组件上调用revalidate():在您的上下文中,它是包含JButton的面板。
  5. 作为一般规则,请不要忘记拨打repaint()
  6. 所以,你的完整解决方案将成为:

    class Question {
    
    int count = 0;
    ArrayList<JTextField>[] jt;
    JPanel buttonPanel;;
    JScrollPane scrollPane;
    
    public Question() {
        final JFrame f = new JFrame();
        f.setLayout(new BorderLayout());
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JMenuBar mnuBar = new JMenuBar();
    
        JMenu mnu1 = new JMenu("ثبت");
        mnu1.setMnemonic(KeyEvent.VK_E);
    
        buttonPanel = new JPanel();
        buttonPanel.setBorder(BorderFactory.createLineBorder(Color.red));
        buttonPanel.setLayout(new BoxLayout(buttonPanel, 1));
    
        scrollPane  = new JScrollPane(buttonPanel);
        f.getContentPane().add(scrollPane);
    
        JMenuItem menuItem = new JMenuItem("Insert Places", KeyEvent.VK_T);
        menuItem.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent arg0) {
    
                count = 0;
                jt = new ArrayList[4];
                for (int i = 0; i < 4; i++)
                    jt[i] = new ArrayList<JTextField>();
    
    
               for (int i = 0; i < 100; i++) {
                    buttonPanel.add(new JButton("kjdh"));
               }
    
               buttonPanel.revalidate(); // calling revalidate 
               buttonPanel.repaint();// calling repaint
    
            }
        });
    
        mnu1.add(menuItem);
        mnuBar.add(mnu1);
        f.setJMenuBar(mnuBar);
        f.pack();
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);
        f.setVisible(true);
    }
    
    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                new Question();
            }
        };
        SwingUtilities.invokeLater(r);
    } 
    }
    

答案 1 :(得分:0)

f.revalidate();添加ActionListener

 menuItem.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    .................

    JScrollPane scrollPane = new JScrollPane(panel);
    f.getContentPane().add(scrollPane);

    f.revalidate();  // Add this method with JFrame

    }

   });