Java中的简单下拉菜单

时间:2014-03-19 12:47:34

标签: java user-interface jbutton jlabel jcombobox

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

在此GUI中,我想显示:

  1. 页面顶部带有一些文字的标签
  2. 上述标签下的JComboBox
  3. 上述JComboBox下的JButton
  4. 这是我的代码:

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

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

    image

    如图所示,标签,JComboBox和JButton在同一条线上!

    相反,我想要他们&#34;堆积&#34;如上所述:

    的JLabel

    的JComboBox

    的JButton

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

    非常感谢!

6 个答案:

答案 0 :(得分:6)

使用frame.setLayout(null);这将允许您将标签,按钮等放置在您喜欢的地方

答案 1 :(得分:4)

您应该使用Java标准布局之一(GridLayout,LinearLayout,BoxLayout)

我建议使用1列和3行的网格布局

如下所示

  setLayout(new GridLayout(1,3));
         add(new Button("1"));
         add(new Button("2"));
         add(new Button("3"));

答案 2 :(得分:3)

这是由于布局用于对齐孩子。默认情况下,它的FlowLayout将流中所有子组件从左到右放置,从而得到上面的显示。

根据您的要求,您可以使用具有3行1列的GridLayout。

GridLayout

All Layouts

答案 3 :(得分:2)

如果我理解了您的问题,以下代码可以完成您的尝试而不会过于复杂:

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

    }
}

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

答案 4 :(得分:1)

学习一些关于使用布局管理器的教程,这是您可以找到解决方案的地方。但它们非常艰难。

答案 5 :(得分:-1)

使用Panel.setLayout(null); 并对每个组件使用setBounds,以根据您的选择放置它们 例如:

JButton btn=new JButton("Press"); 
btn.setBounds(10, 10, 100, 20);//where(x, y, length, height)
JPanel.add(btn);//this is just a snippet, you will have to declare an object
//for the JPanel:)
相关问题