摆动组件的困难

时间:2010-12-19 02:54:13

标签: java swing

我似乎无法让这一切工作。我正在尝试使用JTextField(input)将文本传递给位于其上方的JTextArea(reader),当按下回车键时,它们甚至不会显示在JFrame中。任何想法如何让我的代码snippit正常工作?我可能忽视了显而易见的事情。

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

2 个答案:

答案 0 :(得分:3)

您正在为all面板和mainLeft面板使用相同的布局对象。移除all.setLayout(s_layout)mainleft.setLayout(s_layout)行并查看。

答案 1 :(得分:1)

您的问题出在LayoutManager或布局约束中......例如,如果您将s_layout更改为FlowLayout,您的组件就会显示出来。