Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Set the location of the button  RSS feed

 
iman mutiara
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
}
});
}
}

 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • 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
 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get OP's set up, add this to Rob's code:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!