繁体   English   中英

将标签添加到JComboBox和JTextField

[英]Adding a label to a JComboBox and JTextField

嗨我无法在我的组合框和textfield中添加标签。它编译得很好,但只显示框但没有标签。

import javax.swing. *;
import java.awt.event. *;   
import java.awt.FlowLayout;        

public class AreaFrame2  
{  

   public static void main(String[]args)  
   { 

      //Create array containing shapes  
      String[] shapes ={"(no shape selected)","Circle","Equilateral Triangle","Square"};  

      //Use combobox to create drop down menu
      JComboBox comboBox=new JComboBox(shapes);
      JPanel panel1 = new JPanel(); 
      JLabel label1 = new JLabel("Select shape:");
      panel1.add(label1);
      comboBox.add(panel1);
      JButton button = new JButton("GO");
      JTextField text = new JTextField(20);

      //Create a JFrame that will be use to put JComboBox into it 
      JFrame frame=new JFrame("Area Calculator Window");  
      frame.setLayout(new FlowLayout()); //set layout
      frame.add(comboBox);//add combobox to JFrame
      text.setLocation(100,100);
      frame.add(text);
      frame.add(button);

      //set default close operation for JFrame 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      //set JFrame ssize 
      frame.setSize(250,250);  

      //make JFrame visible. So we can see it 
      frame.setVisible(true);  

   }  
}  

我认为以下代码会产生或多或少的预期。

    public static void main(String[]args)
{
    //Create array containing shapes
    String[] shapes ={"(no shape selected)","Circle","Equilateral Triangle","Square"};

    //Use combobox to create drop down menu
    JComboBox comboBox=new JComboBox(shapes);
    JPanel panel1 = new JPanel(new FlowLayout());
    JLabel label1 = new JLabel("Select shape:");
    panel1.add(label1);
    panel1.add(comboBox);

    JButton button = new JButton("GO");
    JTextField text = new JTextField(20);
    //Create a JFrame that will be use to put JComboBox into it
    JFrame frame=new JFrame("Area Calculator Window");
    frame.setLayout(new FlowLayout()); //set layout
    frame.add(panel1);
    frame.add(text);
    frame.add(button);
    //set default close operation for JFrame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //set JFrame ssize
    frame.setSize(250,250);

    //make JFrame visible. So we can see it
    frame.setVisible(true);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM