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);
}}
Related
I have problem with JOptionPane. I have created contructor in which I defined JOptionPane. I need to create object of that class in another class where I created JFrame. The problem is that JOptionPane opens before JFrame does and I want it to be just the opposite.
Every time i create an object of the class with JOptionPane it shows up.
I simply want to JOptionPane show and change "delay" only when I click "play" on my JMenu.
public class Gameplay extends Paint implements KeyListener, ActionListener {
private Timer timer;
private int q = 0;
private int delay;
public Gameplay() {
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay=Integer.parseInt(JOptionPane.showInputDialog("set Speed")), this);
timer.start();
}
public class PlayGame 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);
}
public void beVisible() {
f2.setVisible(true);
}
public class Menu {
PlayGame playGame = new PlayGame();
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();
JMenu fileMenu = new JMenu("File");
JMenuItem play = new JMenuItem("Play");
play.addActionListener((ActionEvent event) -> {
playGame.openGame();
playGame.beVisible();
});
I am trying to develop a Java app with more JPanels. For now, I have all JPanels in the main constructor Graphics(), and in there, I am adding them all to a JFrame. I want to break the code, and move each container into its own class, but, when I am running that code, it goes over and over again, never stops generating JFrames. Any ideas on how to break my code?
Here is the code:
public Graphics() {
//====== GENERAL=========//
frame = new JFrame();
frame.setSize(1300, 700);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setVisible(true);
//====== GENERAL=========//
//====== MENU BAR=========//
JMenuBar menuBar = new JMenuBar();
menuBar.setBounds(0, 0, 1284, 21);
JMenu menuFile = new JMenu("File");
menuBar.add(menuFile);
JMenuItem itemMain = new JMenuItem(new AbstractAction("Main"){
public void actionPerformed(ActionEvent e) {
mainPage.setVisible(true);
}
});
menuFile.add(itemMain);
JMenuItem itemLog = new JMenuItem(new AbstractAction("Log off"){
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
loginPage.setVisible(true);
mainPage.setVisible(false);
passField.setText("");
lblWrongPassword.setVisible(false);
}
});
menuFile.add(itemLog);
JMenu menuHelp = new JMenu("Help");
menuBar.add(menuHelp);
JMenuItem menuHelpInfo = new JMenuItem(new AbstractAction("Directions"){
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, helpInfo);
}
});
menuHelp.add(menuHelpInfo);
frame.setJMenuBar(menuBar);
//====== MENU BAR=========//
//====== MAIN PAGE=========//
mainPage = new JPanel();
mainPage.setBounds(0, 0, 1284, 662);
frame.getContentPane().add(mainPage);
mainPage.setLayout(null);
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.
as the title states, i need to know how to determine if a menu item was clicked or not, then run the function associated. for instance i have a JMenu with a JMenuItem "exit", which when clicked should run a close form method.
import javax.swing.*;
public class selector_form extends JFrame {
/**
*
*/
private static final long serialVersionUID = -5963842156289770842L;
public selector_form(String name)
{
super.setTitle(name);
setupComponents(this);
super.setVisible(true);
}
private void setupComponents(JFrame frame)
{
JMenuItem file_items = new JMenuItem("Exit");
JMenuItem config_items = new JMenuItem("Preferences");
JMenuItem[] machine_items = {new JMenuItem("Refresh"),
new JMenuItem("Add Dynamically"), new JMenuItem("Remove Dynamically")
};
JMenuItem[] emulator_items = {new JMenuItem("Start Emulator"),
new JMenuItem("Stop Emulator"), new JMenuItem("Pause Emulator"),
new JMenuItem("Reset Emulator"), new JMenuItem("Crash Emulator")
};
JMenuItem[] memory_items = {new JMenuItem("View Emulator Memory"),
new JMenuItem("System Cheats"), new JMenuItem("Dump Emulator Memory"),
new JMenuItem("Edit Specific Address"),
new JMenuItem("Show Allocations"), new JMenuItem("Allocate Memory"),
new JMenuItem("DeAllocate Memory")
};
JMenuItem[] cpu_items = {new JMenuItem("Show Host Specs"),
new JMenuItem("Show Emulator Specs"),
new JMenuItem("Enable HyperThreadding")
};
JMenuItem[] about_items = {new JMenuItem("Help Contents"),
new JMenuItem("About")
};
frame.setSize(800, 600);
JMenuBar jmb = new JMenuBar();
JMenu jm_a = new JMenu("File");
JMenu jm_c = new JMenu("Config");
JMenu jm_d = new JMenu("Machines");
JMenu jm_e = new JMenu("Emulator");
JMenu jm_f = new JMenu("Memory");
JMenu jm_g = new JMenu("CPU");
JMenu jm_h = new JMenu("About");
jm_a.add(file_items);
jm_c.add(config_items);
for(JMenuItem item : machine_items)
jm_d.add(item);
for(JMenuItem item : emulator_items)
jm_e.add(item);
for(JMenuItem item : memory_items)
jm_f.add(item);
for(JMenuItem item : cpu_items)
jm_g.add(item);
for(JMenuItem item : about_items)
jm_h.add(item);
jmb.add(jm_a);
jmb.add(jm_c);
jmb.add(jm_d);
jmb.add(jm_e);
jmb.add(jm_f);
jmb.add(jm_g);
jmb.add(jm_h);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setJMenuBar(jmb);
}
}
Just add an ActionListener to it.
menu_item.addActionListener(this);
and implement the ActionListener interface.
You can do this with an ActionListener. You can create your own ActionListener class.
class MenuActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Selected: " + e.getActionCommand());
}
}
Than add it to the JMenuItem.
JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.addActionListener(new MenuActionListener());
thanks for all the answers. here is what i did.
i used totymedli's example and wrote a class called MenuActionHelper, which calls a MenuEventHandler speciality class, which passes an ID to a function which calls the function needed.
updated selector-form:
package application;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class selector_form extends JFrame {
public static MenuEventHandler mehandler;
/**
*
*/
private static final long serialVersionUID = -5963842156289770842L;
public selector_form(String name)
{
super.setTitle(name);
setupComponents(this);
super.setVisible(true);
mehandler = new MenuEventHandler(this);
}
private void setupComponents(JFrame frame)
{
JMenuItem file_items = new JMenuItem("Exit");
JMenuItem config_items = new JMenuItem("Preferences");
JMenuItem[] machine_items = {new JMenuItem("Refresh"),
new JMenuItem("Add Dynamically"), new JMenuItem("Remove Dynamically")
};
JMenuItem[] emulator_items = {new JMenuItem("Start Emulator"),
new JMenuItem("Stop Emulator"), new JMenuItem("Pause Emulator"),
new JMenuItem("Reset Emulator"), new JMenuItem("Crash Emulator")
};
JMenuItem[] memory_items = {new JMenuItem("View Emulator Memory"),
new JMenuItem("System Cheats"), new JMenuItem("Dump Emulator Memory"),
new JMenuItem("Edit Specific Address"),
new JMenuItem("Show Allocations"), new JMenuItem("Allocate Memory"),
new JMenuItem("DeAllocate Memory")
};
JMenuItem[] cpu_items = {new JMenuItem("Show Host Specs"),
new JMenuItem("Show Emulator Specs"),
new JMenuItem("Enable HyperThreadding"),
new JMenuItem("Show Disassembly in real time")
};
JMenuItem[] about_items = {new JMenuItem("Help Contents"),
new JMenuItem("About")
};
frame.setSize(800, 600);
JMenuBar jmb = new JMenuBar();
JMenu jm_a = new JMenu("File");
JMenu jm_c = new JMenu("Config");
JMenu jm_d = new JMenu("Machines");
JMenu jm_e = new JMenu("Emulator");
JMenu jm_f = new JMenu("Memory");
JMenu jm_g = new JMenu("CPU");
JMenu jm_h = new JMenu("About");
jm_a.add(file_items);
jm_c.add(config_items);
for(JMenuItem item : machine_items)
jm_d.add(item);
for(JMenuItem item : emulator_items)
jm_e.add(item);
for(JMenuItem item : memory_items)
jm_f.add(item);
for(JMenuItem item : cpu_items)
jm_g.add(item);
for(JMenuItem item : about_items)
jm_h.add(item);
jmb.add(jm_a);
jmb.add(jm_c);
jmb.add(jm_d);
jmb.add(jm_e);
jmb.add(jm_f);
jmb.add(jm_g);
jmb.add(jm_h);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setJMenuBar(jmb);
//Menu Action Helpers
file_items.addActionListener(new MenuActionHelper(1));
}
}
/* EFFECTIVE ACTIONLISTENER
*
* passes an integral argument to MeunEventHandler
*/
class MenuActionHelper implements ActionListener
{
public static int digitalIdentifier;
public MenuActionHelper(int i)
{
MenuActionHelper.digitalIdentifier = i;
}
#Override
public void actionPerformed(ActionEvent arg0)
{
try
{
if(!(selector_form.mehandler.parseEvent(MenuActionHelper.digitalIdentifier)))
{
throw new Exception("Invalid Menu Event ID Parsed!");
}
else return;
}catch(Exception e)
{
e.printStackTrace();
}
}
}
and the new MenuEventHandler class:
package application;
import javax.swing.JFrame;
public class MenuEventHandler {
/* have a static number which is the max an id can be */
public static final int maxActionId = 1000; //good large number
public static JFrame frame;
public MenuEventHandler(JFrame frame) //import jframe functionality
{
MenuEventHandler.frame = frame;
}
public boolean parseEvent(int i)
{
boolean success = false;
switch(i)
{
case 1:
{
MenuEventHandler.frame.dispose();
success = true;
}
break;
}
return success;
}
}
now if someone clicks on the exit MenuItem, the form closes and the application ends.
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.