将多个面板和事件添加到GUI

时间:2015-09-21 04:38:21

标签: java swing events mouseevent jtextfield

我是java新手,正在进行GUI分配,我遇到了一些问题。前提是一个简单的GUI窗口,它有一些鼠标事件和一些键盘事件。

我把一个带有一些鼠标事件的窗口放在一起,一旦开始工作就开始向窗口添加一些JTextFields,但它们没有出现在窗口上,我不确定为什么。

现在是问题所在。我创建了一个新的面板(panel2)来将JTextFields添加到窗口中,我现在看到了JTextFields,但它超过了整个窗口,鼠标事件不能用于它。如果我将JTF添加到鼠标事件上方的面板中,则JTF不会显示,鼠标事件也可以工作.......

import javax.swing.*;

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

    public class EventDemo extends JFrame {

        private JPanel mousePanel;
        private JPanel panel1;
        private JPanel panel2;
        private JPanel panel3;
        private JLabel statusBar;
        private JLabel directions1;
        private JLabel directions2;
        private JLabel directions3;
        private JTextField textField1;
        private JTextField textField2;

    public EventDemo() {
        super("EVENT DEMO PROGRAM");

    panel1 = new JPanel();
    panel2 = new JPanel();

    //Add directions for the events to top of the window.
    directions1 = new JLabel("Enter & Leave window." + 
                            "    Press & hold, release, drag, move cursor to display a message in statusbar." + 
                            "    Clicking in one spot will display coordinates.");  
    panel1.add(directions1);
    add(panel1, BorderLayout.PAGE_START);   

    //Add mouse and statusBar to panel.
    mousePanel = new JPanel();
    mousePanel.setBackground(Color.WHITE); 
    add(mousePanel, BorderLayout.CENTER);   
    statusBar = new JLabel("Default");
    add(statusBar, BorderLayout.SOUTH);

    //Create handler object for Mouse events
    TheHandler handler = new TheHandler();
    mousePanel.addMouseListener(handler);
    mousePanel.addMouseMotionListener(handler); 


    textField1 = new JTextField(10);
    panel2.add(textField1, BorderLayout.SOUTH);
    textField2 = new JTextField("Enter Text Here");
    panel2.add(textField2, BorderLayout.SOUTH);
    add(panel2);

    TheHandler handlerJTF = new TheHandler();
    textField1.addActionListener(handlerJTF);
    textField2.addActionListener(handlerJTF); 


    }

    private class TheHandler implements MouseListener, MouseMotionListener {
        public void mouseClicked(MouseEvent event) {
            statusBar.setText(String.format("YOU CLICKED THE MOUSE AT %d, %d", event.getX(), event.getY()));
        }

        public void mousePressed(MouseEvent event) {
            statusBar.setText("YOU HAVE PRESSED THE MOUSE BUTTON.");
        }

        public void mouseReleased(MouseEvent event) {
            statusBar.setText("YOU HAVE RELEASED THE MOUSE BUTTON.");
        }

        public void mouseEntered(MouseEvent event) {
            statusBar.setText("YOU HAVE ENTERED THE WINDOW THE BACKGROUND CHANGES TO RED.");
            mousePanel.setBackground(Color.RED);
        }

        public void mouseExited(MouseEvent event) {
            statusBar.setText("EXITING THE WINDOW, BACKGROUND CHANGES BACK TO WHITE.");
            mousePanel.setBackground(Color.WHITE);
        }

        //Mouse Motion events.
        public void mouseDragged(MouseEvent event) {
            statusBar.setText("YOU ARE DRAGGING THE MOUSE.");
        }
        public void mouseMoved(MouseEvent event) {
            statusBar.setText("YOU ARE MOVING THE MOUSE AROUND.");
        }
    }

    public void actionPerformed(ActionEvent event) {
        textField1.getText();
        textField2.getText();
    }
    public static void main(String[] args) {
        EventDemo go = new EventDemo();
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.setSize(960, 300);
        go.setVisible(true);
        new EventDemo();

    }
    }

1 个答案:

答案 0 :(得分:2)

  1. TheHandler没有implement ActionListener这是JTextField#addActionListener所需的类型。有关详细信息,请参阅How to Write an Action ListenersHow to Use Text Fields
  2. 您的actionPerformed方法实际上并未在TheHandler类中定义,而是EventDemo类的方法
  3. 当您认为重写方法(或实现@Override中的方法)时,应使用interface注释,因为这会在您出错时导致编译错误(由于某种原因)
  4. 您的字段未显示有两个原因,首先,textField已添加到panel2textField2从未添加到任何内容,BorderLayout.CENTER已添加到框架中(默认情况下) add(mousePanel, BorderLayout.CENTER);)的位置,但被BorderLayout覆盖/规避,因此它实际上从未被帧{{1}}列出。有关详细信息,请查看How to Use Borders