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();
}
Related
I have two classes in one JFrame creation:
public class Window extends JFrame {
public void createWindow() throws IOException {
setTitle(GeneralValues.PROGRAM_NAME);
setSize(1100, 600);
setResizable(false);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLayout(null);
setVisible(true);
}
}
And in the second I have a JLabel:
public class InitialConfiguration {
Window w = new Window();
public void main() {
JLabel blauncherIcon = new JLabel();
blauncherIcon.setBounds(100, 100, 100, 100);
blauncherIcon.setOpaque(true);
blauncherIcon.setText("Text");
w.add(blauncherIcon);
}
}
My main method:
public class Main {
Window w = new Window();
InitialConfiguration ic = new InitialConfiguration();
public static void main(String[] args) {
w.createWindow();
ic.main();
}
}
I wanted to add a label from the InitialConfiguration class to the frame from the Window class but unfortunately this code creates a frame but does not add the label from the InitialConfiguration class. Can it be done at all?
Your issue is, that you went wrong with the instances of your Window. Your InitialConfiguration has a member variable of Window, to which the label is being added. But this is not the same instance of the Window that you have in your Main class.
So all in all, you create and show the window, but never show the second window you actually added your label to.
So altogether, if you really want to keep your structure this way (I would advise against that but you may have your reasons), passing the correct Window instance as a method parameter should fix it.
public class InitialConfiguration {
// Removed second Window here
public void main(Window w) { // rename this method please
JLabel blauncherIcon = new JLabel();
blauncherIcon.setBounds(100, 100, 100, 100); // use layout managers
blauncherIcon.setOpaque(true);
blauncherIcon.setText("Text");
w.add(blauncherIcon);
}
}
And your "Main":
public class Main {
Window w = new Window(); // rename the class Window please
InitialConfiguration ic = new InitialConfiguration();
public static void main(String[] args) {
w.createWindow();
ic.main(w);
}
}
You didn't start the Jframe at window class and I don't know your main class so I made it for your understanding. Check this codes;
(Edited)
Main: Example code:
public class MainMenu{
public static void main(String[] args) {
Windows w = new Windows();
InitialConfiguration ini = new InitialConfiguration(w);
w.createWindow();
ini.main();
}
}
ICONFIG class example code:
public class InitialConfiguration {
Window w;
public InitialConfiguration(Windows w){
this.w = w;
}
public void main() {
JLabel blauncherIcon = new JLabel();
blauncherIcon.setBounds(100, 100, 100, 100);
blauncherIcon.setOpaque(true);
blauncherIcon.setText("Text");
w.add(blauncherIcon);
w.repaint();
}
}
Window class example code:
public class Window extends JFrame {
public void createWindow(){
setTitle("Your title");
setSize(1100, 600);
setResizable(false);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLayout(null);
setVisible(true);
}
}
shortened code:
public class Test{
private JFrame frame;
private TestPanel panel;
Object obj;
public static void main(String args[]) {
Test test = new Test();
}
public Test() {
try {
// Setup GUI
frame = new JFrame("Test");
panel = new TestPanel();
frame.add(panel);
}
}
}
Is it possible to directly access obj from panel?
If yes: How?
Thank you in advance for your help. :)
No; Not without passing the reference to object to the panel.
My Professor ask me to create one JSwing application which have three tabbed panes each hold a single game.
I have created three different java applications for three different games (Snake, MineSweaper, and Packman) according to zetcode.com website. Each Application contains a Main Class which hold the JFrame and the main method, and another class (extends JPanel) to create the board of the game and lunch it.
Here is an example:
public class Minesweeper_Main extends JFrame {
JFrame jfrm;
Container pane;
JLabel statusbar;
public Minesweeper_Main () {
jfrm= new JFrame("Minesweeper");
jfrm.setSize(500,700);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setLocationRelativeTo(null);
jfrm.setResizable(false);
pane=jfrm.getContentPane();
statusbar = new JLabel("");
add(statusbar, BorderLayout.SOUTH);
add(new MineSweeper_Board(statusbar));
jfrm.setVisible(true);
}
public static void main(String[] args) {
new Minesweeper_Main ();
}
}
public class MineSweeper_Board extends JPanel {
……..
public MineSweeper_Board(JLabel statusbar) {
this.statusbar = statusbar;
………
setDoubleBuffered(true);
addMouseListener(new MinesAdapter());
newGame();
}
……..
}
Now, I would like to create a new Java Application (Swing Interface), which contains three Tabbed Panes, each hold or call one specific game. Unfortunately, I am either getting error all the times or getting blanks tabs.
The code for the new JSwing application is as following:
public class MF_GameHub extends JFrame {
JFrame jfrm;
Container pane;
JTabbedPane jtp;
public MF_GameHub(){
jfrm= new JFrame("GameHub");
jfrm.setSize(500,700);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setLocationRelativeTo(null);
jfrm.setResizable(false);
pane=jfrm.getContentPane();
………
jtp = new JTabbedPane();
jtp.addTab("MineSweeper", new MineSweeper());
jtp.addTab("PacMan", new PacMan());
jtp.addTab("Snake", new Snake());
pane.add(jtp);
jfrm.setVisible(true);
}
public static void main(String[] args) {
new MF_GameHub();
}
}
class MineSweeper extends JPanel {
//I DO NOT KNOW HOW TO CALL THE OTHER JAVA APPLICATION FROM HERE
}
class PacMan extends JPanel {
//I DO NOT KNOW HOW TO CALL THE OTHER JAVA APPLICATION FROM HERE
}
class Snake extends JPanel {
//I DO NOT KNOW HOW TO CALL THE OTHER JAVA APPLICATION FROM HERE
}
How can I integrate all three games under one application?
In this case, I will have split my JFrame and the JFrame's contentPane.
After that, you just have to use the contentpane and import this into your app where you want!
Example with your code:
public class Minesweeper_Main extends JFrame {
Container pane;
JLabel statusbar;
public Minesweeper_Main (JPanel contentPane) {
super("Minesweeper");
setSize(500,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setContentPane(contentPane); // << Add
setVisible(true);
}
public static void main(String[] args) {
new Minesweeper_Main (new MineSweeper_Pane()); // << Changed
}
}
public class MF_GameHub extends JFrame {
Container pane;
JTabbedPane jtp;
public MF_GameHub(){
super("GameHub");
setSize(500,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
pane=getContentPane();
………
jtp = new JTabbedPane();
jtp.addTab("MineSweeper", new MineSweeper_Pane());
jtp.addTab("PacMan", new PacMan_Pane());
jtp.addTab("Snake", new Snake_Pane());
pane.add(jtp);
setVisible(true);
}
public static void main(String[] args) {
new MF_GameHub();
}
}
And you add this:
public class MineSweeper_Pane extends JPanel {
public MineSweeper_Pane() {
statusbar = new JLabel("");
add(statusbar, BorderLayout.SOUTH);
add(new MineSweeper_Board(statusbar));
}
}
EDIT I'm tired I think; you are using JFrame attribute in a JFrame extended class; Remove the JFrame attribute and use this (or nothing) instead of using the attribute jfrm ;)
I'm a freshman with all the GUI stuff and I want to know why do I have to declare all the JFrames with "static" in my code(otherwise it wont compile) and how can I modify my code to compile without declaring them "static"?
public class Ventana extends JFrame {
private static JFrame frameInicio = new JFrame("Ingresar Datos");
private static JFrame frameCrearUsuario = new JFrame("Crear Usuario");
private static JFrame frameMenu = new JFrame("Menu Del Juego");
private static JFrame frameJuego = new JFrame("El Ahorcado");
public static void main(String[] args) {
Inicio();
}
public static void Inicio(){
frameCrearUsuario.dispose();
frameMenu.dispose();
frameInicio.setSize(450,200);
frameInicio.setLocationRelativeTo(null);
frameInicio.add(new FrameInicio());
frameInicio.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameInicio.setVisible(true);
}
public void CrearUsuario(){
frameInicio.dispose();
frameCrearUsuario.setSize(450,300);
frameCrearUsuario.setLocationRelativeTo(null);
frameCrearUsuario.add(new FrameCrearUsuario());
frameCrearUsuario.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameCrearUsuario.setVisible(true);
}
public void Menu(){
frameInicio.dispose();
frameJuego.dispose();
frameMenu.setSize(660,290);
frameMenu.setLocationRelativeTo(null);
frameMenu.add(new FrameMenu());
frameMenu.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameMenu.setVisible(true);
}
public void Juego(){
frameMenu.dispose();
frameJuego.setSize(1100,800);
frameJuego.setLocationRelativeTo(null);
frameJuego.add(new FrameJuego());
frameJuego.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameJuego.validate();
frameJuego.setVisible(true);
}
It's becuase your public static void Inicio() is static. You can't call a non static property from a static method
Here's a common way to use a JFrame. Create a constructor for the JFrame and add all the components inside of it. Then just Create the JFrame object in the main method
public class Ventana extends JFrame {
JPanel panel = new JPanel();
JButton button = new JButton("button");
JTextArea jta = new JTextArea();
public Vantan(){
add(panel);
add(button);
add(jta);
}
public static void main(String[] args){
JFrame frame = new Ventana();
frame.setTitle("Frame);
frame.setSize(500, 500);
frame.pack();
frame.setDefaulCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
This way you don't need the static properties as in your case.
I have a working application, but I need to convert it to an applet.
My main method isn't located in my frame class so I can't just extend JApplet and change my main method to init().
Is there an easy way to "wrap" an applet around an application.
I would separate out the guts of your UI creation, then call it either from main() or init(). See the below example:
public class Test extends Applet {
private JPanel mainPanel;
// run as application
public static void main(String[] args) {
Test test = new Test();
test.createUI();
JFrame frame = new JFrame();
frame.add(test.mainPanel);
frame.pack();
frame.setVisible(true);
}
// run as applet
public void init() {
createUI();
add(mainPanel);
}
// create your UI here
private void createUI() {
mainPanel = new JPanel();
mainPanel.add(new JButton("Test"));
}
}