java jcombox 宽度,JComboBox宽度

I have created a jComboBox but it takes the full width of the frame. how to set the width fixed.

yes borderlayout for the frame and box layout for the panel. i am adding the code here:

import javax.swing.*;

import java.awt.BorderLayout;

public class Window8 {

JFrame frame;

JPanel panel;

JComboBox combo;

public void go(){

String[] option = { "STUDENT", "TEACHER" };

combo.setPreferredSize(new Dimension(1,25));

combo = new JComboBox(option);

menu.setSelectedIndex(0);

frame = new JFrame("DELETION");

frame.setLocationRelativeTo(null);

frame.setSize(400, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

panel = new JPanel();

panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

frame.getContentPane().add(BorderLayout.NORTH,panel);

panel.add(combo);

}

解决方案

The width is automatically determined by the width of the largest item added to the combo box. You can control the display by using:

comboBox.setPrototypeDisplayValue("text here");

You might also consider using the Combo Box Popup to control the popup size.

Edit:

Since you added code that shows you are using a BoxLayout you can try the following:

comboBox.setMaximumSize( comboBox.getPreferredSize() );

Or you can do something like:

JPanel wrapper = new JPanel();

wrapper.add( comboBox );

panel.add( wrapper );

Read the section from the Swing tutorial on Using Layout Managers to understand how these suggestions work.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值