gpt4 book ai didi

java - JComboBox在java中的定位

转载 作者:搜寻专家 更新时间:2023-11-01 01:54:23 25 4
gpt4 key购买 nike

我想按照下图所示的方式放置一个JComboBox。我想知道是否有简单的代码可以实现这一点?

enter image description here

最佳答案

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

关于java - JComboBox在java中的定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14243263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com