0

I am new to Java, so this question might be obvious.

I have this Initialization code:

frame = new JFrame();
frame.setTitle("Test");
frame.setBounds(100, 100, 512, 468);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

JPanel panel = new MyJPanel();
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
panel.setAlignmentY(Component.TOP_ALIGNMENT);
panel.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.setPreferredSize(new Dimension(0, 0));
panel.setMinimumSize(new Dimension(0, 0));
frame.getContentPane().add(panel, BorderLayout.CENTER);

JMenuBar menuBar = new JMenuBar();
frame.getContentPane().add(menuBar, BorderLayout.NORTH);

JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);

mntmOpenBBinary.setPreferredSize(new Dimension(149, 22));
mnFile.add(mntmOpenBBinary);

JSeparator separator = new JSeparator();
mnFile.add(separator);

JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        System.exit(0);
    }
});
mnFile.add(mntmExit);

MyJPanel is a custom class that extends the JPanel class. Just as a test, it just writes "test" to the screen in the paintComponent method:

public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    g.setColor(Color.BLACK);
    g.setFont(RenderFont);
    g.drawString("TEST", 1, 1);
    }

You can see from the image below that, for some reason, the drawString method is drawing behind the menu. The coordinates I give in drawString, I'd think, would be the coordinates relative to the JPanel window. Also, the JPanel is "filling" the entire space of the JFrame. I'd prefer that my MyJFrame be only 100x100, but it seems to always want to auto fill the JFrame. How can I solve these 2 issues?

enter image description here

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Icemanind
  • 47,519
  • 50
  • 171
  • 296

1 Answers1

4

The text is hidden by the menu bar because last parameter of drawString() is the text baseline, and not the upper bound: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#drawString(java.lang.String,%20int,%20int)

So you need to use something like:

 g.drawString("TEST", 1, 50);

Or better, use Font.getStringBounds() to compute your text height:

 Rectangle2D textBounds = g.getFont().getStringBounds("TEST", (((Graphics2D) g).getFontRenderContext());

And to avoid having your panel taking all Frame space, replace:

frame.getContentPane().add(panel, BorderLayout.CENTER);

with:

JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(panel, BorderLayout.NORTH);
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);

Although, you should not need this:

panel.setAlignmentY(Component.TOP_ALIGNMENT);
panel.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.setPreferredSize(new Dimension(0, 0));
panel.setMinimumSize(new Dimension(0, 0));
xav
  • 5,452
  • 7
  • 48
  • 57
  • Thank you for this. Do you have an answer to my second part? How to make the JPanel not autofill the JFrame? – Icemanind Apr 13 '14 at 22:44
  • @icemanind: you're adding it `BorderLayout.CENTER` so it's going to fill, unless you use a different layout manager or different position in the BorderLayout. – Hovercraft Full Of Eels Apr 13 '14 at 22:45
  • @HovercraftFullOfEels -- What kind of layout manager should I use if I want my JPanel to be "free form"? – Icemanind Apr 13 '14 at 22:47
  • @icemanind: ok, I've updated by answer (use `BorderLayout.NORTH`) – xav Apr 13 '14 at 22:48
  • @HovercraftFullOfEels -- Yea, so I could move my JPanel to say 100,100 with a size of 50,50, for example. Right now, I can't seem to move it. It just stays locked in the upper left corner, taking up the same size as the JFrame – Icemanind Apr 13 '14 at 22:53
  • @icemanind: if you're like most, your gut instinct will be to use a `null` layout and calling `setBounds(...)` on your components to place them with absolute positioning, but I'm going suggest that you not do this as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's. – Hovercraft Full Of Eels Apr 13 '14 at 22:56
  • 1
    Use layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Apr 14 '14 at 00:54