Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Aligning left for JMenuBar

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    Okahoma, US
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Aligning left for JMenuBar

    I want the items in the JMenuBar to align to the left, and I cannot get it to do so. I have tried using FlowLayout, but that makes the height of the JMenuBar bigger, and I don't want that. How do I fix this so it would align left-to-right and have the regular height size?

    Another thing that irritates me is that the JMenuItems won't have an outline upon hover, and when you click it doesn't act like it was "pushed" down like the JMenu, except when you release your click it will no long be "pushed". How would I accomplish this as well?

    All day of searching and I haven't figured it out, here is my current code:

    public class Game extends Canvas implements Runnable {
    	private static final long serialVersionUID = 1L;
     
    	private final short WIDTH = 220;
    	private final short HEIGHT = WIDTH;
    	private final short SCALE = 2;
     
    	private JFrame frame;
    	private JMenuBar menu;
    	private JMenuItem menuItem;
    	private JMenuItem menuItem2;
    	private JPanel panel;
    	private JPanel panelTop;
    	private JPanel panelGame;
     
    	private Thread thread;
    	private Screen screen;
     
    	private boolean running = false;
     
    	private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    	private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
     
    	public Game() {
    		try {
    			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
     
    		setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
     
    		screen = new Screen(WIDTH, HEIGHT);
     
    		frame = new JFrame();
    		menu = new JMenuBar();
    		menuItem = new JMenuItem("Item");
    		menuItem2 = new JMenuItem("Two");
    		panel = new JPanel();
    		panelTop = new JPanel();
    		panelGame = new JPanel();
    	}
     
    	public synchronized void start() {
    		running = true;
    		thread = new Thread(this, "Test");
    		thread.start();
    	}
     
    	public synchronized void stop() {
    		running = false;
    		try {
    			thread.join();
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
     
    	public void run() {
    		long lastTime = System.nanoTime();
    		long timer = System.currentTimeMillis();
    		final double ns = 1000000000d / 60d;
    		double delta = 0;
    		int frames = 0;
    		int updates = 0;
     
    		while (running) {
    			long now = System.nanoTime();
    			delta += (now - lastTime) / ns;
    			lastTime = now;
     
    			while (delta >= 1) {
    				update();
    				updates++;
    				delta -= 1;
    			}
    			render();
    			frames++;
    			if (System.currentTimeMillis() - timer > 1000) {
    				timer += 1000;
    				frame.setTitle("Test     FPS: " + frames + "     UPS: " + updates);
    				frames = 0;
    				updates = 0;
    			}
    		}
    		stop();
    	}
     
    	public void update() {
    	}
     
    	public void render() {
    		BufferStrategy bs = getBufferStrategy();
    		if (bs == null) {
    			createBufferStrategy(3);
    			return;
    		}
    		screen.clear();
    		screen.render();
     
    		for (int i = 0; i < pixels.length; i++) {
    			pixels[i] = screen.pixels[i];
    		}
     
    		Graphics g = bs.getDrawGraphics();
    		g.setColor(Color.black);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
     
    		g.dispose();
    		bs.show();
    	}
     
    	public static void main(String[] args) {
    		Game game = new Game();
    		GridBagConstraints gbc = new GridBagConstraints();
     
    		game.panel.setLayout(new GridBagLayout());
     
    		gbc.gridy = 0;
     
    		game.panel.add(game.panelTop, gbc);
     
    		gbc.gridy = 1;
     
    		game.panelGame.setLayout(new GridBagLayout());
    		game.panelGame.add(game, gbc);
    		game.panel.add(game.panelGame, gbc);
     
    		game.menu.add(game.menuItem);
    		game.menu.add(game.menuItem2);
     
    		game.frame.setTitle("Test");
    		game.frame.setResizable(false);
    		game.frame.add(game.panel);
    		game.frame.pack();
    		game.frame.setJMenuBar(game.menu);
    		game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		game.frame.setLocationRelativeTo(null);
    		game.frame.setVisible(true);
     
    		game.start();
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Aligning left for JMenuBar

    That seems like a lot of code for simply showing us how you're adding a menu to a JFrame. I suggest boiling your problem down to an SSCCE.

    Have you tried using the alignment methods?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Problem with JMenuBar(Solved)
    By albin1228 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 4th, 2013, 04:08 PM
  2. Menus will not appear in JMenubar
    By mwardjava92 in forum AWT / Java Swing
    Replies: 8
    Last Post: March 19th, 2013, 04:02 PM
  3. Aligning labels
    By pottsiex5 in forum AWT / Java Swing
    Replies: 11
    Last Post: September 25th, 2011, 05:54 PM
  4. aligning help
    By Macgrubber in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2010, 01:52 AM
  5. Aligning Issues in the Output
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 15th, 2010, 05:29 AM

Tags for this Thread