i'm relatively new to programming and i'm trying to get the JLabel to update, unfortunately every time I reset the text it doesn't update on the JFrame. Is there something i'm doing wrong? The h.run(); segment in the actionlistener simply decrements an integer in another thread. The h.gethealth then returns that value.
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class Main {
JFrame healthcounter = new JFrame();
JLabel number = new JLabel();
public void startHealthFrame(int health){
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
System.out.println("health: "+health);
if(health==500) {
healthcounter.setLocation((int) dim.getWidth() - 300, 0);
healthcounter.setUndecorated(true);
healthcounter.setOpacity(1);
healthcounter.setSize(300,100);
number.setFont(new Font("Arial", Font.BOLD,50));
number.setText("Health: "+Integer.toString(health));
healthcounter.getContentPane().add(number);
healthcounter.setVisible(true);
}else {
number.setText("Health: "+Integer.toString(health));
}
}
public static void main(String args[]){
Main m = new Main();
Health h = new Health();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
JFrame q1 = new JFrame();
JFrame q2 = new JFrame();
JFrame q4 = new JFrame();
JFrame q3 = new JFrame();
m.startHealthFrame(500);
q1.setUndecorated(true);
q2.setUndecorated(true);
q3.setUndecorated(true);
q4.setUndecorated(true);
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
button1.setBorder(null);
button2.setBorder(null);
button3.setBorder(null);
button4.setBorder(null);
try {
Image topleft = ImageIO.read(Main.class.getResource("/resources/upperleft.png"));
Image topright = ImageIO.read(Main.class.getResource("/resources/upperright.png"));
Image bottomleft = ImageIO.read(Main.class.getResource("/resources/bottomleft.png"));
Image bottomright = ImageIO.read(Main.class.getResource("/resources/bottomright.png"));
button1.setIcon(new ImageIcon(topleft));
button2.setIcon(new ImageIcon(topright));
button3.setIcon(new ImageIcon(bottomleft));
button4.setIcon(new ImageIcon(bottomright));
} catch (IOException e) {
e.printStackTrace();
}
q1.add(button1);
q2.add(button2);
q4.add(button4);
q3.add(button3);
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
h.run();
Main m = new Main();
m.startHealthFrame(h.gethealth());
}
});
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
h.run();
Main m = new Main();
m.startHealthFrame(h.gethealth());
}
});
button3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
h.run();
Main m = new Main();
m.startHealthFrame(h.gethealth());
}
});
button4.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
h.run();
Main m = new Main();
m.startHealthFrame(h.gethealth());
}
});
q1.setSize(250,250);
q2.setSize(250,250);
q3.setSize(250,250);
q4.setSize(250,250);
q1.setDefaultCloseOperation(q1.EXIT_ON_CLOSE);
q2.setDefaultCloseOperation(q2.EXIT_ON_CLOSE);
q3.setDefaultCloseOperation(q3.EXIT_ON_CLOSE);
q4.setDefaultCloseOperation(q4.EXIT_ON_CLOSE);
q1.setLocation((dim.width/2-q1.getSize().width/2)-125, (dim.height/2-q1.getSize().height/2)-125);
q2.setLocation(q1.getX()+q1.getWidth()-8,q1.getY());
q3.setLocation(q1.getX(),q1.getY()+q1.getHeight()-8);
q4.setLocation(q1.getX()+q1.getWidth()-8,q1.getY()+q1.getHeight()-8);
q1.setVisible(true);
q2.setVisible(true);
q4.setVisible(true);
q3.setVisible(true);
try
{
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(Main.class.getResource("/resources/megalovania.wav")));
clip.start();
clip.loop(clip.LOOP_CONTINUOUSLY);
}
catch (Exception exc)
{
exc.printStackTrace(System.out);
}
Time time = new Time(q1,q2,q3,q4);
time.start();
}
}
Related
i'm making a tiny program where you have 3 timers and when you press start it gives a progress bar until time is over, but JFrame decided to do this after I pressed start:
Here is my code:
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
static int getTime(JFrame frame){
String ts = JOptionPane.showInputDialog(frame, "Set time: ", "time", 1);
return Integer.parseInt(ts);
}
public static class globalVars{
public static int time1 = 5;
public static int time2 = 5;
public static int time3 = 5;
}
static void startTimer(JFrame frame) {
int time = globalVars.time1+globalVars.time2+globalVars.time3;
int timeF = Math.toIntExact(System.currentTimeMillis() / 1000l);
JProgressBar bar = new JProgressBar(0, time);
frame.add(bar);
frame.pack();
int i=0;
while (System.currentTimeMillis()!=timeF+time* 60L){
bar.setValue((timeF+time*60-Math.toIntExact(System.currentTimeMillis() / 1000l))/60);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Timer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
//-----------------------------------------------------------//
Icon icon1 = new ImageIcon("placeholder.png");
String button1Text = "Button1, ";
JButton button1 = new JButton(String.format("%s%dmin", button1Text, globalVars.time1));
button1.setIcon(icon1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
globalVars.time1=getTime(frame);
button1.setText(String.format("%s%dmin", button1Text, globalVars.time1));
}
});
frame.add(button1);
//-----------------------------------------------------------//
Icon icon2 = new ImageIcon("placeholder.png");
String button2Text = "Button2, ";
JButton button2 = new JButton(String.format("%s%dmin", button2Text, globalVars.time2));
button2.setIcon(icon2);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
globalVars.time2=getTime(frame);
button2.setText(String.format("%s%dmin", button2Text, globalVars.time2));
}
});
frame.add(button2);
//-----------------------------------------------------------//
Icon icon3 = new ImageIcon("placeholder.png");
String button3Text = "Button3, ";
JButton button3 = new JButton(String.format("%s%dmin", button3Text, globalVars.time3));
button3.setIcon(icon3);
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
globalVars.time3=getTime(frame);
button3.setText(String.format("%s%dmin", button3Text, globalVars.time3));
}
});
frame.add(button3);
//-----------------------------------------------------------//
JButton buttonStart = new JButton("Start");
buttonStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonStart.removeActionListener(this);
startTimer(frame);
}
});
frame.add(buttonStart);
//-----------------------------------------------------------//
frame.pack();
frame.setSize(400, 200);
frame.setVisible(true);
}
}
I know it is a very inefficient way of doing things and I will improve it but now I'm more worried about the black screen. Tnx!
The problem here is that my countdown timer just subtracts the number I input whenever I click the "START" button. I don't really know what kind of loop to use here for it to be like a real countdown timer... is it do..while? for loop? or while loop?
Btw, we must rely only on Threads and GUI.
I tried for loop, but I don't know what to use after that.
package com.countdown;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class CountDown extends WindowAdapter implements ActionListener, Runnable {
#Override
public void run() {
}
public void stop() {
f.dispose();
}
JLabel l1, l2, l3;
JTextField tf1, tf2, tf3;
JButton btn1, btn2, btn3;
JFrame f;
JPanel p1, p2;
public CountDown() {
l1 = new JLabel("hrs:");
l2 = new JLabel("mins:");
l3 = new JLabel("sec:");
tf1 = new JTextField("", 5);
tf2 = new JTextField("", 5);
tf3 = new JTextField("", 5);
btn1 = new JButton("START");
btn2 = new JButton("STOP");
btn3 = new JButton("BACK");
p1 = new JPanel();
p2 = new JPanel();
f = new JFrame("Countdown Timer");
}
public void setLaunch() {
f.add(p1);
f.add(p2);
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(l3);
p1.add(tf3);
p2.add(btn1);
p2.add(btn2);
p2.add(btn3);
f.setLayout(new GridLayout(2,1));
f.pack();
f.setSize(440,150);
f.setVisible(true);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
f.addWindowListener(this);
}
public static void main(String[] args) {
CountDown cd = new CountDown();
cd.setLaunch();
Thread t1 = new Thread(cd); //hrs
Thread t2 = new Thread(cd); //mins
Thread t3 = new Thread(cd); //sec
Thread t4 = new Thread(cd); //stop
t1.start();
try {
t1.join();
}
catch(Exception e) {
System.out.println(e);
}
t2.start();
try {
t2.join();
}
catch(Exception e) {
System.out.println(e);
}
t3.start();
try {
t3.join();
}
catch(Exception e) {
System.out.println(e);
}
t4.stop();
}
#Override
public void actionPerformed(ActionEvent e) {
int hrs = Integer.parseInt(tf1.getText());
int mins = Integer.parseInt(tf2.getText());
int secs = Integer.parseInt(tf3.getText());;
if (hrs < 60) {
try {
Thread.sleep(1000);
hrs--;
}
catch (InterruptedException e1) {
e1.printStackTrace();
}
}
tf1.setText(Integer.toString(hrs));
if (mins < 60) {
try {
Thread.sleep(1000);
mins--;
}
catch (InterruptedException e1) {
e1.printStackTrace();
}
}
tf2.setText(Integer.toString(mins));
if (secs < 60) {
try {
Thread.sleep(1000);
secs--;
}
catch (InterruptedException e1) {
e1.printStackTrace();
}
}
tf3.setText(Integer.toString(secs));
}
public void windowClosing(WindowEvent e) {
f.dispose();
}
}
The goal of the program is: when I put a number in hrs, minutes and/or secs, it countdowns in text fields.
One good option for repetitive swing gui updates is to use a swing timer
as demonstrated in the following mcve :
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Counter {
private int count = 1;
Counter() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new CounterPane(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
class CounterPane extends JPanel {
CounterPane() {
setPreferredSize(new Dimension(200, 100));
setLayout(new GridBagLayout());
JLabel label = new JLabel(" ");
add(label);
Timer timer = new Timer(1000, e -> {
count++;
label.setText(Integer.toString(count));
});
timer.setInitialDelay(0);
timer.start();
}
}
public static void main(String[] args) {
new Counter();
}
}
The GUI on the calculator is finished however it does not do any math.
I heard about this command called "parse Int". With this I am trying to receive a string from the user and convert it into an integer. When I click an operator button I want it to receive input again and when i press the equals button I want java to register that string as the second number.
Does anyone know how I could approach this?
I am new in Java and I am doing this for experience with no time limit so I am open to different ways to make the calculator. Below I provide the code...
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.GridLayout;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Scanner;
import javax.swing.JSplitPane;
public class CalcGUI {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalcGUI window = new CalcGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
// parse (int)
/**
* Create the application.
*/
public CalcGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
panel.setLayout(new BorderLayout(0, 0));
JSplitPane splitPane = new JSplitPane();
panel.add(splitPane);
textField = new JTextField();
splitPane.setLeftComponent(textField);
textField.setColumns(10);
//a = int.parse(int);
textField_1 = new JTextField();
splitPane.setRightComponent(textField_1);
textField_1.setColumns(10);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new GridLayout(0, 3, 0, 0));
JPanel panel_2 = new JPanel();
panel_1.add(panel_2);
JButton btnNewButton_01 = new JButton("1");
panel_2.add(btnNewButton_01);
btnNewButton_01.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "1");
}});
JButton btnNewButton_04 = new JButton("4");
panel_2.add(btnNewButton_04);
btnNewButton_04.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "4");
}});
JButton btnNewButton_07 = new JButton("7");
panel_2.add(btnNewButton_07);
btnNewButton_07.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "7");
}});
JButton btnNewButton_squared = new JButton("x^2");
panel_2.add(btnNewButton_squared);
btnNewButton_squared.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "^2");// NOT SURE YET
}});
JButton btnNewButton_root = new JButton("Sqr");
panel_2.add(btnNewButton_root);
btnNewButton_root.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//do whatever should happen when the button is clicked...
}
});
JButton btnNewButton_clear = new JButton("clear");
btnNewButton_clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel_2.add(btnNewButton_clear);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3);
JButton btnNewButton_02 = new JButton("2");
panel_3.add(btnNewButton_02);
btnNewButton_02.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "2");
}});
JButton btnNewButton_05 = new JButton("5");
panel_3.add(btnNewButton_05);
btnNewButton_05.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "5");
}});
JButton btnNewButton_08 = new JButton("8");
panel_3.add(btnNewButton_08);
btnNewButton_08.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "8");
}});
JButton btnNewButton_09 = new JButton("9");
panel_3.add(btnNewButton_09);
btnNewButton_09.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "9");
}});
JButton btnNewButton_times = new JButton("x");
panel_3.add(btnNewButton_times);
btnNewButton_times.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "x");
}});
JButton btnNewButton_divide = new JButton("/");
panel_3.add(btnNewButton_divide);
btnNewButton_divide.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "/");
}});
JButton btnNewButton_delete = new JButton("delete");
panel_3.add(btnNewButton_delete);
btnNewButton_delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//do whatever should happen when the button is clicked...
}
});
JPanel panel_4 = new JPanel();
panel_1.add(panel_4);
JButton btnNewButton_03 = new JButton("3");
btnNewButton_03.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "3");
}
});
panel_4.add(btnNewButton_03);
JButton btnNewButton_06 = new JButton("6");
btnNewButton_06.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel_4.add(btnNewButton_06);
btnNewButton_06.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "6");
}});
JButton btnNewButton_00 = new JButton("0");
panel_4.add(btnNewButton_00);
btnNewButton_00.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "0");
}});
JButton btnNewButton_plus = new JButton("+");
panel_4.add(btnNewButton_plus);
btnNewButton_plus.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "+");
}});
JButton btnNewButton_minus = new JButton("-");
panel_4.add(btnNewButton_minus);
btnNewButton_minus.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "-");
}});
JButton btnNewButton_equals = new JButton("=");
panel_4.add(btnNewButton_equals);
btnNewButton_equals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
}
Copying the whole code into the question is a bit too much, but let me suggest you a hint as more or less pseudocode:
String numToConvert = textField.getText();
try {
int num = new Integer(numToConvert);
//sum up or ...
} catch (Exception e) { //numToConvertIsNotAnInteger
//-> must be an operator
}
I have created an ActionListener and the button but the button doesn't work.
The action listener is supposed to be integrated with the submit button, please help?
Code:
import javax.swing.*;
import javax.swing.event.DocumentListener;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Executer {
private JLabel lblCommand;
private JTextField txtEnter;
private JButton btNext, btPrevious, btSubmit;
private JPanel panel;
public static void main(String[] args) {
new Executer();
}
public Executer() {
JFrame frame = new JFrame("Script Executer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,300);
frame.setVisible(true);
myPanel();
Text();
Fields();
Buttons();
frame.add(panel);
frame.setVisible(true);
}
public void myPanel() {
panel = new JPanel();
panel.setLayout(null);
}
public void Text() {
lblCommand = new JLabel("Enter Here");
lblCommand.setBounds(145, 100, 150, 20);
Font styleOne = new Font("Arial", Font.BOLD, 13);
lblCommand.setFont(styleOne);
panel.add(lblCommand);
}
public void Fields() {
txtEnter = new JTextField();
txtEnter.setBounds(230, 100, 120, 20);
panel.add(txtEnter);
}
public void Buttons() {
btNext = new JButton ("Next");
btNext.setBounds(300,215,100,20);
panel.add(btNext);
btPrevious = new JButton ("Previous");
btPrevious.setBounds(190,215,100,20);
panel.add(btPrevious);
btSubmit = new JButton("Submit");
btSubmit.setBounds(80,215,100,20);
panel.add(btSubmit);
btSubmit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String userEntered = txtEnter.getText();
if(userEntered.equalsIgnoreCase("yes"))
{
//run your script
}
}
});
}
}
Your code appears fine.
Enter a print statement and you can see it is working.
btSubmit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// here the click happend so you can check your Textfield
String userEntered = txtEnter.getText();
System.out.println("User enterd: " + userEntered);
if(userEntered.equalsIgnoreCase("yes"))
{
System.out.println("Entered Yes");
}
}
});
I am trying to remove a JPanel not hide it but i can't find anything that works.
This is the code in the panel that needs to remove itself when a button is pressed:
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Frame frame = new Frame(); //referencing to my JFrame class (this class is a JPanel)
//need to remove this panel on this line
frame.ThreeD(); // adds a new panel
}
});
UPDATED
This is the full code:
package ThreeD;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;
import Run.Frame;
public class Launcher extends JPanel{
private JButton play, options, help, mainMenu;
private Rectangle rplay, roptions, rhelp, rmainMenu;
private int buttonWidthLocation, buttonWidth, buttonHeight;
private int width = 1280;
public Launcher() {
this.setLayout(null);
drawButtons();
}
private void drawButtons() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
play = new JButton("Play");
options = new JButton("Options");
help = new JButton("Help");
mainMenu = new JButton("Main Menu");
buttonWidthLocation = (width / 2) - (buttonWidth / 2);
buttonWidth = 80;
buttonHeight = 40;
rplay = new Rectangle(buttonWidthLocation, 150, buttonWidth, buttonHeight);
roptions = new Rectangle(buttonWidthLocation, 300, buttonWidth, buttonHeight);
rhelp = new Rectangle(buttonWidthLocation, 450, buttonWidth, buttonHeight);
rmainMenu = new Rectangle(buttonWidthLocation, 600, buttonWidth, buttonHeight);
play.setBounds(rplay);
options.setBounds(roptions);
help.setBounds(rhelp);
mainMenu.setBounds(rmainMenu);
add(play);
add(options);
add(help);
add(mainMenu);
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Frame frame = new Frame();
//need to remove this panel here
frame.ThreeD();
}
});
options.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("options");
}
});
help.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("help");
}
});
mainMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("mainMenu");
}
});
}
}
And this is my Frame class:
package Run;
import javax.swing.*;
import ThreeD.Display;
import ThreeD.Launcher;
import TowerDefence.Window;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Frame extends JFrame{
public static String title = "Game";
/*public static int GetScreenWorkingWidth() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
}*/
/*public static int GetScreenWorkingHeight() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
}*/
//public static Dimension size = new Dimension(GetScreenWorkingWidth(), GetScreenWorkingHeight());
public static Dimension size = new Dimension(1280, 774);
public static void main(String args[]) {
Frame frame = new Frame();
System.out.println("Width of the Frame Size is "+size.width+" pixels");
System.out.println("Height of the Frame Size is "+size.height+" pixels");
}
public Frame() {
setTitle(title);
setSize(size);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ThreeDLauncher();
}
public void ThreeDLauncher() {
Launcher launcher = new Launcher();
add(launcher);
setVisible(true);
}
public void TowerDefence() {
setLayout(new GridLayout(1, 1, 0, 0));
Window window = new Window(this);
add(window);
setVisible(true);
}
public void ThreeD() {
BufferedImage cursor = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Cursor blank = Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(0, 0), "blank");
getContentPane().setCursor(blank);
Display display = new Display();
add(display);
setVisible(true);
display.start();
}
}
Basically - you are creating new instance of Frame in line:
Frame frame = new Frame(); //referencing to my JFrame class (this class is a JPanel)
New instance of Frame is not visible, and you're try to remove your Launcher from not visible new Frame. But this is wrong - you should remove Launcher from Frame that you created previously in main function (that is: parent of Launcher component).
Here goes an example:
public class TestFrame extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TestFrame frame = new TestFrame();
frame.getContentPane().add(new MyPanel(frame));
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
And MyPanel class:
public class MyPanel extends JPanel {
public MyPanel(final TestFrame frame) {
JButton b = new JButton("Play");
add(b);
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Container pane = frame.getContentPane();
pane.remove(MyPanel.this);
JPanel otherPanel = new JPanel();
otherPanel.add(new JLabel("OtherPanel"));
pane.add(otherPanel);
pane.revalidate();
}
});
}
}
In your example you should add a reference to Frame in your Launcher constructor:
public Launcher(Frame frame) {
this.frame = frame;
...
Init Launcher:
public void ThreeDLauncher() {
Launcher launcher = new Launcher(this);
and use:
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//need to remove this panel here
frame.getContentPane().remove(Launcher.this);
frame.ThreeD();
}
});
Say your panel is myPanel you can remove it from the main frame by:
frame.getContentPane().remove(myPanel);