So I'm making a game and It's working quite well, but I just need to have a start menu with 3 buttons. A Play, Instructions and Exit button. The problem is that I want it in a different frame than the game itself, if you press on Start the frame should switch to the game frame. Can someone help me with this?
This is a part of my code:
package frametest;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;
public class FrameTest extends JFrame implements ActionListener {
public JPanel createContentPane() {
//Jpanels and stuff
}
public JPanel createMainMenu() {
//Start menu JPanels and stuff
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Poker Game");
FrameTest demo = new FrameTest();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1080,640);
frame.setVisible(false);
JFrame menu = new JFrame("Poker Game");
menu.setContentPane(demo.createMainMenu());
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu.setSize(1080,640);
menu.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
You must have a setting frame class for your main function and essential for frame and you should have another class for game controlling and a class for menu.
You should get instant of game controller in frame and do the same in controller for menu then you can have separate frame that you want.
please use interfaces to have a clean code.
Related
I'm writing a program where I am supposed to have a title screen for a game, where you can click a 'play' button that opens a different window and simultaneously closes the other one. What I am attempting to do is utilize an ActionListener for a button that makes the current window invisible while simultaneously making a different window visible. I am having a hard time getting this to work and am encountering a lot of syntax and logical errors. I'm quite certain there is a better way to do this, so any pointers will be greatly appreciated. I am using swing. Disclaimer: beginner java level programmer lol :p
My first class includes this (does not include my imports):
public static void main(String[] args) {
//Creating the Frame
MyFrame menuFrame = new MyFrame();
// (here i have code for the frame in my actual program)
}
static class MyFrame extends JFrame {
MyFrame () {
this.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(new JLabel(new ImageIcon("logo.png")));
this.pack();
this.setVisible(true);
new EightOff.returnHomeListener (this);
}
}
}
My other class that has the other frame being opened includes the following button and action listener where I am trying to reference my frame from GameMenu:
public class EightOff
{
private static JButton returnHome = new JButton("Return to Game Menu");
public static class returnHomeListener implements ActionListener {
public returnHomeListener(GameMenu.MyFrame myFrame) {
}
#Override
public void actionPerformed(ActionEvent e)
{
returnHomeListener (JFrame visibleFrame) {
visibleFrame.toSetVisible (true);
}
}
}
}
You should start by having a look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener
The ActionListener should be registered to the button, so if can be notified when some action occurs, for example...
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JButton button = new JButton("Click me");
button.addActionListener(new TestActionListener());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(button);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "I'd prfefer if you did't do that");
}
}
}
In order to make changes to a object, you first need a reference to that object. This is pretty basic, Java 101 kind of stuff and you should have a look at Passing Information to a Method or a Constructor for more details.
You should also have a read of the JavaDocs for JFrame to get a better understanding of what properties and functionality the class provides.
Having a look at How to Make Frames also wouldn't hurt.
I'd recommend having a look at How to use CardLayout instead of trying to hide/show windows, you'll have a better user experience generally.
So I'm making a simple program that jumps from panel to panel and am using an actionlistener Button to make the jump. What kind of method or operation do I use to jump from panel to panel?
I tried to use setVisible(true); under the action listener, but I get just a blanks screen. Tried using setContentPane(differentPanel); but that doesn't work.
ackage Com.conebind.Characters;
import Com.conebind.Tech.TechA16;
import Com.conebind.Overviews.OverviewA16;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Char_A16 extends JFrame {
private JButton combosButton16;
private JButton techButton16;
private JButton overviewButton16;
private JLabel Image16;
private JPanel panel16;
private JPanel panelOverviewA16;
public Char_A16() {
overviewButton16.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
OverviewA16 overview16 = new OverviewA16();
overview16.setVisible(true);
overview16.pack();
overview16.setContentPane(new Char_A16().panelOverviewA16);
}
});
techButton16.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//Todo
}
});
}
private void createUIComponents(){
Image16 = new JLabel(new ImageIcon("Android 16.png"));
}
public static void main (String[] args){
JFrame frame = new JFrame("Android 16");
frame.setContentPane(new Char_A16().panel16);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);}
}
The setContentPane(OverviewA16) doesn't work because there's not an object that defines the panel.
Please check this demo project showing how to use CardLayout with IntelliJ IDEA GUI Designer.
The main form has a method that switches between 2 forms displayed inside it:
public void showPanel(String id) {
final CardLayout cl = (CardLayout) cardPanel.getLayout();
cl.show(cardPanel, id);
}
Both forms are added to the card layout during the main form initialization:
FormOne one = new FormOne();
one.setParentForm(this);
cardPanel.add(one.getPanel(), FORM_ONE);
FormTwo two = new FormTwo();
two.setParentForm(this);
cardPanel.add(two.getPanel(), FORM_TWO);
final CardLayout cl = (CardLayout) cardPanel.getLayout();
cl.show(cardPanel, FORM_ONE);
A reference to the main parent form is passed to these 2 forms using setParentForm() method so that FormOne and FormTwo classes can access the showPanel() method of the MainForm.
In a more basic case you may have a button or some other control that switches the forms
located directly on the MainForm, then you may not need passing the main form reference to the subforms, but it can be still useful depending on your app logic.
Good day everyone! I just got back into programming, and I'm facing some problem with the construction of my code. What I'm trying to do is a menu for a future application that could help me and my friends when we play a certain board game.
So right now, I'm having difficulty to figure out how I should structure my code. I have 3 class planned so far. the Main class, which create a JFrame. a MainFrame class which help the main setup the frame, and finally the third class is MainPanel, which display the proper JPanel on the frame depending on the state of the program.
The plan JPanel classes are:
a menu Panel, with 3 button on it: New, Load and Exit.
a loading Panel, which just display "Loading" while the application load stuff (will be use when someone click New)
a loadBoard Panel, which display the list of created board (when someone click Load)
I'm having 2 issue right now:
Where do I fit the code to monitor the Key event (such as KeyListener on the frame, so independently of where I am at in the program, the program Exit when I press the Esc key.)
How do I make sure the repaint() is handle properly? I have a problem where the Label "Loading" change location when I resize the window.
I know my code ain't construct ideally, that's why I'm posting this thread. I would like to better understand how to setup the code to have a similar result of what I have right now, but well construct.
This is my code:
MAIN Class
package test.project
import java.awt.Dimension;
public class Main
{
public static void main(String[] args)
{
Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
javax.swing.SwingUtilities.invokeLater(() ->
{
MainFrame theWindow = new MainFrame("Test",screenSize);
theWindow.setLocationRelativeTo(null);
theWindow.getContentPane().add(new MenuPanel(theWindow,theWindow.getSize()));
});
}
}
MAINFRAME Class
package test.project;
import java.awt.Dimension;
import javax.swing.JFrame;
public class MainFrame extends JFrame
{
#SuppressWarnings("LeakingThisInConstructor")
public MainFrame(String windowName, Dimension theSize)
{
setTitle(windowName);
setSize(theSize.width/2,theSize.height/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
MAINPANEL Class
package test.project;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class MainPanel extends JPanel
{
JFrame theHolder;
public MainPanel(JFrame holder, Dimension theSize)
{
theHolder = holder;
setSize(theSize);
}
}
class MenuPanel extends MainPanel implements ActionListener
{
JButton newButton = new JButton("NEW");
JButton loadButton = new JButton("LOAD");
JButton exitButton = new JButton("EXIT");
#SuppressWarnings("LeakingThisInConstructor")
public MenuPanel(JFrame holder, Dimension theSize)
{
super(holder, theSize);
setLayout(new GridLayout(0,1));
SetButton(this);
setVisible(true);
theHolder.repaint();
}
private void SetButton(JPanel thePanel)
{
newButton.addActionListener(this);
loadButton.addActionListener(this);
exitButton.addActionListener(this);
thePanel.add(newButton);
thePanel.add(loadButton);
thePanel.add(exitButton);
}
#Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(newButton))
{
theHolder.getContentPane().remove(this);
theHolder.getContentPane().add(new LoadingPanel(theHolder,theHolder.getSize()));
theHolder.repaint();
}
if(e.getSource().equals(loadButton))
{
}
else if(e.getSource().equals(exitButton))
{
System.exit(0);
}
}
}
class LoadingPanel extends MainPanel
{
JLabel loadingLabel = new JLabel("LOADING");
#SuppressWarnings("LeakingThisInConstructor")
public LoadingPanel(JFrame holder, Dimension theSize)
{
super(holder, theSize);
setLayout(new GridLayout(0,1));
SetLabel(this);
setVisible(true);
theHolder.repaint();
}
private void SetLabel(JPanel thePanel)
{
thePanel.add(loadingLabel);
loadingLabel.setSize(thePanel.getSize());
loadingLabel.setHorizontalAlignment(SwingConstants.CENTER);
loadingLabel.setVerticalAlignment(SwingConstants.CENTER);
}
}
I'm developing an app for computer, and i have a JFrame with a lot of JPanel on it, and when i click on a button, i want another JPanel to popup.
Example: When i click on this button
http://i62.tinypic.com/c2fzr.jpg
I want this window to popup
http://i62.tinypic.com/2qi0in7.jpg
I already tried making a popup menu, but i don't want a menu, i want a window, and i can't seen to find out how to do it :(
It's probably easy, but i don't have enough knowledge in java
Any help? thanks guys!
Ok so,for this you will need 2 JFrames. First one is where the buttons and everything is and the second one is the one that will popup. You will have 3 classes: Main, classWhere1stJframeis, ClassWhere2ndJframeis.
This is main:
package proba;
import javax.swing.JFrame;
public class mejn {
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame1 frejm = new Frame1();
frejm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frejm.setVisible(true);
frejm.setSize(250, 300);
}
}
This is Frame1:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame1 extends JFrame {
JFrame Frame = new JFrame();
JButton Button1 = new JButton();
public Frame1()
{
super("The title");
Frame = new JFrame();
Button1 = new JButton();
Frame.add(Button1);
thehandler handler = new thehandler();
Button1.addActionListener(handler);
}
private class thehandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==Button1)
{
Frejm2 frejm = new Frejm2();
frejm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frejm.setVisible(true);
}
}
}
}
This is Frame2:
import javax.swing.JFrame;
public class Frejm2 extends JFrame {
JFrame Frame2 = new JFrame();
public Frejm2()
{
super("Title");
}
}
that is not just a panel you want to pop up that would be considered a whole other frame. I would suggest making a different JFrame class that when the button is clicked instantiates the other frame.
I want to run this code that will create a window with a simple button on it. The program will run in Netbeans on a Mac but the problem is that it does not work. Here is the code below.
import javax.swing.JFrame;
public class Test {
public static JButton button(){
JButton button = new JButton("random button");
}
public static void main(String[] args) {
button();
new JFrame();
}
}
Please help me figure this out soon please. Thank you.
You're not adding the button to anything or displaying the JFrame. Your method returns a JButton object, but you're not doing anything with this object.
Create a JPanel
Add the JButton to the JPanel
Add the JPanel to the JFrame
Display the JFrame by calling setVisible(true)
Most important: Making up code and hoping it will magically work is not a successful heuristic for learning to program. Instead read the Swing tutorials which you can find here.
For example
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MyTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JButton button = new JButton("Button");
JPanel panel = new JPanel();
panel.add(button);
JFrame frame = new JFrame("foo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}