2010-12-19 59 views
1

我似乎無法得到這個工作。我試圖讓JTextField(input)在按下回車鍵時將文本傳遞給位於其上方的JTextArea(reader),但它們甚至沒有顯示在JFrame中。任何想法如何讓這個代碼snippit正常工作?我可能忽略了這個明顯的。Swing組件難點

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


public class Gui extends JPanel implements ActionListener{ 

    public static JTextField input; 
    public JTextArea reader; 


    public Gui() { 

     //layout to be repeatedly used 
     SpringLayout s_layout = new SpringLayout(); 

     //masterpanel, everything's on it 
     JPanel all = new JPanel(); 
     all.setPreferredSize(new Dimension(600,400)); 
     all.setLayout(s_layout); 

     //left jpanel - holds reader and input areas 
     JPanel mainleft = new JPanel(); 
     mainleft.setPreferredSize(new Dimension(500,400)); 
     mainleft.setLayout(s_layout); 
     //input textfield 
     input = new JTextField(45); 
     input.addActionListener(this); 
     //reader textarea 
     reader = new JTextArea(); 
     reader.setEditable(false); 
     reader.setLineWrap(true); 
     reader.setWrapStyleWord(true); 
     JScrollPane reader_sp = new JScrollPane(reader); 
     reader_sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
     reader_sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     reader_sp.setPreferredSize(new Dimension(480,350)); 

     //set layout constraints for reader and input 
     s_layout.putConstraint(SpringLayout.SOUTH, input, 
       -10, 
       SpringLayout.SOUTH, mainleft); 

     s_layout.putConstraint(SpringLayout.NORTH, reader_sp, 
       5, 
       SpringLayout.NORTH, mainleft); 

     //add all of the components to left panel 
     mainleft.add(reader_sp); 
     mainleft.add(input); 

     //add left panel to masterpanel 
     all.add(mainleft); 

     //add masterpanel to jframe 
     add(all); 

    } 

    public void actionPerformed(ActionEvent VK_ENTER) { 

     //get the command from input area 
     String text = input.getText(); 
     //append input to reader plus a new line 
     reader.append(text + "\n"); 
     //clear input area 
     input.setText(""); 
     //reset reader's caret position for next append 
     reader.setCaretPosition(reader.getDocument().getLength()); 


    } 


    private static void createAndShowGUI() { 

     JFrame f = new JFrame("why doesn't this work?"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(new Gui()); 
     f.pack(); 
     f.setLocationRelativeTo(null); //centers it 
     f.setResizable(true); 
     f.setVisible(true); 

    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 
+0

你的ActionListener沒問題。雖然我沒有看到您的組件,但由於您的佈局使用。 – 2010-12-19 03:04:27

回答

3

您正在爲all面板和mainLeft面板使用相同的佈局對象。刪除all.setLayout(s_layout)mainleft.setLayout(s_layout)行,並看看。

+0

wtf我是個白癡。非常感謝。 – whuff739 2010-12-19 03:10:15

1

你的問題是在佈局管理,或在你的佈局約束......如果你改變s_layout是一個FlowLayout中,例如,您的組件將顯示出來。