2013-11-23 40 views
-3

所以我對這個小程序的工作,當我跑在這個problem.Its主要是關於位置設置了GridBagLayout的它不會告訴我的文本該JLable.Another問題是,在將我的進步每那些正在停止工作,後來來到back.Any想法它是什麼?我也喜歡有一個人也幫我的位置問題,我會希望放置的JTextField中左下角任何幫助嗎?我不能去解決這個問題,並沒有信息網上專門幫助me.So也許你可以幫助me.Hers到目前爲止我的代碼...位置設置了GridBagLayout的是不會好與JTextField的&的JButton不會生效

package Main; 

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 


public class code extends JFrame{ 

    public static JTextField consol; 
    public static String title = "Metal-Lock:The Start"; 
    public static Dimension size = new Dimension(650, 550); 
    public static JPanel panel; 
    public static JButton enter; 
    public static JLabel output; 

    public static void main(String args[]) { 
     code frame = new code(); 
    } 
     public code() { 
      setTitle(title); 
      setSize(size); 
      setResizable(true); 
      setLocationRelativeTo(null); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      setVisible(true); 

     // VISITOR LIST 
      CONSOL(); 
      P1(); 
      P2(); 
} 
//****************************************************************************************************************************** 

     public void CONSOL() { 
       consol = new JTextField(30); 

       consol.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e){ 
         String input = consol.getText(); 
         output.setText(input); 
        }}); 
       final JButton enter = new JButton("Enter"); 
       enter.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e){ 
         String input = consol.getText(); 
         output.setText(input); 
         panel.add(consol); 
         add(panel); 

         panel.add(enter); 
         add(panel); 

         output = new JLabel(); 
         panel.add(output); 
         add(panel); 
        } 
       });  
       } 

//****************************************************************************************************************************** 
     public void P1() { 

     } 

//****************************************************************************************************************************** 
     public static JLabel grid1; 
     public static JLabel grid2; 
     public static JLabel grid3; 
     public static JLabel grid4; 
     public static JLabel grid5; 

     public void P2() { 

      JPanel panel = new JPanel(new GridBagLayout()); 
      GridBagConstraints R = new GridBagConstraints(); 

     JLabel grid1 = new JLabel ("Hello"); panel.add(grid1, R); 
     R.gridx = 0; R.gridy = 0; 
     JLabel grid2 = new JLabel ("Hello"); panel.add(grid2, R); 
     R.gridx = 0; R.gridy = 0; 
     JLabel grid3 = new JLabel ("Hello"); panel.add(grid3, R); 
     R.gridx = 0; R.gridy = 0; 
     JLabel grid4 = new JLabel ("Hello"); panel.add(grid4, R); 
     R.gridx = 0; R.gridy = 0; 
     JLabel grid5 = new JLabel ("Hello"); panel.add(grid5, R); 
     R.gridx = 0; R.gridy = 0; 

     } 
} 
+0

,你在哪裏在容器中添加組件?? – subash

+0

如果您使用Eclipse,則可以使用UI編輯器。 對於實例的WindowBuilder。之後,您也可以從生成的代碼中學習。 –

+0

TY你的GaborCsikós用於顯示我的窗口建設者,但現在我必須做出輸入按鈕的工作,這樣它會顯示我在打字。赫斯等,以我的新問題! http://stackoverflow.com/questions/20169052/press-on-button-equals-print-what-ever-is-on-jtextfiled-on-the-lable – user3024659

回答

0

剛走到真的很快通過您的碼。

  1. 更改這些行:

    GridBagConstraints R = new GridBagConstraints(); 
    R.gridx = 0; R.gridy = 0;  
    JLabel grid1 = new JLabel ("Hello"); 
    //important to set these R values BEFORE you ad grid1. 
    panel.add(grid1, R); 
    

    改變這一切有行...

  2. 你加入6個標籤所有的位置爲gridx = 0,gridy = 0,是錯的。想象一下,它像一個Excel TABEL,要插入每一個標籤字段1

    如果你想添加字段做這樣

    例如

x y 
z 

//a.gridx=0; a.gridy=0; 
//y.gridx=1; y.gridy=0; 
//z.gridx=0; z.gridy=1; 

x coordinates are horizontal. 
y are vertical starting from the top left corner.
  • 閱讀這篇文章,它是真正的好: