Open another class in java - java

I recently started learning some java, and I am facing a problem at this moment.
When an if statement is completed, I want a JFrame from another class to pop up.
So I would like something like this:
if(...)
{
//open the JFrame from the other class
}
My JFrame class looks like this:
import javax.swing.*;
public class Game {
public static Display f = new Display();
public static int w = 600;
public static int h = 400;
public static void main(String args[])
{
f.setSize(w, h);
f.setResizable(true);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Frame Test");
}
}
class Display extends JFrame
{
}

Something like this would work:
if(...)
{
//open the JFrame from the other class
OtherClass otherClass = new OtherClass();
otherClass.setVisible(true);
}
You should also initialise your JFrames in the constructor, otherwise your main method will get messy

Related

Passing objects to a gui in java

I'm currently in a java class and I'm trying to work on build a my first interface. The one below is not the one I'm working on, but a test one so I can figure out how to do what I want to do. Here is the code, the question will follow:
package test;
import java.awt.*;
import java.awt.event.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.*;
/**
*
* #author StrifeX
*/
public class Test {
public static class buttonTest extends JFrame {
private int number;
private JButton push;
public ButtonTest(){
setLayout(new GridBagLayout ());
//Creates an instance of the layout
GridBagConstraints panel = new GridBagConstraints();
// Establishes the pixels suronding each object within the layout
panel.insets = new Insets (5, 5, 5, 5);
//create the withdraw button and its properties
push = new JButton("push");
panel.fill = GridBagConstraints.HORIZONTAL;
panel.gridx = 0;
panel.gridy = 0;
panel.gridwidth = 1;
add(push, panel);
MyEvent buttonClick = new MyEvent();
push.addActionListener(buttonClick);
}//end constructor
public class MyEvent implements ActionListener {
public void actionPerformed (ActionEvent buttonClick){
String operation = buttonClick.getActionCommand();
if(operation.equals("Withdraw")){
JOptionPane.showMessageDialog( null, "test" );
}
}
}
}//end button test
public static class TestObject{
int testField;
public void testObject(){
testField = 10;
}
public void setterMethod(int newInt){
this.testField = newInt;
}
public int getterMethod(){
return this.testField;
}
}
public static void main(String[] args)
{
TestObject obj1 = new TestObject();
buttonTest button = new buttonTest();
// Establish basic parameters for the GUI
button.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setLocationRelativeTo(null);
button.setVisible(true);
button.setSize(450, 350);
button.setTitle("Change the Balance");
}
}
What I want to do is build an object, then use that objects methods with the GUI button. I can build the object, and pass it to the constructor that make the GUI (buttonTest), but the Event class cant see that object, and from the little I know, I can't pass it to the event class, but maybe the actionPerformed method like this:
public void actionPerformed (ActionEvent buttonClick, testObject thing)
However, when I tried to do that, I received and error that said
buttonTest.event is not abstract and does not override abstract method
actionPerformed(actionEvent) in ActionListener.
Any help would be greatly appreciated. Thank you.
The problem is wrongly defined the following:
public void actionPerformed (ActionEvent buttonClick, testObject thing)
The correct definition will be:
public void actionPerformed (ActionEvent buttonClick)
Getting passed all your previous issues and getting to the heart of the question
Passing objects to a gui in java
You basically need to pass the TestObject to all the classes which need access to it.
Start by modifying your MyEvent class to take a reference to TestObject
public class MyEvent implements ActionListener {
private TestObject obj;
public MyEvent(TestObject obj) {
this.obj = obj;
}
#Override
public void actionPerformed(ActionEvent buttonClick) {
String operation = buttonClick.getActionCommand();
// Now you have acces to obj
if (operation.equals("Withdraw")) {
JOptionPane.showMessageDialog(null, "test");
}
}
}
This will require you to modify your ButtonTest class to allow it to pass the TestObject through to the event handler...
public static class ButtonTest extends JFrame {
//...
public ButtonTest(TestObject obj) {
//...
MyEvent buttonClick = new MyEvent(obj);
//...
And finally, you create and pass the object to the instance of ButtonTest which needs it
public static void main(String[] args) {
TestObject obj1 = new TestObject();
ButtonTest button = new ButtonTest(obj1);
// Establish basic parameters for the GUI
button.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setLocationRelativeTo(null);
button.setVisible(true);
button.setSize(450, 350);
button.setTitle("Change the Balance");
}

java.AWT - setSize() method

I am facing an issue using setSize() method in the below program.
Error : The method setSize(int,int) is not defined for the type frame.
When I see Java API, "Class Frame" has this Method inherited from class
java.awt.Window. As i have instantiated the Frame class, this object should have setSize() method as Frame is derived class of Window.
Why am I getting this error then? How can a derived class doesnt contain its superclass method?
public class AwtPrac{
public static void main(String[] args) {
Frame fm = new Frame("Java Programm");
Button b= new Button ("Click Here");
fm.add(b);
fm.setVisible(true);
fm.SetSize(300,300);
fm.dispose();
}
}
Take this code
import java.awt.Frame;
public class AwtPrac {
private static void init(){
Frame fm = new Frame("Java Programm");
fm.setTitle("AwtPrac");
fm.setSize(300,300);
fm.setVisible(true);
}
public static void main(String[] args) {
init();
}
}

Creating JFrame in an instance object

I'm trying to display a countdown and I'm searching how to do it and trying codes out but that's not what I'm here to ask in this question, although I'd be happy if you helped me in that area too.
This seems a bit elementary but I can't seem to get the JFrame showing.
I predicted that if I create an instance of the testmain and there's a creation of a JFrame in the constructor, it'd show the JFrame.
I even tried getting an input from the keyboard so that it'll stop.
But nothing happens and the program ends right away.
It says build successful.
What am I missing?
public class testmain
{
Timer t;
JLabel label;
public void testmain()
{
JFrame myFrame = new JFrame();
label = new JLabel();
myFrame.setSize(400, 400);
myFrame.setAlwaysOnTop(true);
myFrame.setLocationRelativeTo(null);
label.setText("This works");
myFrame.add(label);
myFrame.setVisible(true);
// Scanner keyboard = new Scanner(System.in);
// keyboard.nextInt();
// start();
}
void start()
{
t = new Timer(1000, new TimeTest());
}
class TimeTest implements ActionListener
{
private int counter = 0;
#Override
public void actionPerformed(ActionEvent e)
{
label.setText("" + counter++);
if(counter == 10)
t.removeActionListener(this);
}
}
public static void main(String[] args)
{
testmain tester = new testmain();
}
}
You've got a pseudo-constructor which is not being called. Constructors have no return type, not void, not anything.
Change
// this never gets called
public void testmain() {
}
to
// but this **will** be called
public testmain() {
}
As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.
So the class should actually be called TestMain:
public class TestMain {
public TestMain() {
// constructor code here
}
}

How to make a proper JFrame in Java Language?

Hello community! I am very new to Java language and need help to make JFrame
work. I believe that the code posted below is wrong as it terminates. I am using the text editor Eclipse. Please help as I cannot find what the problem is. Thanks!
import javax.swing.JFrame;
public class Game extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public void run(){
setVisible(true);
setTitle("Good");
setSize(900,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
}
}
In your case main() method should look like this:
public static void main(String[] args) {
Game game = new Game();
game.run();
}

java temperature conversion in GUI

im struggling again, with doing a program in java that converts the temperature from celsius to fahrenhiet, but it must be done in a GUI so that the user may enter in a number for celsius and click a botton to convert it. ive been modeling it off of a example in my book however in the book it shows it working with out having a static main(), but my IDE gave me an error saying that it was needed, so ive added a static main() and ive tried calling the tempwindow() to see if that would work but still nothing and even if i comment out the call it doesnt give me a error but nothing happens.
im hopping someone can help show me what im doing wrong and how i should go about this.
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.event.*;
public class tempcon extends JFrame
{
private JPanel panel;
private JLabel messageLabel;
private JTextField tempC;
// private JRadioButton tempF;
// private ButtonGroup radioButtonGroup;
private JButton calcButton;
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 100;
public tempwindow()
{
setTitle("Temurture convertion");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}
private void buildPanel()
{
messageLabel = new JLabel("enter tempurture in celsius");
tempC = new JTextField(10);
calcButton = new JButton("convert");
calcButton.addActionListener(new CalcButtonListener());
panel = new JPanel();
panel.add(messageLabel);
panel.add(tempC);
panel.add(calcButton);
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input;
double temp;
input = tempC.getText();
temp = Double.parseDouble(input) * 1.8 + 32;
JOptionPane.showMessageDialog(null, "that is " + temp + "degrees fehrenhiet");
}
}
public static void main(String[] args)
{
tempwindow();
}
}
You are doing a few things wrong:
Your constructor can't have a different name from your class name
You need to instantiate the object to call the constructor, rather instantiating an object does call the constructor but you can't just access it like a method.
You should use the Java naming conventions for class names.
The class:
public class TempCon extends JFrame
{
// Variable declarations
public TempCon() // Constructor should match the Class name
{
}
}
The main class:
public static void main(String[] args)
{
TempCon converter = new TempCon();
}
public static void main(String[] args)
{
tempcon myTempWindowInstance = new tempcon();
myTempWindowInstance.tempwindow();
}
You never initialize a tempcon. Your constructor must have the same name as the class, so I recommend the following changes instead:
Replace public tempwindow() with public tempcon() to correct the constructor.
public tempcon()
{
setTitle("Temurture convertion");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}
create an instance of tempcon using new, which calls the constructor:
public static void main(String[] args)
{
tempcon myTempWindowInstance = new tempcon();
}
please write return type your function like.
public void tempwindow()
create object of class and call method.
public static void main(String[] args)
{
tempcon t=new tempcon();
t.tempwindow();
}
and learn java object java object oriented programming.

Categories

Resources