Hi everybody, i have a problem on this code.
The ideas is to make a moveable message and that string message obtained from JTextField
_______________________________________________
|JPanel("Input Word") | JTextField |
|______________________________________________|
| |
| |
| |
| here is the moveable message |
| |
| |
|______________________________________________|
This is the part of my code, not finished yet.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestListener extends JFrame { private JTextField jtfMessage = new JTextField(); private JPanel p = new JPanel(new GridLayout(1, 2)); public TestListener() { p.add(new JLabel("Word")); p.add(jtfMessage); //add(p, BorderLayout.CENTER); add(new MovableMessagePanel("Welcome to Java")); } public static void main(String[] args) { TestListener frame = new TestListener(); frame.setSize(300, 300); frame.setTitle("Test move and Listener"); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } private class MovableMessagePanel extends JPanel { private String message = "Welcome to java"; int x = 20; int y = 20; public MovableMessagePanel(String s) { message = s; addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { x = e.getX(); y = e.getY(); repaint(); } }); } protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawString(message, x, y); } } }
If i use BorderLayout.CENTER for jtextfield
and BorderLayout.SOUTH for movablemessage
will cause the moveablemessage disappear.
Please help me thanks.