添加JPanel后,JLabel放错地方

时间:2013-05-30 09:57:10

标签: java swing jpanel jlabel

在阅读了大量的java文章之后,我几乎失明了,但仍然无法让小组工作 当我添加2个JLabel时,它们很好地对齐到左边,由EmptyBorder定义的5px填充,正如我想要的那样。

enter image description here

我发现在为带填充添加额外边框的ProgressBar后,无法按预期工作,添加了1个面板,我添加了ProgressBar。进展看起来不错,但我的所有标签都被取代了 最后它看起来像这样(RED背景用于调试,看看JPanel如何绘制): enter image description here

问题1:如何解决此问题?
问题2:摆动将面板放在其他面板内的标准方法与其他面板只是为了让我想要成型吗?



来源:

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 个答案:

答案 0 :(得分:1)

使用布局管理器而不仅仅是相互添加面板。特别是在你的情况下,我认为你可以使用 GridLayout

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

有关布局的详细信息,请参阅此tutorial

答案 1 :(得分:1)

或者,您可以BorderLayout使用panel

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();
            }
        });
    }
}