• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Set the location of the button

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output of this code has two buttons which placed side by side. I want to add two more buttons to move left and right. How to set the location of the button like in this picture . Which part of this code do I need to modify?


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Custom Graphics Example: Using key/button to move a line left or right.
*/
@SuppressWarnings("serial")
public class CGMoveALine extends JFrame {
// Name-constants for the various dimensions
public static final int CANVAS_WIDTH = 400;
public static final int CANVAS_HEIGHT = 140;
public static final Color LINE_COLOR = Color.BLACK;
public static final Color CANVAS_BACKGROUND = Color.CYAN;

// The line from (x1, y1) to (x2, y2), initially position at the center
private int x1 = CANVAS_WIDTH / 2;
private int y1 = CANVAS_HEIGHT / 8;
private int x2 = x1;
private int y2 = CANVAS_HEIGHT / 8 * 7;

private DrawCanvas canvas; // the custom drawing canvas (extends JPanel)

**strong text**/** Constructor to set up the GUI */
public CGMoveALine() {
// Set up a panel for the buttons
JPanel btnPanel = new JPanel(new FlowLayout());
JButton btnLeft = new JButton("Move Left ");
btnPanel.add(btnLeft);
btnLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
x1 -= 10;
x2 -= 10;
canvas.repaint();
requestFocus(); // change the focus to JFrame to receive KeyEvent
}
});
JButton btnRight = new JButton("Move Right");
btnPanel.add(btnRight);
btnRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
x1 += 10;
x2 += 10;
canvas.repaint();
requestFocus(); // change the focus to JFrame to receive KeyEvent
}
});

// Set up a custom drawing JPanel
canvas = new DrawCanvas();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));

// Add both panels to this JFrame
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(canvas, BorderLayout.CENTER);
cp.add(btnPanel, BorderLayout.SOUTH);

// "this" JFrame fires KeyEvent
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
switch(evt.getKeyCode()) {
case KeyEvent.VK_LEFT:
x1 -= 10;
x2 -= 10;
repaint();
break;
case KeyEvent.VK_RIGHT:
x1 += 10;
x2 += 10;
repaint();
break;
}
}
});

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Handle the CLOSE button
setTitle("Move a Line");
pack(); // pack all the components in the JFrame
setVisible(true); // show it
requestFocus(); // set the focus to JFrame to receive KeyEvent
}

/**
* DrawCanvas (inner class) is a JPanel used for custom drawing
*/
class DrawCanvas extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(CANVAS_BACKGROUND);
g.setColor(LINE_COLOR);
g.drawLine(x1, y1, x2, y2); // draw the line
}
}

/** The entry main() method */
public static void main(String[] args) {
// Run GUI codes on the Event-Dispatcher Thread for thread safety
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CGMoveALine(); // Let the constructor do the job
}
});
}
}

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have arrived in the wrong forum; this one is for discussing the website itself. Never fear! I can move you somewhere more appropriate. Your code us illegible because you haven't indented it. Please tell us how you are adding the 4 buttons, and which layout you are using. Also how you would like them to be arranged. Then we can probably help you.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key to layout management is that you can't always use just one layout manager.

So in this case you might use a vertical BoxLayout and then add components/panels to this panel:



Read the section from the Swing tutorial on How to Use BoxLayout for more information and examples
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To get OP's set up, add this to Rob's code:
 
reply
    Bookmark Topic Watch Topic
  • New Topic