2017-05-12 10 views
0

現在、私は抵抗、LED、コンデンサなどの電子部品を含むボードを作成する予定のプロジェクトに取り組んでいます。私はすべての小さなのJPanel保持するメインのJFrame、持ってJTextAreaを更新した後のJButton位置の変更

: - ボードに Rightpanelが含まれています - Centerpanelが、私は仕事にそれを想像してどのように選択したコンポーネント

に関する情報が含まれていますが、次のとおりです。

  1. コンポーネントをJComboBoxから選択します。

    1.1。コンポーネントは、あなたがコンポーネントをクリックすると、ボードその後

に新しいコンポーネントを追加するcenterpanel上のどこかをクリックComponentFactory

  • を介して作成され、情報が右側のパネルで示すべきですJTextArea

    私の問題は次のとおりです:2回目にコンポーネントをクリックするまで、すべてのことが計画どおりに機能し、コンポーネントに関する情報が表示されます(右側のパネル参照)。その後、JButtonのサイズがはるかに小さくなり、左上隅に移動します。

    String strBuilder = "Type: " + e.getComponent().getName() + "\n" + 
    "ID: " + ((Components) e.getComponent()).getCompID() + "\n"; 
    infoContainer.append(strBuilder); 
    

    完全なコード:

    public class Framework extends JFrame{ 
    // Declaring variables for the frame 
    private ComponentFactory cf = new ComponentFactory(); 
    private JToolBar menuToolbar; 
    private String[] menuItemsString = new String[]{ "newFile", "loadFile", "saveFile" }; 
    private JButton[] menuItems = new JButton[menuItemsString.length]; 
    
    private JPanel menuPane, centerPane, innerCenter; 
    private JPanel rightPane; 
    private JTextArea infoContainer; 
    
    private JComboBox<String> componentList; 
    private final String NOT_SELECTABLE_OPTION = " - Select a component - "; 
    private String[] componentListStrings = new String[] {"Resistor","LED","Capacitor","Inductor"}; 
    private Components newComponent, selectedComponent; 
    private boolean componentSelected = false; 
    
    private ArrayList<Components> compList = new ArrayList<Components>(); 
    
    /** 
    * Creates a new dispatcher that will listen for keyboard actions on all the selected elements. 
    * @author Zovsaman 
    */ 
    private class MyDispatcher implements KeyEventDispatcher { 
        @Override 
        public boolean dispatchKeyEvent(KeyEvent e) { 
         if(e.getID() == KeyEvent.KEY_PRESSED){ 
          if(componentSelected){ 
           if(e.getKeyCode() == 27){ 
            componentSelected = false; 
           } 
    
          } 
         } 
         return false; 
        } 
    } 
    
    public Framework(){ 
        setLayout(new BorderLayout()); 
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
        setSize((int)screenSize.getWidth()/2, (int)screenSize.getHeight()/2); 
    
        // Initiating all the JPanels and stuff 
        menuToolbar = new JToolBar(null); 
        componentList = new JComboBox<String>(); 
        centerPane = new JPanel(); 
        innerCenter = new JPanel(); 
        rightPane = new JPanel(); 
        infoContainer = new JTextArea(5, 20); 
    
        // Setting settings and adding buttons to JToolBar at the top of the program 
        menuToolbar.setFloatable(false); 
        for(int i = 0; i < menuItemsString.length; i++){ 
         menuItems[i] = new JButton(); 
         menuItems[i].setName(menuItemsString[i]); 
         addIcon(menuItems[i]); 
         menuToolbar.add(menuItems[i]); 
         if(i < menuItemsString.length){ 
          // Add spacing to the button menu 
          menuToolbar.addSeparator(new Dimension(4, 0)); 
         } 
        } 
    
        // Changing settings on the JComboBox that holds all the different kinds of components 
        // Changing the ComboBox to a fixed Size 
        componentList.setMaximumSize(new Dimension(200, 24)); 
    
        // Adding all the items to JComboBox 
        componentList.addItem(NOT_SELECTABLE_OPTION); 
        for(int i = 0; i < componentListStrings.length; i++){ 
         componentList.addItem(componentListStrings[i]); 
        } 
        // Setting actionListener to listen after changing the JComboBox 
        componentList.addActionListener(new ActionListener(){ 
         @Override 
         public void actionPerformed(ActionEvent e) { 
          componentSelected = true; 
          newComponent = cf.createComponent((String)componentList.getSelectedItem()); 
         } 
        }); 
    
        menuToolbar.add(componentList); 
    
        add(menuToolbar, BorderLayout.NORTH); 
    
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); 
        manager.addKeyEventDispatcher(new MyDispatcher()); 
    
    
    
        // Creating the center piece 
        innerCenter.setLayout(new BoxLayout(innerCenter, EXIT_ON_CLOSE)); 
    
        innerCenter.setPreferredSize(new Dimension(700, 400)); 
        innerCenter.setBackground(Color.LIGHT_GRAY); 
    
        innerCenter.addMouseListener(new MouseAdapter(){ 
         public void mousePressed(MouseEvent e){ 
          if(newComponent != null){ 
           Dimension size = newComponent.getPreferredSize(); 
    
           newComponent.setBounds(e.getX() - (size.width/2), e.getY() - (size.height/2), size.width, size.height); 
           newComponent.setLocation(e.getPoint()); 
    
           newComponent.addMouseListener(new MouseAdapter(){ 
            public void mousePressed(MouseEvent e){ 
             // TODO Update infopanel with info about marked component.... 
             String strBuilder = "Type: " + e.getComponent().getName() + "\n" + 
                  "ID: " + ((Components) e.getComponent()).getCompID() + "\n"; 
             infoContainer.append(strBuilder); 
            } 
           }); 
    
           innerCenter.add(newComponent); 
           innerCenter.repaint(); 
    
           componentSelected = false; 
           newComponent = null; 
          } 
         } 
        }); 
        centerPane.add(innerCenter); 
        centerPane.setVisible(true); 
        add(centerPane, BorderLayout.CENTER); 
    
        JPanel tempPane = new JPanel(); 
        tempPane.setLayout(new BoxLayout(tempPane, BoxLayout.PAGE_AXIS)); 
    
        // Right pane, info panel 
        // rightPane = new JPanel(); 
        // infoContainer = new JTextArea(); 
        infoContainer.setBackground(Color.LIGHT_GRAY); 
        infoContainer.setVisible(true); 
    
        JLabel tempLabel = new JLabel("Information about component:"); 
        tempPane.add(tempLabel); 
        tempPane.add(infoContainer); 
        rightPane.add(tempPane); 
    
        add(rightPane, BorderLayout.EAST); 
    
        setVisible(true); 
        pack(); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 
    

    とイメージ(私がより良い何を意味するか最初のイメージを説明します。コンポーネントは、onclickのボードの上に置かれ、第二私は、コードを追加したとき

    これが起こりました画像:コンポーネントをもう一度クリックすると、右のパネルに表示されます)

    コンポーネントを追加:

    Adding component

    それに二回クリック:あなたはSwingのレイアウトが混乱している

    Clicking on it a second time

  • 答えて

    1

    を。あなたはレイアウトマネージャでパネルを設定している :

    innerCenter.setLayout(new BoxLayout(innerCenter, EXIT_ON_CLOSE)); 
    

    しかし、その後、あなたはそれを手動で配置しているかのようにその上のコンポーネントの境界を設定しようとしている:

    newComponent.setBounds(e.getX() - (size.width/2), e.getY() - (size.height/2), size.width, size.height); 
    newComponent.setLocation(e.getPoint()); 
    
    をあなたはそれを手動で配置したい場合は

    は、あなたがパネルからレイアウトマネージャを削除する必要があります。だから、

    innerCenter.setLayout(null); 
    

    あなたはそれをすべてのあなたが境界を設定しますスイングと場所を言っていること手作りの子供たち。それ以外の場合は、コンテナを追加/削除/再検証/パックするたびに、レイアウトマネージャはすべてのレイアウトを変更しようとします。

    +0

    ありがとうございます!今の魅力のように動作します。手動でボタンを配置しているときにその部分を忘れてしまった。 –

    関連する問題