Eclipse WindowBuilder(swing) - 代码中无法访问某些组件?

时间:2013-04-11 14:59:40

标签: java eclipse swing windowbuilder

修改 - @Heuster链接another question即可解答此问题。

我刚刚发现了WindowBuilder,我正在制作一个简单的聊天客户端,用它来教我自己。现在我已经完成了基本的聊天框架,但只有部分我添加的组件可以在代码中访问。具体来说,我无法访问我的输入JTextArea,taInput。有什么我需要做的就是能够引用它(在其中发送文本以便发送等)吗?

以下是设计视图的图片:

这是生成的代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;

public class frame extends JFrame
{

    private JPanel contentPane;
    private JButton btnSend;
    private JTextArea taDisplay;

    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    frame frame = new frame();
                    frame.setVisible(true);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public frame()
    {
        setResizable(false);
        setTitle("Client");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 440, 316);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        JMenuItem mntmConnect = new JMenuItem("Connect...");
        mnFile.add(mntmConnect);

        JMenuItem mntmSaveChatLog = new JMenuItem("Save chat log...");
        mnFile.add(mntmSaveChatLog);

        JMenuItem mntmSettings = new JMenuItem("Settings...");
        mnFile.add(mntmSettings);

        JMenuItem mntmClose = new JMenuItem("Close");
        mnFile.add(mntmClose);

        JMenu mnEdit = new JMenu("Edit");
        menuBar.add(mnEdit);

        JMenu mnView = new JMenu("View");
        menuBar.add(mnView);

        JMenu mnHelp = new JMenu("Help");
        menuBar.add(mnHelp);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(null);

        btnSend = new JButton("Send");
        btnSend.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent arg0)
            {
                taDisplay.append("Send clicked.\n");
            }
        });
        btnSend.setBounds(314, 197, 100, 50);
        panel.add(btnSend);

        taDisplay = new JTextArea();
        taDisplay.setLineWrap(true);
        taDisplay.setEditable(false);
        taDisplay.setBounds(10, 11, 404, 180);
        panel.add(taDisplay);

        JScrollPane spInput = new JScrollPane();
        spInput.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        spInput.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        spInput.setBounds(10, 197, 294, 49);
        panel.add(spInput);

        JTextArea taInput = new JTextArea();
        taInput.setLineWrap(true);
        spInput.setViewportView(taInput);
    }
}

1 个答案:

答案 0 :(得分:1)

在设计选项卡中,您可以右键单击项目(taInput),然后在上下文菜单中单击重命名,在对话框中,名称右侧有2个按钮,c上(f)按钮(字段)然后确定。

enter image description here