JComboBox 宽度

发布于 2024-10-10 18:05:01 字数 794 浏览 9 评论 0原文

我创建了一个 jComboBox,但它占据了框架的整个宽度。如何设置固定宽度。

是的,框架的边框布局和面板的框布局。我在这里添加代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

水波映月 2024-10-17 18:05:01

宽度由添加到组合框中的最大项目的宽度自动确定。您可以通过以下方式控制显示:

comboBox.setPrototypeDisplayValue("text here");

您还可以考虑使用 组合框Popup 控制弹出窗口的大小。

编辑:

由于您添加了显示您正在使用 BoxLayout 的代码,因此您可以尝试以下操作:

comboBox.setMaximumSize( comboBox.getPreferredSize() );

或者您可以执行以下操作:

JPanel wrapper = new JPanel();
wrapper.add( comboBox );
panel.add( wrapper );

阅读 使用布局管理器了解这些建议的工作原理。

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.

残疾 2024-10-17 18:05:01

尝试comboBox.setPreferredWidth(200);或其他一些值来设置宽度

jzd 是正确的。实际的 API 是 setPreferredSize(new Dimension(...));

try comboBox.setPreferredWidth(200); or some other value to set the width

jzd is right. The actual API is setPreferredSize(new Dimension(...));

追风人 2024-10-17 18:05:01

您可以使用框布局执行以下操作。

  • 将轴改为线轴,添加
  • 水平胶水,添加刚性区域,
  • 放置元件


代码片段如下:

panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
panel.add(Box.createHorizontalGlue());
panel.add(Box.createRigidArea(new Dimension(10, 0)));
panel.add(combo);
frame.getContentPane().add(BorderLayout.NORTH, panel);

Here is something you can do with box layout.

  • Change axis to line axis, Add
  • horizontal glue, Add rigid area,
  • place the component

.
code snippet below:

panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
panel.add(Box.createHorizontalGlue());
panel.add(Box.createRigidArea(new Dimension(10, 0)));
panel.add(combo);
frame.getContentPane().add(BorderLayout.NORTH, panel);
乱世争霸 2024-10-17 18:05:01

您可能想使用 setSize() 方法。

combo.setSize(200, combo.getPreferredSize().height);

You might want to use setSize() method.

combo.setSize(200, combo.getPreferredSize().height);
无语# 2024-10-17 18:05:01

使用不同的布局管理器。尝试FlowLayout

Use a different LayoutManager. Try FlowLayout.

梦里°也失望 2024-10-17 18:05:01

不要使用 JCombobox<>() 而不是使用 Combobox<>() ,那么宽度不会改变,而且如果您设置首选大小,它也不会随 Combobox 改变,所以请使用它

Don't use JCombobox<>() instead of that use Combobox<>() then width won't change, also if you set preferred size it won't change with Combobox so use this instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文