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(); } }