JCombo Box在java中的定位

时间:2023-01-28 11:20:05

I would like to position a JComboBox in the way illustrated in the picture below. I was wondering if there is easy code to achieve this?

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

JCombo Box在java中的定位

3 个解决方案

#1


3  

JCombo Box在java中的定位

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://*.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


3  

use an appropriate layout manager, e.g. BoxLayout. for more information, please see How to Use BoxLayout.

使用适当的布局管理器,例如BoxLayout的。有关更多信息,请参阅如何使用BoxLayout。

#3


1  

you could use MigLayout with row and column constraints like [max][min][max] - would divide your root panel into a 3x3 grid with "greedy" borders, and in the middle area (the one thats min/min) drop a JPanel with a vertical flow layout, into which you could add your components

您可以使用具有行和列约束的MigLayout,例如[max] [min] [max] - 将根面板划分为具有“贪婪”边框的3x3网格,并在中间区域(最小/分钟的那个)中删除a具有垂直流布局的JPanel,您可以在其中添加组件

#1


3  

JCombo Box在java中的定位

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://*.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


3  

use an appropriate layout manager, e.g. BoxLayout. for more information, please see How to Use BoxLayout.

使用适当的布局管理器,例如BoxLayout的。有关更多信息,请参阅如何使用BoxLayout。

#3


1  

you could use MigLayout with row and column constraints like [max][min][max] - would divide your root panel into a 3x3 grid with "greedy" borders, and in the middle area (the one thats min/min) drop a JPanel with a vertical flow layout, into which you could add your components

您可以使用具有行和列约束的MigLayout,例如[max] [min] [max] - 将根面板划分为具有“贪婪”边框的3x3网格,并在中间区域(最小/分钟的那个)中删除a具有垂直流布局的JPanel,您可以在其中添加组件