meow00
asked on
display a button using (x,y) coordinate ...
Hi experts,
this is actually a follow up question of :
https://www.experts-exchange.com/questions/21148929/I-can't-see-my-button.html
I need to display a button in my frame using (x,y) coordinates (setLocation ...). However, it doesn't work after many different tries. Does anyone have any suggestions ? thanks !!!
-------------------------- ---------- --
import java.lang.* ;
import javax.swing.*;
import java.awt.* ;
import java.awt.event.* ;
public class Play extends JFrame{
private JButton myButton ;
public Play() {
myButton = new JButton() ;
myButton.setSize(60,60) ;
myButton.setLocation(20,20 ) ;
getContentPane().setSize(5 00,500) ;
getContentPane().setLocati on(10,10) ;
getContentPane().add(myBut ton) ;
setDefaultCloseOperation(J Frame.EXIT _ON_CLOSE) ;
myButton.requestFocusInWin dow() ;
// setVisible(true); //Display the window.
setSize(600,600) ;
// pack() ;
show() ;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Play myPlay = new Play() ;
}
}
this is actually a follow up question of :
https://www.experts-exchange.com/questions/21148929/I-can't-see-my-button.html
I need to display a button in my frame using (x,y) coordinates (setLocation ...). However, it doesn't work after many different tries. Does anyone have any suggestions ? thanks !!!
--------------------------
import java.lang.* ;
import javax.swing.*;
import java.awt.* ;
import java.awt.event.* ;
public class Play extends JFrame{
private JButton myButton ;
public Play() {
myButton = new JButton() ;
myButton.setSize(60,60) ;
myButton.setLocation(20,20
getContentPane().setSize(5
getContentPane().setLocati
getContentPane().add(myBut
setDefaultCloseOperation(J
myButton.requestFocusInWin
// setVisible(true); //Display the window.
setSize(600,600) ;
// pack() ;
show() ;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Play myPlay = new Play() ;
}
}
u need to use a null layout
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the component you are adding must have:
setBounds(x,y,_,__) method called on them
setBounds(x,y,_,__) method called on them
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
meow00, we've already been through this, and i gave you working code:
https://www.experts-exchange.com/questions/21144542/a-moving-button.html
meow00, we've already been through this, and i gave you working code:
https://www.experts-exchange.com/questions/21144542/a-moving-button.html
:)
This example may be interesting and you may be able to change it to use in your application
/////////////////////////
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: SequelSys</p>
* @author Armoghan
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// KeyDemoGUI.java - JFrame subclass
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TestFrame {
public static void main(String[] args) {
JFrame window = new KeyDemoGUI();
// window.setDefaultCloseOper ation(EXIT _ON_CLOSE) ;
window.setVisible(true);
}
}
////////////////////////// ////////// ////////// ////////// //////// KeyDemoGUI
/** JFrame subclass for KeyDemo GUI.
virtual keys (arrows), and characters.
@author Fred Swartz
@version 2004-05-06
*/
class KeyDemoGUI extends JFrame {
MovingTextPanel drawing;
//======================== ========== ========== ========== ====constr uctor
public KeyDemoGUI() {
drawing = new MovingTextPanel();
this.getContentPane().setL ayout(new BorderLayout());
JLabel instructions = new JLabel("<html><ul><li>Type text.</li>"
+ "<li>Use arrow keys to move text.</li>"
+ "<li>Press shift arrows to move faster.</li></html>");
this.getContentPane().add( instructio ns, BorderLayout.NORTH);
this.getContentPane().add( drawing, BorderLayout.CENTER);
this.setTitle("KeyDemo");
this.pack();
drawing.requestFocus(); // Give the panel focus.
}//end constructor
}//endclass KeyDemoGUI
// KeyDemoGUI.java - JFrame subclass
////////////////////////// ////////// ////////// ////////// //////// KeyDemoGUI
/** JFrame subclass for KeyDemo GUI.
virtual keys (arrows), and characters.
@author Fred Swartz
@version 2004-05-06
*/
// MovingTextPanel.java - Demonstrates handling three kinds of keys:
////////////////////////// ////////// ////////// ////////// / class DrawingPanel
/** Define a new panel to draw on, therefore it has a paintComponent method.
This class implements KeyListener, altho the listeners don't really
have to be inside this class.
This class displays any characters that are typed, and handles
virtual keys (arrows) and modifiers (shift) to move the text.
@author Fred Swartz
@version 2004-05-06
*/
class MovingTextPanel extends JPanel implements KeyListener {
//======================== ========== ========== ========= field variables
String display = ""; // Initial string to display
private int x = 50; // Initial coordinates of string
private int y = 50;
private Font biggerFont = new Font("sansserif", Font.PLAIN, 24);
private int speed = 2; // number of pixels to move
//======================== ========== ========== ========== === constructor
public MovingTextPanel() {
this.setBackground(Color.w hite);
this.setFont(biggerFont);
this.setPreferredSize(new Dimension(300, 200));
this.addKeyListener(this); // This class has its own key listeners.
this.setFocusable(true); // Allow panel to get focus
}//endconstructor
//======================== ========== ========== ========== = paintComponent
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(display, x, y);
}//endmethod paintComponent
//======================== ========== ========== ======== keyTyped listener
/** This listener is called for character keys. */
public void keyTyped(KeyEvent kevt) {
//System.out.println("keyT yped");
char c = kevt.getKeyChar();
if (c == '\b') { // if this is a backspace
if (display.length() > 0) { // remove last character
display = display.substring(0, display.length()-1);
}
} else {
display += c;
}
this.repaint();
}//endmethod keyTyped
//======================== ========== ========== ====== keyPressed listener
/** This listener is called for both character and non-character keys. */
public void keyPressed(KeyEvent e) {
// Check the shift key, and do 10x the movement if the
// shift key is down when the arrow keys are pressed.
// Altho keyPressed is called for normal characters, they
// are ignored here and handled in keyTyped.
if (e.isShiftDown()) {
speed = 10;
} else {
speed = 2;
}
//-- Process arrow "virtual" keys
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT : x -= speed; x = Math.max(x, 0); break;
case KeyEvent.VK_RIGHT: x += speed; x = Math.min(x, 300); break;
case KeyEvent.VK_UP : y -= speed; y = Math.max(y, 0); break;
case KeyEvent.VK_DOWN : y += speed; y = Math.min(y, 200); break;
}
speed = 2; // Restore speed to its default value
this.repaint(); // Display the changes.
}//endmethod keyPressed
//------------------------ ---------- ---------- ----- keyReleased listener
public void keyReleased(KeyEvent ke) {} // Ignore.
}//endclass MovingTextPanel
/////////////////////////
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: SequelSys</p>
* @author Armoghan
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// KeyDemoGUI.java - JFrame subclass
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TestFrame {
public static void main(String[] args) {
JFrame window = new KeyDemoGUI();
// window.setDefaultCloseOper
window.setVisible(true);
}
}
//////////////////////////
/** JFrame subclass for KeyDemo GUI.
virtual keys (arrows), and characters.
@author Fred Swartz
@version 2004-05-06
*/
class KeyDemoGUI extends JFrame {
MovingTextPanel drawing;
//========================
public KeyDemoGUI() {
drawing = new MovingTextPanel();
this.getContentPane().setL
JLabel instructions = new JLabel("<html><ul><li>Type
+ "<li>Use arrow keys to move text.</li>"
+ "<li>Press shift arrows to move faster.</li></html>");
this.getContentPane().add(
this.getContentPane().add(
this.setTitle("KeyDemo");
this.pack();
drawing.requestFocus(); // Give the panel focus.
}//end constructor
}//endclass KeyDemoGUI
// KeyDemoGUI.java - JFrame subclass
//////////////////////////
/** JFrame subclass for KeyDemo GUI.
virtual keys (arrows), and characters.
@author Fred Swartz
@version 2004-05-06
*/
// MovingTextPanel.java - Demonstrates handling three kinds of keys:
//////////////////////////
/** Define a new panel to draw on, therefore it has a paintComponent method.
This class implements KeyListener, altho the listeners don't really
have to be inside this class.
This class displays any characters that are typed, and handles
virtual keys (arrows) and modifiers (shift) to move the text.
@author Fred Swartz
@version 2004-05-06
*/
class MovingTextPanel extends JPanel implements KeyListener {
//========================
String display = ""; // Initial string to display
private int x = 50; // Initial coordinates of string
private int y = 50;
private Font biggerFont = new Font("sansserif", Font.PLAIN, 24);
private int speed = 2; // number of pixels to move
//========================
public MovingTextPanel() {
this.setBackground(Color.w
this.setFont(biggerFont);
this.setPreferredSize(new Dimension(300, 200));
this.addKeyListener(this);
this.setFocusable(true); // Allow panel to get focus
}//endconstructor
//========================
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(display, x, y);
}//endmethod paintComponent
//========================
/** This listener is called for character keys. */
public void keyTyped(KeyEvent kevt) {
//System.out.println("keyT
char c = kevt.getKeyChar();
if (c == '\b') { // if this is a backspace
if (display.length() > 0) { // remove last character
display = display.substring(0, display.length()-1);
}
} else {
display += c;
}
this.repaint();
}//endmethod keyTyped
//========================
/** This listener is called for both character and non-character keys. */
public void keyPressed(KeyEvent e) {
// Check the shift key, and do 10x the movement if the
// shift key is down when the arrow keys are pressed.
// Altho keyPressed is called for normal characters, they
// are ignored here and handled in keyTyped.
if (e.isShiftDown()) {
speed = 10;
} else {
speed = 2;
}
//-- Process arrow "virtual" keys
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT : x -= speed; x = Math.max(x, 0); break;
case KeyEvent.VK_RIGHT: x += speed; x = Math.min(x, 300); break;
case KeyEvent.VK_UP : y -= speed; y = Math.max(y, 0); break;
case KeyEvent.VK_DOWN : y += speed; y = Math.min(y, 200); break;
}
speed = 2; // Restore speed to its default value
this.repaint(); // Display the changes.
}//endmethod keyPressed
//------------------------
public void keyReleased(KeyEvent ke) {} // Ignore.
}//endclass MovingTextPanel