2017-12-10 19 views
0

このコードでは、JScrollPaneのボタンを簡単に作成できます。私はスクロールペインを使用しました。なぜなら、将来はボタンが多くなるからです。JScrollPaneのJPanel位置

ボタンがある場合はJPanelしか表示されませんが、スクロールペインの中央に表示され、上部に表示されます。

できますか?

JPanel pane = new JPanel(new GridBagLayout()); 
     for (int i = 0; i < 100; i++) { 

      JButton button; 
      pane.setLayout(new GridBagLayout()); 
      GridBagConstraints c = new GridBagConstraints(); 
      c.fill = GridBagConstraints.HORIZONTAL; 

      button = new JButton("Button 1"); 
      c.weightx = 0.5; 
      c.gridx = 0; 
      c.gridy = 0; 
      pane.add(button, c); 

      button = new JButton("Button 2"); 
      c.gridx = 0; 
      c.gridy = 1; 
      pane.add(button, c); 

      button = new JButton("Button 3"); 
      c.gridx = 1; 
      c.gridy = 1; 
      pane.add(button, c); 

      button = new JButton("Long-Named Button 4"); 
      c.ipady = 40; 
      c.weightx = 0.0; 
      c.gridwidth = 2; 
      c.gridx = 0; 
      c.gridy = 2; 
      pane.add(button, c); 

     } 
     JPanel borderLayoutPanel = new JPanel(new BorderLayout()); // wrapper JPanel 
     borderLayoutPanel.add(pane, BorderLayout.PAGE_START); // add pane to the top 

     jScrollPane2.setViewportView(borderLayoutPanel); 

enter image description here

:私は複数のJPanelためTHIコードを作成し、しかしのJPanelが垂直、水平ではなく表示されているウナギ応答のホバークラフト本文に基づい を編集

JPanel pane = new JPanel(new GridBagLayout()); 

JButton button; 
pane.setLayout(new GridBagLayout()); 
GridBagConstraints c = new GridBagConstraints(); 
c.fill = GridBagConstraints.HORIZONTAL; 

button = new JButton("Button 1"); 
c.weightx = 0.5; 
c.gridx = 0; 
c.gridy = 0; 
pane.add(button, c); 

button = new JButton("Button 2"); 
c.gridx = 0; 
c.gridy = 1; 
pane.add(button, c); 

button = new JButton("Button 3"); 
c.gridx = 1; 
c.gridy = 1; 
pane.add(button, c); 

button = new JButton("Long-Named Button 4"); 
c.ipady = 40;  
c.weightx = 0.0; 
c.gridwidth = 3; 
c.gridx = 0; 
c.gridy = 2; 
pane.add(button, c); 

jScrollPane2.setViewportView(pane); 

enter image description here

+0

'c.gridwidth = 3;'ロジックがあり何ですか?私は 'c.gridwidth = 2;'がより適切だろうと思っていたでしょう。 BTW - すぐに役立つように、[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/)を投稿してください。 –

答えて

1

新しい「ラッパー」JPanelを作成するBorderLayoutを使用し、ペインをラッパーJPanelのBorderLayout.PAGE_START位置に追加してから、同じラッパーJPanelをJScrollPaneのビューポートに追加します。例:作業MCVEの例えば

JPanel borderLayoutPanel = new JPanel(new BorderLayout()); // wrapper JPanel 
borderLayoutPanel.add(pane, BorderLayout.PAGE_START); // add pane to the top 
jScrollPane2.setViewportView(borderLayoutPanel); // add wrapper to viewport 

、チェックアウトしてください:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class Foo extends JPanel { 
    private static final int PREF_W = 400; 
    private static final int PREF_H = 600; 
    private JPanel gridPanel = new JPanel(new GridLayout(0, 1)); 
    public Foo() { 
     JPanel buttonPanel = new JPanel(); 
     buttonPanel.add(new JButton(new AbstractAction("Add Panel") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       gridPanel.add(createButtonPanel()); 
       gridPanel.revalidate(); 
       gridPanel.repaint(); 
      } 
     })); 

     JPanel borderLayoutPanel = new JPanel(new BorderLayout()); 
     borderLayoutPanel.add(gridPanel, BorderLayout.PAGE_START); 
     JScrollPane scrollPane = new JScrollPane(borderLayoutPanel); 
     scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

     setPreferredSize(new Dimension(PREF_W, PREF_H)); 
     setLayout(new BorderLayout()); 
     add(scrollPane); 
     add(buttonPanel, BorderLayout.PAGE_END); 
    } 

    private JPanel createButtonPanel() { 
     JPanel pane = new JPanel(new GridBagLayout()); 
     pane.setBorder(BorderFactory.createLineBorder(Color.BLUE)); 

     JButton button; 
     pane.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.anchor = GridBagConstraints.PAGE_START; 

     button = new JButton("Button 1"); 
     c.weightx = 0.5; 
     c.gridx = 0; 
     c.gridy = 0; 
     pane.add(button, c); 

     button = new JButton("Button 2"); 
     c.gridx = 0; 
     c.gridy = 1; 
     pane.add(button, c); 

     button = new JButton("Button 3"); 
     c.gridx = 1; 
     c.gridy = 1; 
     pane.add(button, c); 

     button = new JButton("Long-Named Button 4"); 
     c.ipady = 40;  
     c.weightx = 0.0; 
     c.gridwidth = 3; 
     c.gridx = 0; 
     c.gridy = 2; 
     pane.add(button, c); 
     return pane; 
    } 

    private static void createAndShowGui() { 
     Foo mainPanel = new Foo(); 

     JFrame frame = new JFrame("Foo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 
+0

私の編集したポストをご覧ください。 – user2847219

+0

@ user2847219:Andrew Thompsonが示唆しているように、まだ固まっている場合は、あなたの質問を編集し、適切な[mcve]、* small *スタンドアロンプ​​ログラムを投稿してください問題のコード形式のテキストとして、コンパイルして実行し、問題を示しています。 –

+0

@ user2847219:* what * JPanels?投稿したコードに複数の水平JPanelを表示することはありません。再びまともなMCVEがこれに答えるだろう。 –

関連する問題