I'm just starting out with java and I was working on a new GUI. I made a method that makes my JFrame, sets its default location, close operation, background colour etc.
I made a separate method for this to keep it out of the main code for the sake of tidiness. I already know how to solve this method if I just put all of those things in the main method.
This is the code:
public class Main {
public static void makeWindow(){
JFrame mainWindow = new JFrame();
mainWindow.setVisible(true);
mainWindow.setPreferredSize(new Dimension(400,400));
mainWindow.pack();
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setLocationRelativeTo(null);
}
public static void main(String args[]){
makeWindow();
JPanel mainPanel = new JPanel();
mainWindow.add(mainPanel);
}
}
I get an error in my main method saying that "mainWindow" can't be resolved. Why is this? is it because I made the mainWindow object in the other method? Is there a way to resolve this issue without putting everything from the makeWindow() method into the main method?
That won't work because mainWindow only exists in the context of your makeWindow method. You can make it a static field instead:
public class Main {
private static JFrame mainWindow; // declare it here so it can be seen from your main method.
public static void makeWindow(){
mainWindow = new JFrame();
mainWindow.setVisible(true);
mainWindow.setPreferredSize(new Dimension(400,400));
mainWindow.pack();
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setLocationRelativeTo(null);
}
public static void main(String args[]){
makeWindow();
JPanel mainPanel = new JPanel();
mainWindow.add(mainPanel);
}
}
EDIT
As #Xing commented (credits shared), you can return the mainWindow from your makeWindow method:
public class Main {
public static JFrame makeWindow(){
JFrame mainWindow = new JFrame();
mainWindow.setVisible(true);
mainWindow.setPreferredSize(new Dimension(400,400));
mainWindow.pack();
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setLocationRelativeTo(null);
return mainWindow;
}
public static void main(String args[]){
JFrame mainWindow = makeWindow();
JPanel mainPanel = new JPanel();
mainWindow.add(mainPanel);
}
}
create mainWindow instance/ reference globally
private static JFrame mainWindow;
public static void makeWindow(){
mainWindow = new JFrame();
}
So if someone would take a look at the code below and give me hand I would owe you my life. So here's the issue, obviously I can get this to work if I were to put playerCreationSelectionin its own class, my questions is getting it to work, inside class superClass I cannot for the life of me move things around to make it work. Any help would be great, thanks everyone!
forgot to actually put what goes wrong! So what will happen is it says playerCreationSelection is not a symbol
public class superClass
{
public static void main(String[] args)
{
playerCreationSeletion gui = new playerCreationSeletion();
}
public class playerCreateSelection extends JFrame implements ActionListener
{
//create label
public JLabel playerCreatedLabel;
public void playerCreationSeletion()
{
setSize(WIDTH,HEIGHT);
WindowDestroyer listener = new WindowDestroyer();
addWindowListener(listener);
Container contentPane = getContentPane();
contentPane.setBackground(Color.DARK_GRAY);
contentPane.setLayout(new FlowLayout());
//create button
JButton playerCreationButton = new JButton("Create New Player");
playerCreationButton.addActionListener(this);
contentPane.add(playerCreationButton);
//create label
playerCreatedLabel = new JLabel("Welcome New Player!");
playerCreatedLabel.setVisible(false);
}
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
Container contentPane = getContentPane();
if(actionCommand.equals("Create New Player"))
{
contentPane.setBackground(Color.LIGHT_GRAY);
playerCreatedLabel.setVisible(true);
}
}
}
}
Well, you have a typo "playerCreationSeletion()". Also, You need to call an inner class constructor like this and use setVisible and setSize.
public static void main(String[] args) {
playerCreateSelection gui = new superClass().new playerCreateSelection();
gui.setSize(500, 500);
gui.setVisible(true);
}
Try that.
This question is about Frames, Java and Processing.
This questions sounds pretty convoluted but its really not. I'll try keep this to a simple minimum. I'm creating a small ball in a maze game to get my head around physics and rendering. It's been a good experience so far but I've hit a bit of a brick wall.
The general layout I decided on was to contain PApplets within a AWT Frame and have the Frame close. The reason for this is because I was told that you should only have on instance of a Papplet at a time.
PApplet is the Applet class in Processing, a rendering library.
I have 3 classes here including the main
public class Menu extends PApplet
{
//images and buttons
PImage background, playbtn1, playbtn2, hsbtn1, hsbtn2, abbtn1, abbtn2, exbtn1, exbtn2;
FBox pBtn, hBtn, eBtn;
FWorld menu;
//simple constructor
public Menu()
{
}
public void setup()
{
size(600, 400);
smooth();
Fisica.init(this);
menu = new FWorld();
//loading and placing images
background = loadImage("MenuAlt.jpg");
System.out.println(background);
playbtn1 = loadImage("play1.gif");
playbtn2 = loadImage("play2.gif");
hsbtn1 = loadImage("high1.gif");
hsbtn2 = loadImage("high2.gif");
exbtn1 = loadImage("exit1.gif");
exbtn2 = loadImage("exit2.gif");
//loading and placing buttons
pBtn = new FBox(120, 150);
pBtn.setPosition(135, 215);
pBtn.setDrawable(false);
hBtn = new FBox(120, 150);
hBtn.setPosition(295, 215);
hBtn.setDrawable(false);
eBtn = new FBox(120, 150);
eBtn.setPosition(455, 215);
eBtn.setDrawable(false);
//add item to world
menu.add(pBtn);
menu.add(hBtn);
menu.add(eBtn);
}
public void draw()
{
image(background, 0, 0);
image(playbtn1, 80, 140);
image(hsbtn1, 237, 135);
image(exbtn1, 400, 140);
mouseOver();
menu.draw();
}
//close this frame an open a new level, high score or exit
//depending on what the use clicks
public void mousePressed()
{
FBody pressed = menu.getBody(mouseX, mouseY);
if (pressed == pBtn)
{
System.out.println("play game");
this.getParent().getParent().getParent().getParent().setVisible(false);
ExampleFrame x = new ExampleFrame(new Level("level1.txt"));
x.setLocation(this.getParent().getParent().getParent().getParent().getLocation());
}
if (pressed == hBtn)
{
System.out.println("high scores");
this.getParent().getParent().getParent().getParent().setVisible(false);
/* these are just for finding the parent
System.out.println(this.getName());
System.out.println(this.getParent().getName());
System.out.println(this.getParent().getParent().getName());
System.out.println(this.getParent().getParent().getParent().getName());
System.out.println(this.getParent().getParent().getParent().getParent().getName());
*/
ExampleFrame x = new ExampleFrame(new HighScores()); //for testing, you can change this to new menu()
x.setLocation(this.getParent().getParent().getParent().getParent().getLocation());
}
if (pressed == eBtn)
{
System.out.println("exit");
System.exit(0);
}
}
the exampleFrame class
public class ExampleFrame extends JFrame
{
PApplet app;
public ExampleFrame(PApplet emApp)
{
super("Ball Maze Game");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(200, 200);
app = emApp;
setSize(615,438);
setVisible(true);
setLayout(new BorderLayout());
add(app, BorderLayout.CENTER);
app.init();
}
}
the main
public class Main
{
public static void main(String[] args)
{
ExampleFrame x = new ExampleFrame(new Menu());
}
}
What needs to happen when mousePressed == ebtn is all the stuff in the Frame will be removed and a Highscores Screen will be loaded. highscores is almost the same as menu. There is no need to post code as there is enough here.
The second class is the one which acts as a frame and holds the PApplet
Bottom line, has anyone have any idea how to call the Frame methods from the PApplet or another way to remove all PApplets contents and load another PApplet in?
What needs to happen when mousePressed == ebtn is all the stuff in the Frame will be removed and a Highscores Screen will be loaded
The demo. below of a nested CardLayout adds an ActionListener instead of a MouseListener. It reacts to both mouse and keyboard input.
There are a multitude of other ways to include more than one GUI element in the same screen space. Off the top of my head, JTabbedPane, JSplitPane, JDesktopPane/JInternalFrame, popping the high scores in a JDialog or JOptionPane..
Screenshots
CardLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CardLayoutDemo {
public static void main(String[] args) {
Runnable r = new Runnable () {
public void run() {
final JRadioButton game = new JRadioButton("Game", true);
JRadioButton highScores = new JRadioButton("High Scores");
ButtonGroup bg = new ButtonGroup();
bg.add( game );
bg.add( highScores );
JPanel buttons = new JPanel(new
FlowLayout(FlowLayout.CENTER, 5, 5));
buttons.add( game );
buttons.add( highScores );
JPanel gui = new JPanel(new BorderLayout(5,5));
gui.add(buttons, BorderLayout.SOUTH);
final CardLayout cl = new CardLayout();
final JPanel cards = new JPanel(cl);
gui.add(cards);
cards.add(new JLabel("Level 1"), "game");
cards.add(new JLabel("High Scores"), "scores");
ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent ae) {
if (game.isSelected()) {
cl.show(cards, "game");
} else {
cl.show(cards, "scores");
}
}
};
game.addActionListener(al);
highScores.addActionListener(al);
JOptionPane.showMessageDialog(null, gui);
}
};
SwingUtilities.invokeLater(r);
}
}
In order to answer How to call the Frame methods from the PApplet?, I have modified your code snippet to bare minimum. In this modified version when the user click mouse button a System.out is fired.
Now there are two ways in which you can access your Frame object. But before that let me state these two points:
When you create a PApplet like new ExampleFrame(new Menu()); and add it in your JFrame like this add(app, BorderLayout.CENTER); then a complex hierarchy of windows/panels are created.
Like this:
javax.swing.JPanel
javax.swing.JLayeredPane
javax.swing.JRootPane
test.ExampleFrame
PApplet provides a public field for setting and accessing your frame object. And amazingly it is called frame :). You can set it before calling app.init();
>>Code
** Checkout the comments in the code**
Modified ExampleFrame.java
import java.awt.BorderLayout;
import javax.swing.JFrame;
import processing.core.PApplet;
public class ExampleFrame extends JFrame
{
private static final long serialVersionUID = 4792534036194728580L;
PApplet app;
public ExampleFrame(PApplet emApp)
{
super("Ball Maze Game");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(200, 200);
app = emApp;
setSize(615,438);
setVisible(true);
setLayout(new BorderLayout());
add(app, BorderLayout.CENTER);
// Setting my frame object
app.frame = this;
app.init();
}
// Sample Method
public void sampleMethod(String msg)
{
System.out.println("I think '"+ msg +"' called me !!");
}
}
Modified Menu.java
import java.awt.Container;
import processing.core.PApplet;
import processing.core.PImage;
public class Menu extends PApplet
{
private static final long serialVersionUID = -6557167654705489372L;
PImage background;
static String tab = "";
//simple constructor
public Menu()
{
}
public void setup()
{
size(600, 400);
smooth();
background = loadImage("C:/temp/background.jpg");
}
public void draw()
{
image(background, 0, 0);
}
public void mousePressed()
{
Container p = getParent();
tab = "";
// FIRST WAY OF ACCESSING PARENT FRAME
while(p != null)
{
//printParentTree(p);
if(p instanceof ExampleFrame)
{
ExampleFrame myframe = (ExampleFrame)p;
myframe.sampleMethod("First Way");
break;
}
p = p.getParent();
}
// SECOND WAY OF ACCESSING PARENT FRAME
if(frame != null && (frame instanceof ExampleFrame))
{
ExampleFrame myframe = (ExampleFrame)p;
myframe.sampleMethod("Second Way");
}
}
void printParentTree(Container p)
{
System.out.println(tab+p.getClass().getName());
tab +='\t';
}
}
Checkout the public void mousePressed() method.
For completeness, I am also including Main.java.
public class Main {
public static void main(String[] args){
new ExampleFrame(new Menu());
}
}
Now to answer Remove all PApplets contents and load another PApplet in
Well I have not tested it. But you can add a JPanel to your JApplet and do all your drawing on that i.e creating child controls etc. When feel like redrawing then call JPanel.removeAll(). Which as per javadoc:
Removes all the components from this
container. This method also notifies
the layout manager to remove the
components from this container's
layout via the removeLayoutComponent
method.
After this call repaint on the JPanel. Try it out, it might work :).
public class Calculator extends JComponent {
Result rez = new Result();
public void init() {
Frame frame = new Frame();
Panel panel = new Panel();
panel.add(rez);
frame.add(panel);
Test test = new Test(); <- I thing this is the problem, instancing a child inside a parent. Not sure.
test.test(); <- doing nothing
}
}
class Frame extends JFrame {
public Frame() {
// .. setVisible, setTitle....
}
}
class Panel extends JPanel {
public Panel() {
// setBounds, setLayout
}
}
class Test extends Calculator {
public void test() {
rez.setText("Test"); <- This does nothing
}
}
Trying to make a calculator and I'm having trouble updating the results box. The method test() in the Test class is not updating the JTextBox.
However, if i create a method in the main Calculator() class, called update() and put the rez.setText() inside, it works every time.
This works tho:
public class Calculator extends JComponent {
Result rez = new Result();
public void init() {
Frame frame = new Frame();
Panel panel = new Panel();
panel.add(rez);
frame.add(panel);
this.update(); <- works
}
public void update() {
rez.setText("Test"); <- works
}
}
Why is that? Extended classes should share variables. Should they?
I'm writing an Java Application and trying to paint an BufferedImage. In my main, the JFrame is being created and the JPanel is being created and is being added to the JFrame. There is also a Thread being started for repainting, but it shows nothing and my System.out.println() in the paintComponent isn't called, too. I've googled this a lot, but i found nothing to solve my problem.
What am i doing wrong and why is it wrong???
My Code:
The main + Thread:
public class Main extends Thread
{
public static Frame frame = new Frame();
public static void main(String[] args) throws IllegalStateException, IOException
{
frame.activePanel = new LoginPanel();
frame.add(frame.activePanel);
new Main();
}
public Main()
{
this.start();
}
#Override
public void run()
{
while(true)
{
if(Main.frame.activePanel != null)
Main.frame.activePanel.repaint();
try{Thread.sleep(15);}catch(InterruptedException e){}
}
}
}
The JFrame:
public class Frame extends JFrame
{
public JPanel activePanel = null;
public Frame()
{
super();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(this.getToolkit().getScreenSize().width / 2 - 640,this.getToolkit().getScreenSize().height / 2 - 400,1279,799);
this.setResizable(false);
this.setUndecorated(true);
this.setVisible(true);
}
}
and the JPanel:
public class LoginPanel extends JPanel
{
BufferedImage loginImg;
public LoginPanel() throws IOException
{
loginImg = ImageIO.read(new File("src/images/Login.PNG"));
}
#Override
protected void paintComponent(Graphics g)
{System.out.println("painting");
g.drawImage(loginImg, 0, 0, null);
}
}
Looks to me like you are adding the panel to the frame AFTER the frame is visible. When you do this the layout manager is not invoked and the size of the panel is (0, 0) so there is nothing to paint.
Restructure your code. The creation of the panel should be done in the Frame class, not Main class.
Also, use a better name instead of Frame. The AWT already has a class called Frame so you name is very confusing. Make your class name more descriptive.