JComboBox在java中的定位

时间:2013-01-09 17:46:12

标签: java swing jcombobox layout-manager

我想按照下图所示的方式定位JComboBox。我想知道是否有简单的代码来实现这个目标?

enter image description here

3 个答案:

答案 0 :(得分:3)

使用适当的布局管理器,例如BoxLayout。有关详细信息,请参阅How to Use BoxLayout

答案 1 :(得分:3)

Centered Combos

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

public class CenteredCombos {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                String[] values = {"Dog","Cat"};
                // put the controls in a single column, with a little space.
                JPanel controlPanel = new JPanel(new GridLayout(0,1,5,5));
                for (int ii=0; ii<4; ii++) {
                    controlPanel.add(new JComboBox(values));
                }

                // the GUI as seen by the user (without frame)
                // The GBL is used to center the controlPanel
                JPanel gui = new JPanel(new GridBagLayout());
                gui.setBorder(new EmptyBorder(2, 3, 2, 3));
                gui.add(controlPanel);

                // Make the BG panel more clear..
                gui.setBackground(Color.WHITE);

                JFrame f = new JFrame("Demo");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

答案 2 :(得分:1)

你可以使用MigLayout行和列限制,例如[max][min][max] - 将你的根面板分成带有“贪婪”边框的3x3网格,以及中间区域(最小/分钟的那个) )删除一个垂直flow layout的JPanel,你可以在其中添加你的组件