• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can this button be align at the top left coner of a container

 
joseph okon
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;

public class HomePage extends JFrame {
JFrame frame;

public HomePage() {
frame = new JFrame("Home");
frame.setBounds(100, 100,600,500);
setGUIPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private void setGUIPanel() {
Container content = frame.getContentPane();
content.setLayout(new BorderLayout(10,10));

//create a Base panel to hold navigation and display panels
JPanel basePanel = new JPanel();
basePanel.setLayout(new GridLayout(0,2,5,5));
content.add("Center",basePanel);

//create left panel and it's components and
//add to the Base panel
JPanel navPanel = new JPanel();
//Dimension size = new Dimension(80,20);
Box controlBox = Box.createVerticalBox();
JButton main = new JButton("Main");
//main.setPreferredSize(size);
JButton employ = new JButton("Employee");
//employ.setPreferredSize(size);
JButton admin = new JButton("Admin");
//admin.setPreferredSize(size);

//add button to the BoxLayout
controlBox.add(main);
controlBox.add(employ);
controlBox.add(admin);
//add controlBox to navPanel
navPanel.add(controlBox);


//add navPanel to BasePanel
basePanel.add(navPanel);
frame.setVisible(true);

}
public static void main(String[] args) {
HomePage hp = new HomePage();
}

}
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which button is this button? You have three of them.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by joseph okon:

can this button be align at the top left coner of a container


I'm not exactly sure what you are asking, but try changing
JPanel navPanel = new JPanel();
to
JPanel navPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
 
joseph okon
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry guys,
i want the all the three(3)buttons to be align horizontally,but at the top left
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by joseph okon:
i want the all the three(3)buttons to be align horizontally,but at the top left


The navPanel thing above will put them at the top left (as opposed to the top middle of the left side).

If you also want the buttons to all be the same size (i.e. to align on the right as well as on the left) then change
Box controlBox = Box.createVerticalBox();
to
JPanel controlBox = new JPanel(new GridLayout(0, 1, 0, x));

Use 0 for x if you want the buttons to run together. Use something like 5 to give them a little space.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic