How to make a proper JFrame in Java Language? - java

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();
}

Related

Java AWT Stops listening to input after random lengths of time

(Sorry I can't get a working E.g. of the issue, I need help for my help!)
I'm in the process of creating a custom game engine and have come upon an issue whereby while the game is running - the game stops taking input
I've checked and the program seems to continue running in the background. It also doesn't seem to vary with different machines
(My main device is a Mac Book Pro 2011)
import java.awt.event.*;
import java.io.IOException;
import java.awt.*;
import javax.swing.*;
public class Focus extends JFrame implements KeyListener {
private static final long serialVersionUID = 1L;
char currKey = '\0';
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {public void run() {new UIManager();}});
}
public Focus() throws IOException {
Container contentPane = getContentPane();
contentPane.add(new DrawCanvas());
addKeyListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setFocusable(true);
setVisible(true);
}
private class DrawCanvas extends JPanel {
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics pen) {
//Drawloop
if(currKey == 'k') {
//This is the code that randomly stops running
System.out.println("Yo");
}
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
#Override
public void keyTyped(KeyEvent e) {
currKey = e.getKeyChar();
}
#Override
public void keyPressed(KeyEvent e) {
currKey = '\0';
}
#Override
public void keyReleased(KeyEvent e) {
}
}
The code looks right to me (But then, it always does) and the only possible tripping point is that AWT is Instantiated in Main, Run in UIManager and the movement code resides in player though I don't know enough about AWT to know whether this would be the case and relocating the code in a backup lead to a program crash. Any help would be greatly appreciated.
Turns out it was an issue with Java and MacOS key repeats- fixed in a newer Java version
To fix update java

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
}
}

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.

Open another class in 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

Categories

Resources