2012-12-07 132 views
0

嗨我想創建一個由JComboBox和JTextField組成的接口。我已經整理出了將代碼添加到JComboBox的代碼,但是我無法在文本字段中添加標籤。任何幫助,將不勝感激。將標籤添加到文本字段

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

    public class AreaFrame3 extends JFrame 
    { 

     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()); //set frame layout 

     JLabel label1 = new JLabel("Select shape:"); 

     panel1.add(label1); 

     panel1.add(comboBox); 



     JTextField text = new JTextField(10); //create text field 


     JFrame frame=new JFrame("Area Calculator Window");//create a JFrame to put combobox 

     frame.setLayout(new FlowLayout()); //set layout 

     frame.add(panel1); 

     frame.add(text); 

     JButton button = new JButton("GO"); //create GO button 

     frame.add(button); 

     //set default close operation for JFrame 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


     //set JFrame ssize 

     frame.setSize(400,250); 

     //make JFrame visible. So we can see it 

     frame.setVisible(true); 

     } 

    } 
+0

在frame.setSize()之前加上'frame.pack()'並更新你的qustion。 – trashgod

+0

「將標籤添加到文本字段」是什麼意思?設置文本字段的文本?在你的文本框內有一些「幽靈文本」?或者只是在文本框前/後添加標籤?在後一種情況下,你應該簡單地模仿你爲組合框做了什麼。 –

+0

只是想在文本框前添加一個標籤。我之前模仿了我爲組合框做的事,但沒有奏效。也許我做錯了 –

回答

2

這是一種方法。只需以適當的順序將所有小部件放入您的panel1即可。

從長遠來看,這可能不是很好維護,你會希望有一個比FlowLayout更好的LayoutManager,但如果你只是想學習Swing,這可能是一個好的開始。如果您覺得FlowLayout不夠好,請查看LayoutManager教程。我個人的最愛是:BorderLayoutGridBagLayoutMigLayout也可能是一個好的,但我從來沒有使用它,它不是JVM的一部分。

import java.awt.FlowLayout; 

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

public class AreaFrame3 { 

    protected void initUI() { 
     // 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); 
     JLabel label1 = new JLabel("Select shape:"); 
     JPanel panel1 = new JPanel(new FlowLayout()); // set frame layout 
     JLabel label2 = new JLabel("Text label:"); 
     JTextField text = new JTextField(10); // create text field 
     panel1.add(label1); 
     panel1.add(comboBox); 
     panel1.add(label2); 
     panel1.add(text); 
     JFrame frame = new JFrame("Area Calculator Window");// create a JFrame to put combobox 
     frame.setLayout(new FlowLayout()); // set layout 
     frame.add(panel1); 
     JButton button = new JButton("GO"); // create GO button 
     frame.add(button); 
     // set default close operation for JFrame 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     // make JFrame visible. So we can see it 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new AreaFrame3().initUI(); 
      } 
     }); 
    } 
}