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"));
}
}
Related
This has been asked before but I would like clarification, I'm new to java coding (sort of, started coding last month) and would like to know simply how can I switch between UIs in one JFrame, Picture this, a settings menu, How do I make it in one JFrame window instead of just make a new window with all the settings, If you don't get it, ask for clarification.
You can implement a frame (JFrame) and, for example, two panels (JPanel). Initially you embed panel A inside frame, when you want to show panel B then call the method showPanelB()
public class MyFrame extends JFrame {
PanelA panelA = new PanelA();
PanelB panelB = new PanelB();
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyFrame().setVisible(true);
}
});
}
public MyFrame() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
showPanelA();
}
public void showPanelA() {
getContentPane().add(panelA, BorderLayout.CENTER);
}
public void showPanelB() {
getContentPane().add(panelB, BorderLayout.CENTER);
}
}
class PanelA extends JPanel {
// Panel implementation
}
class PanelB extends JPanel {
// Panel implementation
}
So im trying to make a simple GUI that goes up by one when you click a button in it. I get this error, however, when i try and run the test GUI: Error. Here is the code
import javax.swing.JFrame;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class Main {
public GUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(borderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setLayout(new GridLayout(0, 1));
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Clicks");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
}
public class Main {
public GUI() {
GUI seems intended as a constructor and the constructor must have the same name as the class. But neither GUI nor Main are good, descriptive names for a class / constructor. The name should be descriptive of that the class actually is. So here is something that might not only be better, but should work for this case.
public class ClicksGUI {
public ClicksGUI() {
Note that if changing the name of the class / constructor, the reference in the main method also needs to change to reflect that.
Add void as return type for your GUI method
public class Main {
public static void GUI() {
.....
}
}
and call it as a method from main() method.
public static void main(String[] args) {
GUI();
}
Java expects a return type for methods . If you don't want to specify return type , use a constructor instead.
If you want GUI to be a function you must specify a return type (I suppose you want void in your case), and call it like a regular function, not to create an object of it:
public class Main {
public static void GUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(borderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setLayout(new GridLayout(0, 1));
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Clicks");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
GUI();
}
}
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();
}
Below are two images showing the problem I'm facing. Whenever I run the project there is 50/50 chance that my JPanels load properly, otherwise only 1 JPanel is loaded even though I'm simply looping through array and adding JPanels to the JFrame.
viewComponents.forEach(viewComponent -> this.add(viewComponent));
Working
Not working
DashboardView.java
public class DashboardView extends JFrame{
List<ViewComponent> viewComponents = new ArrayList();
ViewComponentFactory viewComponentFactory = new ViewComponentFactory();
JFrame dashboardInput = new JFrame();
public DashboardView(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
createGauges(); //Adding 2 JPanels
viewComponents.forEach(viewComponent -> this.add(viewComponent));
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
dashboardInput.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dashboardInput.setLayout(new FlowLayout());
createInputs(); //Adding JPanels
dashboardInput.pack();
dashboardInput.setLocation(this.getX()+(this.getWidth()/2)-(dashboardInput.getWidth()/2), this.getY()+this.getHeight());
dashboardInput.setVisible(true);
}
private void createGauges(){
viewComponents.add(viewComponentFactory.getViewComponent(ViewComponentFactory.ViewComponentType.RadialCircleGauge,0,800, "Speedometer", "KM/H"));
viewComponents.add(viewComponentFactory.getViewComponent(ViewComponentFactory.ViewComponentType.LinearGauge, -100,100, "Temperature", "Celcius"));
}
Main.java
public class Main {
public static void main(String[] args) {
DashboardView dashboardView = new DashboardView();
dashboardView.setVisible(true);
}
}
The main problem seems to be, that your DashboadView isn't initialized within the EDT - Thread (Event Dispatching Thread). All GUI actions must be done within this thread. Otherwise strange things will happen (e.g. artifacts while updateing UI).
One should initialize its GUI this way, to ensure the start is wihtin the right Thread.
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
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 ;)