Java中的简单下拉菜单

Noo*_*Ne0 10 java user-interface jlabel jbutton jcombobox

我正在使用Java中非常简单的GUI.

在这个GUI中我想显示:

  1. 页面顶部带有一些文字的标签
  2. 提到的标签下的JComboBox
  3. 在提到的JComboBox下的JButton

这是我的代码:

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Prova {

public static void main(String[] args) {

    JFrame frame = new JFrame("A Simple GUI");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setLocation(430, 100);

    JPanel panel = new JPanel();

    frame.add(panel);

    JLabel lbl = new JLabel("Select one of the possible choices and click OK");
    lbl.setVisible(true);

    panel.add(lbl);

    String[] choices = { "CHOICE 1","CHOICE 2", "CHOICE 3","CHOICE 4","CHOICE 5","CHOICE 6"};

    final JComboBox<String> cb = new JComboBox<String>(choices);

    cb.setVisible(true);
    panel.add(cb);

    JButton btn = new JButton("OK");
    panel.add(btn);

    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我得到的结果是

图片

正如您在图像中看到的那样,标签,JComboBox和JButton在同一条线上!

相反,我希望它们如上所述"堆叠":

一个JLabel

的JComboBox

一个JButton

我尝试使用setLocation(int x,int y)方法,但它们总是显示在相同的位置.

非常感谢!

Ner*_*kai 6

使用frame.setLayout(null);此功能可以将标签,按钮等放置在您喜欢的位置

  • oops它实际上是.setBounds(yPosition,xPosition,width,height),例如lbl.setBounds(10,10,300,10); (2认同)

bba*_*025 5

如果我理解你的问题,下面的代码可以完成你想要做的事情,而不会过于复杂:

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BoxLayout; // added code
import java.awt.Component; // added code

public class Prova {

public static void main(String[] args) {

    JFrame frame = new JFrame("A Simple GUI");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setLocation(430, 100);

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // added code

    frame.add(panel);

    JLabel lbl = new JLabel("Select one of the possible choices and click OK");
    lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
    //lbl.setVisible(true); // Not needed

    panel.add(lbl);

    String[] choices = { "CHOICE 1", "CHOICE 2", "CHOICE 3", "CHOICE 4",
                         "CHOICE 5", "CHOICE 6" };

    final JComboBox<String> cb = new JComboBox<String>(choices);

    cb.setMaximumSize(cb.getPreferredSize()); // added code
    cb.setAlignmentX(Component.CENTER_ALIGNMENT);// added code
    //cb.setVisible(true); // Not needed
    panel.add(cb);

    JButton btn = new JButton("OK");
    btn.setAlignmentX(Component.CENTER_ALIGNMENT); // added code
    panel.add(btn);

    frame.setVisible(true); // added code

    }
}
Run Code Online (Sandbox Code Playgroud)

setLocation方法通常过于复杂,除非您对布局有非常具体的(艺术?)目标。对于这个问题,一个更简单的解决方案是使用 aBoxLayout并指定您希望在 y 方向(垂直向下)添加内容。请注意,您必须指定JComboBox(以及您可能想要的其他一些 GUI 元素)的尺寸。稍后添加)以避免出现巨大的下拉菜单。参见 另一个 stackoverflow 帖子,JComboBox 宽度


Tae*_*rus 1

研究一些有关使用布局管理器的教程,您将在那里找到解决方案。但他们非常坚强。