2013-05-30 59 views
2

在閱讀大量java swing文章後,我幾乎失明,仍然無法讓面板工作。
當我添加2個JLabel時,它們很好地與左邊對齊,由EmptyBorder定義5px填充,就像我希望它們那樣。JLabel在添加JPanel後錯位了

enter image description here

我發現有額外的邊框加入了進度條後填充如預期,不工作,添加1個更多的面板,在這裏我添加進度。進展看起來不錯,但我所有的標籤都被取代了。
最後它看起來像這樣(紅色背景是調試,看到的JPanel如何平): enter image description here

問題1:如何解決這一問題?
Question2:標準的擺動方法是將面板放置在其他面板的內部,以便與其他面板拼合以獲得我想要的格式?



來源:

public class AppInitProgressDialog { 
    private static final int  VIEW_PADDING_VAL = 5; 
    private static final Border  viewPaddingBorder = new EmptyBorder(VIEW_PADDING_VAL, VIEW_PADDING_VAL, VIEW_PADDING_VAL, VIEW_PADDING_VAL); 

    private JPanel   view; // Dialog view 
    private JPanel   panel; 

    private JPanel   progressPanel; 

    private JLabel   title; 
    private JLabel   progressDesc; 
    private JProgressBar progressBar; 

    private void initPanel(int w, int h) { 
     view = new JPanel(); 
     view.setBorder(BorderFactory.createRaisedSoftBevelBorder()); 
     view.setBackground(Color.LIGHT_GRAY); 
     view.setSize(w, h); 
     view.setLayout(new BorderLayout()); 

     panel = new JPanel(); 
     panel.setBorder(viewPaddingBorder); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); 
     //panel.setLayout(new SpringLayout()); 
     panel.setOpaque(false); 

     JFrame parent = AppContext.getMe().getAppWindow().getFrame(); 
     int posx = (parent.getWidth() - w)/2; 
     int posy = (parent.getHeight() - h)/2; 
     view.add(panel, BorderLayout.CENTER); 
     view.setLocation(posx, posy); 
    } 

    private void initTitle() { 
     title = new JLabel("Progress title"); 
     title.setAlignmentX(JComponent.LEFT_ALIGNMENT); 
     panel.add(title); 
    } 

    private void initProgress() { 
     progressPanel = new JPanel(new BorderLayout()); 
     progressPanel.setBackground(Color.LIGHT_GRAY); 
     progressPanel.setBorder(new EmptyBorder(15, 30, 15, 30)); 
     progressPanel.setBackground(Color.RED); 

     progressBar = new JProgressBar(0, 10000); 
     progressBar.setStringPainted(true); 
     progressBar.setAlignmentX(JComponent.LEFT_ALIGNMENT); 
     progressPanel.add(progressBar); 

     panel.add(progressPanel); 
     progressDesc = new JLabel("Progress description"); 
     panel.add(progressDesc); 
    } 

    public AppInitProgressDialog() { 
     initPanel(400, 100); 
     initTitle(); 
     initProgress(); 
    } 

    public JComponent getView() { 
     return view; 
    } 
} 
+2

我建議修改的問題和消除未來的問題3.它是無關的稱號你原來的問題。 – sm4

回答

1

或者,你可以使用BorderLayoutpanel

panel = new JPanel(new BorderLayout()); 
... 
panel.add(title, BorderLayout.NORTH); 
... 
panel.add(progressPanel); // default CENTER 
... 
panel.add(progressDesc, BorderLayout.SOUTH); 

image

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import javax.swing.BorderFactory; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JProgressBar; 
import javax.swing.border.EmptyBorder; 

/** 
* @see http://stackoverflow.com/a/16837816/230513 
*/ 
public class Test { 

    private JFrame f = new JFrame("Test"); 

    private void display() { 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(new AppInitProgressDialog().getView()); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    class AppInitProgressDialog { 

     private static final int VIEW_PADDING_VAL = 5; 
     private JPanel view; // Dialog view 
     private JPanel panel; 
     private JPanel progressPanel; 
     private JLabel title; 
     private JLabel progressDesc; 
     private JProgressBar progressBar; 

     private void initPanel(int w, int h) { 
      view = new JPanel(); 
      view.setBorder(BorderFactory.createRaisedBevelBorder()); 
      view.setBackground(Color.LIGHT_GRAY); 
      view.setSize(w, h); 
      view.setLayout(new BorderLayout()); 

      panel = new JPanel(new BorderLayout()); 
      panel.setBorder(BorderFactory.createLineBorder(Color.blue)); 
      panel.setOpaque(false); 

      view.add(panel, BorderLayout.CENTER); 
     } 

     private void initTitle() { 
      title = new JLabel("Progress title"); 
      title.setAlignmentX(JComponent.LEFT_ALIGNMENT); 
      panel.add(title, BorderLayout.NORTH); 
     } 

     private void initProgress() { 
      progressPanel = new JPanel(new BorderLayout()); 
      progressPanel.setBackground(Color.LIGHT_GRAY); 
      progressPanel.setBorder(new EmptyBorder(15, 30, 15, 30)); 
      progressPanel.setBackground(Color.RED); 

      progressBar = new JProgressBar(0, 10000); 
      progressBar.setStringPainted(true); 
      progressBar.setAlignmentX(JComponent.LEFT_ALIGNMENT); 
      progressPanel.add(progressBar); 

      panel.add(progressPanel); 
      progressDesc = new JLabel("Progress description"); 
      panel.add(progressDesc, BorderLayout.SOUTH); 
     } 

     public AppInitProgressDialog() { 
      initPanel(400, 100); 
      initTitle(); 
      initProgress(); 
     } 

     public JComponent getView() { 
      return view; 
     } 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Test().display(); 
      } 
     }); 
    } 
} 
1

使用佈局管理器,而不是僅僅將面板彼此。特別是在你的情況下,我認爲你可以使用GridLayout

panel.setLayout(new GridLayout(1,1,0,0)); 

有關佈局的更多詳細信息,請參閱此tutorial

+0

非常抱歉,我只能將一個答案標記爲正確,並且在某些情況下,網格也會起作用,但@trashgod答案聽起來更像是這樣做的方式。希望'投票'將會成爲現實,因爲你有些回答我的第二個問題,女巫聽起來像「使用面板佈局」。謝謝 –