View Single Post
  #2 (permalink)  
Old 03-27-2008, 05:24 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class XYRx extends JFrame { // Variables declaration private JTextField jTextField1; private JTextField jTextField2; //----- private JPanel jPanel1; //----- int x; int y; // End of variables declaration public XYRx() { super(); initializeComponent(); // // TODO: Add any constructor code after initializeComponent call // this.setVisible(true); } private void initializeComponent() { JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); jTextField1 = new JTextField(); jTextField2 = new JTextField(); JButton jButton1 = new JButton(); Container contentPane = (JPanel)this.getContentPane(); //----- jPanel1 = new JPanel() { protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.red); g.fillOval(x-2, y-2, 4, 4); } }; //----- // // jLabel1 // jLabel1.setText("X Coordinate"); // // jLabel2 // jLabel2.setText("Y Coordinate"); // // jTextField1 // jTextField1.addActionListener(al); // // jTextField2 // jTextField2.addActionListener(al); // // jButton1 // jButton1.setText("GO!"); jButton1.addActionListener(al); jPanel1.addMouseListener(ma); // // contentPane // contentPane.setLayout(null); addComponent(contentPane, jLabel1, 292,251,82,18); addComponent(contentPane, jLabel2, 292,295,84,17); addComponent(contentPane, jTextField1, 355,250,57,22); addComponent(contentPane, jTextField2, 355,292,57,22); addComponent(contentPane, jButton1, 330,332,62,28); addComponent(contentPane, jPanel1, 16,23,266,210); // // jPanel1 // jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); jPanel1.setBorder(BorderFactory.createLineBorder(Color.blue)); // // XY // this.setTitle("XY - extends JFrame"); this.setLocation(new Point(0, 0)); this.setSize(new Dimension(700, 500)); } /** Add Component Without a Layout Manager (Absolute Positioning) */ private void addComponent(Container container,Component c, int x,int y,int width,int height) { c.setBounds(x,y,width,height); container.add(c); } // // TODO: Add any appropriate code in the following Event Handling Methods // private ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { x = Integer.parseInt(jTextField1.getText()); y = Integer.parseInt(jTextField2.getText()); jPanel1.repaint(); } }; private MouseListener ma = new MouseAdapter() { public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); jPanel1.repaint(); } }; public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); try { UIManager.setLookAndFeel("com.sun.java" + ".swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception ex) { System.out.println("Failed loading L&F: "); System.out.println(ex); } new XYRx(); } }
Reply With Quote