As of now, I have this
And this is my source code for MyFrame1
:
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Color;
import java.awt.Color.*;
import java.awt.Font;
import java.awt.Font.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class Test
{
public static void main(String[] args)
{
new Test();
}
public Test()
{
String line = "";
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e)
{
e.printStackTrace();
}
JMenuBar mBar = new JMenuBar();
//creating new JMenuItem
JMenuItem mHelp = new JMenuItem("Help");
JMenuItem mCredits = new JMenuItem("Credits");
JMenuItem mExit = new JMenuItem("Exit");
/*try
{
BufferedReader br = new BufferedReader(new FileReader("1.txt"));
line = br.readLine();
}
catch(Exception e)
{
e.printStackTrace();
}*/
JLabel jUser = new JLabel("User is: " );
mHelp.setOpaque(false);
mHelp.setForeground(Color.DARK_GRAY);
mHelp.setFont(new Font("Verdana", Font.PLAIN,12));
mCredits.setOpaque(false);
mCredits.setForeground(Color.DARK_GRAY);
mCredits.setFont(new Font("Verdana", Font.PLAIN,12));
mExit.setOpaque(false);
mExit.setForeground(Color.DARK_GRAY);
mExit.setFont(new Font("Verdana", Font.PLAIN,12));
mBar.add(mHelp);
mBar.add(mCredits);
mBar.add(mExit);
mBar.add(jUser);
//mBar.add(line);
JFrame frame = new JFrame("MYFRAME");
frame.setJMenuBar(mBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}
});
}
public class TestPane extends JPanel
{
public TestPane()
{
setBorder(new EmptyBorder(20, 20, 20, 20));
setLayout(new GridLayout(3, 3, 60, 60));
add(makeButton("Account Code"));
add(makeButton("Unit Details"));
add(makeButton("Item Details"));
add(makeButton("Clearing"));
add(makeButton("Search"));
add(makeButton("Exit"));
}
protected JButton makeButton(String text)
{
JButton btn = new JButton(text);
btn.setFont(new Font("Verdana", Font.PLAIN,18));
btn.setMargin(new Insets(30, 30, 30, 30));
btn.setBackground(Color.blue);
btn.setOpaque(true);
btn.setBorderPainted(false);
return btn;
}
}
}
I am still new and still have a small knowledge about Java and GUI. I am still learning about it so I am doing Trial-Error on my program. I tried using UIManager, or UILayout, but still not working for me or I still dont know how to use it. I really want to learn more about GUI and Java, please help me. Any comments, remarks, suggestions are accepted and well-appreciated.
MyFrame1:
As for the output I am aiming for this kind, pls. see next picture.
MyDesireOutput:
Also if you notice there's a bufferedReader, I am practicing to read a "1.txt" with a String, and putting it as label or (still dont know about it) in the menu bar...
You should add your
JMenuItem
s toJMenu
objects and then add yourJMenu
s to yourJMenuBar
.But adding a
JLabel
toJMenuBar
is not a good idea. If you want to have something like you depicted in you question, you may want to add aJPanel
to thenorth
region of your frame, and then add the User label to theFlowLayout.TRAILING
region of that panel:Good Luck
First you must know these
JMenuBar:
JMenu:
JMenuItem:
So add your
JMenuItem
s toJMenu
, later add thisJMenu
toJMenuBar
.Output: