在閱讀大量java swing文章後,我幾乎失明,仍然無法讓面板工作。
當我添加2個JLabel時,它們很好地與左邊對齊,由EmptyBorder定義5px填充,就像我希望它們那樣。JLabel在添加JPanel後錯位了
我發現有額外的邊框加入了進度條後填充如預期,不工作,添加1個更多的面板,在這裏我添加進度。進展看起來不錯,但我所有的標籤都被取代了。
最後它看起來像這樣(紅色背景是調試,看到的JPanel如何平):
問題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;
}
}
我建議修改的問題和消除未來的問題3.它是無關的稱號你原來的問題。 – sm4