2016-07-16 8 views
-1

У меня есть проект, где я всегда хочу сосредоточить сосредоточенный JPanel. Поэтому я подумал, что могу просто изменить позицию Viewport. Но я не могу использовать окно просмотра. Я создал пример проекта, чтобы показать, как я использую viewport. Я просто хочу, чтобы пользователь видел только один из оранжевых ящиков. Но также необходимо просмотреть все ящики одновременно. Таким образом, представление должно увеличиться или что-то вроде этого. Как я могу исправить эту проблему? Мой пример:Всегда центрируйте сфокусированный JPanel

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


public class main { 

    public static void main(String [] args){ 
     //create JFrame 
     JFrame _frame = new JFrame(); 

     //create Viewport 
     JViewport _view = new JViewport(); 

     //create Mainpanel 
     JPanel _mainPanel = new JPanel(); 

     //tell the view to handle mainpanel 
     _view.setView(_mainPanel); 

     //create Layout 
     GridLayout _layout = new GridLayout(3,3,3,3); 

     //set gridlayout to mainpanel 
     _mainPanel.setLayout(_layout); 




     for(int i = 0;i<12;i++){ 
      JPanel _tempPanel = new JPanel(); 
      _tempPanel.setBackground(Color.ORANGE); 
      _tempPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 

      _mainPanel.add(_tempPanel); 
     } 


     _view.setExtentSize(new Dimension(300,300)); 

     //add mainpanel to frame 
     _frame.add(_mainPanel); 


     _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     _frame.pack(); 
     //set size of Jframe 
     _frame.setSize(1000,1000); 
     _frame.setVisible(true); 
    } 
} 
+0

почему не используется [CardLayout] (https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – guleryuz

+0

@guleryuz эй, потому что я хочу, чтобы иметь возможность просматривать все JPanels в однажды – ReasyEasyPeasy

+0

«Я просто хочу, чтобы пользователь видел только один из оранжевых ящиков», вы говорите в своем вопросе – guleryuz

ответ

1

JViewPort не может помочь вам с вашим требованием. Вот уродливый, но бегущий код. Вы можете улучшить его самостоятельно.

public static void main(String[] args) { 

    // create JFrame 
    JFrame _frame = new JFrame(); 

    JPanel conPanel = new JPanel(new BorderLayout()); 

    // create Mainpanel 
    JPanel _mainPanel = new JPanel() { 
     @Override 
     public String toString() { 
      return "All"; 
     } 
    }; 

    // create Layout 
    GridLayout _layout = new GridLayout(3, 3, 3, 3); 

    // set gridlayout to mainpanel 
    _mainPanel.setLayout(_layout); 

    JComboBox<JPanel> combo = new JComboBox<>(); 

    combo.addItem(_mainPanel); 

    for (int i = 0; i < 12; i++) { 
     final int fi = i; 
     JPanel _tempPanel = new JPanel() { 
      @Override 
      public String toString() { 
       return "Panel" + fi; 
      } 

      @Override 
      protected void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       g.drawString(toString(), 5, 15); 
      } 

     }; 
     _tempPanel.setBackground(Color.ORANGE); 
     _tempPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 

     _mainPanel.add(_tempPanel); 

     combo.addItem(_tempPanel); 

    } 

    combo.addActionListener(e -> { 

     JPanel panel = (JPanel)combo.getSelectedItem(); 

     conPanel.remove(_mainPanel); 
     _mainPanel.removeAll(); 

     for(int i = 1; i < combo.getItemCount(); i++) 
      _mainPanel.add(combo.getItemAt(i)); 

     conPanel.add(panel, BorderLayout.CENTER); 

     conPanel.revalidate(); 

     conPanel.repaint(); 

    }); 

    conPanel.add(_mainPanel, BorderLayout.CENTER); 

    JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 

    buttonsPanel.add(combo); 

    conPanel.add(buttonsPanel, BorderLayout.SOUTH); 

    // add mainpanel to frame 
    _frame.setContentPane(conPanel); 

    _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    // set size of Jframe 
    _frame.setSize(1000, 1000); 
    _frame.setVisible(true); 

}