2012-12-06 2 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

JComboBox에 JPanel을 추가하는 이유는 무엇입니까? 이것은 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

감사합니다. –