2012-12-06 92 views
0

嗨我無法將標籤添加到我的組合框和textfield.It編譯正常,但只顯示框但沒有標籤。將標籤添加到JComboBox和JTextField

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

    } 
} 
+0

您的代碼非常難以閱讀,因爲您正在使用空白區域進行過度操作。請編輯您的代碼,以使我們更容易幫助您。 –

+0

你爲什麼要將Janel添加到JComboBox?這是JComboBox的不正確用法。你想用這個做什麼? –

+0

哎呀,如果我必須自己修復你的代碼.... –

回答

2

我認爲下面的代碼會產生或多或少的你所期望的。

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

謝謝! –