I have a small program already started with multiple menu items.
When an item is clicked, I would like for something to show on the frame, and when another item is clicked, the first disappears and the second appears.
I can print to console, but cannot seem to print to frame.
Any suggestions would be appreciated:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class melco extends JFrame{
public static void main(String[] args){
JFrame frame = new JFrame("Salesman Resources");
frame.setVisible(true);
frame.setSize(1000,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu orders = new JMenu("Orders");
menubar.add(orders);
JMenuItem bookedorders = new JMenuItem("Booked Orders");
orders.add(bookedorders);
JMenuItem backorders = new JMenuItem("BackOrders");
orders.add(backorders);
JMenu customers = new JMenu("Customers");
menubar.add(customers);
JMenuItem customersales = new JMenuItem("Customer Sales");
customers.add(customersales);
JMenuItem customeritems = new JMenuItem("Customer Items");
customers.add(customeritems);
JMenuItem customerprices = new JMenuItem("Customer Prices");
customers.add(customerprices);
JMenuItem customerlistings = new JMenuItem("Customer Listings");
customers.add(customerlistings);
JMenu inv = new JMenu("INV");
menubar.add(inv);
JMenuItem surplusinv = new JMenuItem("Surplus Inv");
inv.add(surplusinv);
JMenuItem stockinv = new JMenuItem("Stock Inv");
inv.add(stockinv);
JMenu search = new JMenu("Searh");
menubar.add(search);
JMenuItem itemsearch = new JMenuItem("Item Search");
search.add(itemsearch);
JMenuItem customersearch = new JMenuItem("Customer Search");
search.add(customersearch);
JMenu menulostsales = new JMenu("Lost Sales");
menubar.add(menulostsales);
JMenuItem lostsales = new JMenuItem("Lost Sales");
menulostsales.add(lostsales);
JMenu menumarginadvisor = new JMenu("Margin Advisor");
menubar.add(menumarginadvisor);
JMenuItem marginadvisor = new JMenuItem("Margin Advisor");
menumarginadvisor.add(marginadvisor);
JMenu menumakeandhold = new JMenu("Make and Hold");
menubar.add(menumakeandhold);
JMenuItem makeandhold = new JMenuItem("Make and Hold");
menumakeandhold.add(makeandhold);
makeandhold.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Make and Hold is pressed");
System.out.println("Hello World");
// This is Make and Hold Area
}
}
);
class exitaction implements ActionListener {
public void actionPerformed (ActionEvent e){
System.exit(0);
}
}
exit.addActionListener(new exitaction());
}
}
You cannot print to frame because a frame doesn't have a print method. If you want to add the text to the frame directly you should draw it. That requires to override paint method by the technique below
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class melco extends JFrame{
String str;
int x = 100, y = 100;
public melco(String title){
super(title);
}
void print(String s){
str = s;
repaint();
}
public void paint(Graphics g){
super.paint(g);
if (str != null)
g.drawString(str, x, y);
}
public static void main(String[] args){
final melco frame = new melco("Salesman Resources");
frame.setVisible(true);
frame.setSize(1000,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu orders = new JMenu("Orders");
menubar.add(orders);
JMenuItem bookedorders = new JMenuItem("Booked Orders");
orders.add(bookedorders);
JMenuItem backorders = new JMenuItem("BackOrders");
orders.add(backorders);
JMenu customers = new JMenu("Customers");
menubar.add(customers);
JMenuItem customersales = new JMenuItem("Customer Sales");
customers.add(customersales);
JMenuItem customeritems = new JMenuItem("Customer Items");
customers.add(customeritems);
JMenuItem customerprices = new JMenuItem("Customer Prices");
customers.add(customerprices);
JMenuItem customerlistings = new JMenuItem("Customer Listings");
customers.add(customerlistings);
JMenu inv = new JMenu("INV");
menubar.add(inv);
JMenuItem surplusinv = new JMenuItem("Surplus Inv");
inv.add(surplusinv);
JMenuItem stockinv = new JMenuItem("Stock Inv");
inv.add(stockinv);
JMenu search = new JMenu("Searh");
menubar.add(search);
JMenuItem itemsearch = new JMenuItem("Item Search");
search.add(itemsearch);
JMenuItem customersearch = new JMenuItem("Customer Search");
search.add(customersearch);
JMenu menulostsales = new JMenu("Lost Sales");
menubar.add(menulostsales);
JMenuItem lostsales = new JMenuItem("Lost Sales");
menulostsales.add(lostsales);
JMenu menumarginadvisor = new JMenu("Margin Advisor");
menubar.add(menumarginadvisor);
JMenuItem marginadvisor = new JMenuItem("Margin Advisor");
menumarginadvisor.add(marginadvisor);
JMenu menumakeandhold = new JMenu("Make and Hold");
menubar.add(menumakeandhold);
JMenuItem makeandhold = new JMenuItem("Make and Hold");
menumakeandhold.add(makeandhold);
makeandhold.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Make and Hold is pressed");
frame.print("Make and Hold is pressed");
System.out.println("Hello World");
frame.print("Hello World");
// This is Make and Hold Area
}
}
);
class exitaction implements ActionListener {
public void actionPerformed (ActionEvent e){
System.exit(0);
}
}
exit.addActionListener(new exitaction());
}
}
I can print to console, but cannot seem to print to frame.
What do you mean exactly?
If you want to set a title on the Frame you can use setTitle:
frame.setTitle("a title");
Otherwise, if you want to add some text you have at least to add some component to your JFrame.
Have a look at JLabel:
JLabel label = new JLabel();
frame.getContentPane().add(label);
when you need to change the text:
label.setText("some text");
Don't ever use the EDT (Event Dispatcher Thread) for other things apart from letting it handle the GUI. main() method in Java Gui exits after scheduling the construction of GUI to the Event Dispatcher Thread. So its the EDT which handles it.
Eg:
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
myframe.setVisible(true);
}
}
}
You need something like JLable for adding things like Title.
You can add a JPanel to the JFrame and then add a JTextField to it.
You don't print to a frame. Maybe you want to add a JTextArea and append data to it?
Okay, so you need something to display on the frame, something like a JLabel??
frame.setLayout(new BorderLayout());
JLabel lblMessage = new JLabel(); // You'll probably need to declare this as final
frame.add(lblMessage);
Then in your action listener could do something like
public void actionPerformed (ActionEvent e){
label.setText("This is a message from the menu item");
}
Why don't you add a MouseListener on the JMenuItems? You can hold the most recent "message" reference as a member variable and remove it from the JFrame when another MouseListener is fired.
Related
I have two files/class, the main class and the menubar class.
This is my main class:
public static void main(String[] args){
journalFrame = new JFrame("Journal Viewer");
journalFrame.setJMenuBar(menuBar = new JMenuBar());
journalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
journalFrame.setPreferredSize(new Dimension(500, 800));
journalFrame.pack();
journalFrame.setLocationRelativeTo(null);
journalFrame.setVisible(true);
}
This is my menubar class:
MenuBar(){
mMbar = new JMenuBar();
fileMenu = new JMenu("File");
aboutMenu = new JMenu("About");
openItem = new JMenuItem("Open");
openItem.addActionListener((ActionListener) this);
fileMenu.add(openItem);
exitItem = new JMenuItem("Exit");
exitItem.addActionListener((ActionListener) this);
fileMenu.add(exitItem);
mMbar.add(fileMenu);
}
My output is just a plain window with no menubar. How do I make it appear?
You are not creating an object of your class, you're simply creating a JMenuBar object when you call menuBar = new JMenuBar(). Also if this is your full code it's got mutliple issues and honestly I can't be bothered to figure out why I'm getting compiler errors on dots and semicolons. Note that setJMenuBar(JMenuBar menuBar) can only be called with a JMenuBar object as parameter. So either Override that method to have it behave the way you want, or try something else.
I will join #AndrewThompson in his opinion that it would make a lot more sense to just create the menubar within the GUI class.
Here's what your code could (or maybe should) look like:
public class Main
{
private static JFrame journalFrame;
private static JMenuBar menuBar;
public static void main(String[] args)
{
journalFrame = new JFrame("Journal Viewer");
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu aboutMenu = new JMenu("About");
JMenuItem openItem = new JMenuItem("Open");
openItem.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// TODO: Your open action
}
});
fileMenu.add(openItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// TODO: Your exit action
}
});
fileMenu.add(exitItem);
menuBar.add(fileMenu);
menuBar.add(aboutMenu);
journalFrame.setJMenuBar(menuBar);
journalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
journalFrame.setPreferredSize(new Dimension(500, 800));
journalFrame.pack();
journalFrame.setLocationRelativeTo(null);
journalFrame.setVisible(true);
}
}
I am building a simple snake game, and I want to change speed (variable "delay") of snake in game menu (JMenuBar) before game starts.
I have a problem with changing this variable. I can't change its value in my Menu, it's always equal to set value (100). I tried many things but none of them works. I'm almost done. Here's my code:
package Snake;
public class Menu {
PlayGame playGame = new PlayGame();
Help help = new Help();
JFrame menu = new JFrame();
void create() throws IOException {
createMenuBar();
menu.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("logo.png")))));
menu.setTitle("Menu");
menu.setVisible(true);
menu.setSize(355, 400);
menu.setResizable(false);
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createMenuBar() {
JMenuBar menubar = new JMenuBar();
ImageIcon iconPlay = new ImageIcon("downmouth.png");
ImageIcon iconHelp = new ImageIcon("help.png");
ImageIcon iconAbout = new ImageIcon("about.png");
ImageIcon iconExit = new ImageIcon("exit.png");
JMenu fileMenu = new JMenu("File");
JMenu settingsMenu = new JMenu("Settings");
JMenu helpMenu = new JMenu("Help");
JMenu speedMenu = new JMenu("Speed");
JMenu boardMenu = new JMenu("Board");
JMenuItem faster = new JMenuItem("Faster");
JMenuItem normal = new JMenuItem("Normal");
JMenuItem slower = new JMenuItem("Slower");
JMenuItem theSlowest = new JMenuItem("The Slowest");
JMenuItem noWall = new JMenuItem("Without the wall");
JMenuItem wall = new JMenuItem("With wall");
JMenuItem play = new JMenuItem("Play", iconPlay);
play.addActionListener((ActionEvent event) -> {
playGame.openGame();
playGame.beVisible();
});
JMenuItem exit = new JMenuItem("Exit", iconExit);
exit.addActionListener((ActionEvent event) -> {
System.exit(0);
});
JMenuItem instructions = new JMenuItem("Instructions", iconHelp);
instructions.addActionListener((ActionEvent event) -> {
help.instructions();
});
JMenuItem about = new JMenuItem("About", iconAbout);
about.addActionListener((ActionEvent event) -> {
help.about();
});
JMenuItem theFastest = new JMenuItem("The Fastest");
theFastest.addActionListener((ActionEvent event) -> {
// ???????
});
// and rest of speed variables...
speedMenu.add(theFastest);
speedMenu.addSeparator();
speedMenu.add(faster);
speedMenu.addSeparator();
speedMenu.add(normal);
speedMenu.addSeparator();
speedMenu.add(slower);
speedMenu.addSeparator();
speedMenu.add(theSlowest);
boardMenu.add(noWall);
boardMenu.addSeparator();
boardMenu.add(wall);
fileMenu.add(play);
fileMenu.addSeparator();
fileMenu.add(exit);
settingsMenu.add(speedMenu);
settingsMenu.addSeparator();
settingsMenu.add(boardMenu);
helpMenu.add(instructions);
helpMenu.addSeparator();
helpMenu.add(about);
menubar.add(fileMenu);
menubar.add(settingsMenu);
menubar.add(helpMenu);
menu.setJMenuBar(menubar);
}}
package Snake;
public class Gameplay extends Paint implements KeyListener, ActionListener {
private Timer timer;
private int q = 0;
private int delay = 100;
public Gameplay() {
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);//????????
timer.start();
}
In this class (Gameplay) there's more code which needs timer.
package Snake;
public class PlayGame extends Gameplay implements IbeVisible {
JFrame f2 = new JFrame("Snake");
Gameplay gameplay = new Gameplay();
public void openGame() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
f2.setSize(900, 700);
f2.setResizable(false);
f2.setLocation(dim.width / 2 - f2.getWidth() / 2, dim.height / 2 - f2.getHeight() / 2);
f2.add(gameplay);
}
#Override
public void beVisible() {
f2.setVisible(true);
}}
I'm trying to make a menu with submenus. For some reason my menu will not show up and I can't figure out why. JFrame appears but the JMenuBar does not. Can someone help me figure out what the problem is?
public class AWorldPanel extends JPanel {
/** Declaring all the menu items within the GUI **/
private JMenuItem Fileitem1 = new JMenuItem("New configuration");
private JMenuItem Fileitem2 = new JMenuItem("Open configuration file ");
private JMenuItem Fileitem3 = new JMenuItem("Save");
private JMenuItem Fileitem4 = new JMenuItem("Save As");
private JMenuItem Fileitem5 = new JMenuItem("Exit");
private JMenuItem Viewitem1 = new JMenuItem("Display configuration");
private JMenuItem Viewitem2 = new JMenuItem("Edit configuration");
private JMenuItem Viewitem3 = new JMenuItem("Info about Bugs");
private JMenuItem Viewitem4 = new JMenuItem("Info about Map");
private JMenuItem Edititem1 = new JMenuItem("Remove");
private JMenuItem Edititem2 = new JMenuItem("Add");
private JMenuItem Simulationitem1 = new JMenuItem("Simulation");
private JMenuItem Helpitem1 = new JMenuItem("Info about application");
private JMenuItem Helpitem2 = new JMenuItem("Info about author");
JLabel theLabel;
JPanel thePanel;
JButton Run, Pause, Reset;
JFrame GUI = new JFrame("Graphical User Interface");
public void AWorldPanel() {
// Create the container
JFrame GUI = new JFrame("Graphical User Interface");
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/** Creating the menu **/
JMenuBar menubar = new JMenuBar();
JMenu File = new JMenu("File");
JMenu View = new JMenu("View");
JMenu Edit = new JMenu("Edit");
JMenu Simulation = new JMenu("Simulation");
JMenu Help = new JMenu("Help");
/** sub menus **/
menubar.add(File);
File.add(Fileitem1);
File.add(Fileitem2);
File.add(Fileitem3);
File.add(Fileitem4);
File.add(Fileitem5);
menubar.add(View);
View.add(Viewitem1);
View.add(Viewitem2);
View.add(Viewitem3);
View.add(Viewitem4);
menubar.add(Edit);
Edit.add(Edititem1);
Edit.add(Edititem2);
menubar.add(Simulation);
Simulation.add(Simulationitem1);
menubar.add(Help);
Help.add(Helpitem1);
Help.add(Helpitem2);
}
private static void createAndShowGUI() {
AWorldPanel newworld = new AWorldPanel();
newworld.GUI.pack();
newworld.GUI.setVisible(true);
newworld.GUI.setLocation(300, 100);
newworld.GUI.setSize(500, 500);
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
You create a menu, but don't set it to the frame.
Use this:
GUI.setMenuBar(menubar);
after you create it.
Java is complaining!
cannot find symbol
symbol : constructor Bar()
location: class Bar
JPanel panel = new Bar();
^
QUESTION: Why am I getting this error?...everything seems to be correct.
this is the coding:
public class JFrameWithPanel
{
public static void main(String[] args)
{
JPanel panel = new Bar();
}
}
Bar( ) is
public class Bar extends JPanel
{
public Bar(final JFrame frame)
{
super(new BorderLayout());
String[] tests = { "A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package" };
JComboBox comboBox = new JComboBox(tests);
TextArea text = new TextArea(5, 10);
add(new JLabel("Welcome to the CIT Test Program "));
add(new JLabel("Please select which Test Package from the list below."));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newMenu = new JMenuItem("New (Ctrl+N)");
JMenuItem openMenu = new JMenuItem("Open (Ctrl+O)");
JMenuItem saveMenu = new JMenuItem("Save (Ctrl+S)");
JMenuItem exitMenu = new JMenuItem("Exit (Ctrl+W)");
JMenuItem cutMenu = new JMenuItem("Cut (Ctrl+X)");
JMenuItem copyMenu = new JMenuItem("Copy (Ctrl+C)");
JMenuItem pasteMenu = new JMenuItem("Paste (Ctrl+V)");
JMenuItem infoMenu = new JMenuItem("Help (Ctrl+H)");
fileMenu.add(newMenu);
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
fileMenu.add(exitMenu);
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
helpMenu.add(infoMenu);
this.add(comboBox, BorderLayout.NORTH);
this.add(text, BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
add(new JButton("Select")
{
{
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
JOptionPane.showMessageDialog(frame, "IT WORKS!");
}
});
}
});
}
}
The problem is that you have a constructor that expects a JFrame:
public Bar(final JFrame frame)
but you care calling it with no arguments:
JPanel panel = new Bar();
You need to pass Bar an instance of a JFrame.
You are calling the Bar() constructor, but you do not have a no arguments constructor. You need to pass the JFrame argument.
In addition to TofuBeer answer - consider using an IDE like eclipse or netbeans (just to name two examples). Those IDE's will show errors of this kind already on typing the code.
Im not sure how to reference to JPanel when it was declared like this.
This is the coding of the entire program: Everything works but the layout is not how I want it. adding BorderLayouts to it doesnt seem to work.
class FrameDemo
{
public static void main(String[] args)
{
final JFrame frame = new JFrame("CIT Test Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(350, 250));
frame.add(new JPanel()
{{
String[] tests = {"A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package"};
JComboBox comboBox = new JComboBox(tests);
TextArea text = new TextArea(5,10);
add(new JLabel("Welcome to the CIT Test Program "));
add(new JLabel("Please select which Test Package from the list below."));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newMenu = new JMenuItem("New (Ctrl+N)");
JMenuItem openMenu = new JMenuItem("Open (Ctrl+O)");
JMenuItem saveMenu = new JMenuItem("Save (Ctrl+S)");
JMenuItem exitMenu = new JMenuItem("Exit (Ctrl+W)");
JMenuItem cutMenu = new JMenuItem("Cut (Ctrl+X)");
JMenuItem copyMenu = new JMenuItem("Copy (Ctrl+C)");
JMenuItem pasteMenu = new JMenuItem("Paste (Ctrl+V)");
JMenuItem infoMenu = new JMenuItem("Help (Ctrl+H)");
fileMenu.add(newMenu);
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
fileMenu.add(exitMenu);
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
helpMenu.add(infoMenu);
this.add(comboBox, BorderLayout.NORTH);
this.add(text, BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
add(new JButton("Select")
{{
addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e )
{
frame.dispose();
JOptionPane.showMessageDialog(frame,"IT WORKS!");
}
});
}});
}});
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
Not sure how to reference to JPanel to use BorderLayout.
How would I go about doing this?
If you add a panel to a JFrame (using add() as you are doing here) , you can assume that it is being added as the contentPanel. However, it is much better to be explicit. Instead of this:
frame.add(new JPanel()
{}
use this:
JPanel panel = new JPanel(new BorderLayout());
// add your stuff to the panel;
frame.add(panel);
EDIT:
after looking at your edit, what is clear is that you are initializing an anonymous class. This is not generally bad practice, but here you are putting a lot of initialization code. The code you are putting in is in a double-braced block, which in essence puts it into a static initializer. It seems like a normal anonymous class, but it really isn't. With that much code, it deserves its own class. If you went so far as to code a new class, you should be fine. I would suggest that you then define it as an extension of JPanel and in your own constructor pass a new BorderLayout() to the super.
EDIT 2:
if you create a brand new file/class named Bar and you coded it like this:
public class Bar extends JPanel {
public Bar(final JFrame frame) {
String[] tests = { "A+ Certification", "Network+ Certification",
"Security+ Certification", "CIT Full Test Package" };
JComboBox comboBox = new JComboBox(tests);
TextArea text = new TextArea(5, 10);
add(new JLabel("Welcome to the CIT Test Program "));
add(new JLabel("Please select which Test Package from the list below."));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newMenu = new JMenuItem("New (Ctrl+N)");
JMenuItem openMenu = new JMenuItem("Open (Ctrl+O)");
JMenuItem saveMenu = new JMenuItem("Save (Ctrl+S)");
JMenuItem exitMenu = new JMenuItem("Exit (Ctrl+W)");
JMenuItem cutMenu = new JMenuItem("Cut (Ctrl+X)");
JMenuItem copyMenu = new JMenuItem("Copy (Ctrl+C)");
JMenuItem pasteMenu = new JMenuItem("Paste (Ctrl+V)");
JMenuItem infoMenu = new JMenuItem("Help (Ctrl+H)");
fileMenu.add(newMenu);
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
fileMenu.add(exitMenu);
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
helpMenu.add(infoMenu);
this.add(comboBox, BorderLayout.NORTH);
this.add(text, BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
add(new JButton("Select") {
{
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
JOptionPane.showMessageDialog(frame, "IT WORKS!");
}
});
}
});
}
All you would need to do to use it would be to call
JPanel panel = new Bar(frame);
however, the goal here is to use a BorderLayout, so I would suggest that you put this call in to start:
public Bar(final JFrame frame) {
super(new BorderLayout());
.... everything else
}
On top of all answers already given... Your program has fundamental flaw. All manipulation with Swing components has to be done on EDT thread. So your code should be slightly different
class FrameDemo
{
public static void main(String[] args)
{
SwingUtilities.invokeLater( new Runnable() {
void run() {
/// your code here
}
});
}
}
Otherwise what happens is unpredictable. You can read more about it at http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html
Adding BorderLayout to it doesn't seem to work.
The default layout of a new JPanel is FlowLayout, as can be seen by resizing the frame. To see the difference, replace
frame.add(new JPanel()
with
frame.add(new JPanel(new BorderLayout())
As #akf suggests, lengthy static initialization using double braces can be obscure.