我是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();
}
}
答案 0 :(得分:2)
TheHandler
没有implement
ActionListener
这是JTextField#addActionListener
所需的类型。有关详细信息,请参阅How to Write an Action Listeners和How to Use Text Fields actionPerformed
方法实际上并未在TheHandler
类中定义,而是EventDemo
类的方法@Override
中的方法)时,应使用interface
注释,因为这会在您出错时导致编译错误(由于某种原因) textField
已添加到panel2
,textField2
从未添加到任何内容,BorderLayout.CENTER
已添加到框架中(默认情况下) add(mousePanel, BorderLayout.CENTER);
)的位置,但被BorderLayout
覆盖/规避,因此它实际上从未被帧{{1}}列出。有关详细信息,请查看How to Use Borders