How is JFrame.setResizable supposed to be used? - java

I'm trying to followed java tutorials and now I am going over JFrame.
This is a information inquiry more than help question.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Login {
public static void main(String[] args){
//Creating object of LoginFrame class and setting some of its properties
LoginFrame frame = new LoginFrame();
frame.setTitle("LoginForm");
frame.setVisible(true);
frame.setBounds(10, 10, 370, 600);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
This code will cause the frame to be resized to a very small size at the top left corner regardless of the bounds I set.
A simple fix for this is to place frame.setResizable() before setting its bounds.
Does anyone know why this happens or am I doing something wrong?
I'm also on Ubuntu 20.04, maybe this matters but I haven't found an answer.
Tutorial shows above code.
The following is the code for LoginFrame
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//Creating LoginFrame class
public class LoginFrame extends JFrame implements ActionListener {
//Creating constructor of LoginFrame() class
LoginFrame(){
}
//Overriding actionPerformed() method
#Override
public void actionPerformed(ActionEvent e){
}
}
Like I was saying I was only following a tutorial. This was only the beginning of the tutorial but I had the same issue when starting another very simple frame tutorial.

The following works fine for me :
import javax.swing.*;
class Scratch extends JFrame {
public Scratch() {
super();
}
public static void main(String[] args){
//Creating object of LoginFrame class and setting some of its properties
Scratch frame = new Scratch();
frame.setTitle("LoginForm");
frame.setVisible(true);
frame.setBounds(10, 10, 370, 600);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Result : I see a big rectangular window - in the shape of a smart phone screen I'd say.
setResizable(false) means you cannot resize the frame. I suspect the problem you're trying to identify lies somewhere in the LoginFrame class... no code for this was included though so hard to comment furhter.

Related

JPanel filling whole window

I'm trying to make chess in java, but I'm stuck on the panel's size and location. When I try to change any of them, it does nothing and I don't know why. Could someone explain it to me?
package Chess;
import javax.swing.*;
import java.awt.*;
public class Window extends Component{
public void addChessWindow() {
JFrame chessWindow = new JFrame("Chess");
chessWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chessWindow.setResizable(false);
chessWindow.setExtendedState(JFrame.MAXIMIZED_BOTH);
chessWindow.setUndecorated(true);
chessWindow.setBackground(Color.decode("#4D6713"));
chessWindow.setVisible(true);
JPanel field = new JPanel();
field.setBackground(Color.BLACK);
field.setBounds(30,15,2,1);
chessWindow.add(field);
}
}
Note: everything starts in the main method.
I think you could do something like this:
chessWindow.add(field, BorderLayout.CENTER);

Using JFrame in java

Here is my code, I am wondering why it prints "test" two times!? every command I add in "paintcomponent" performs 2 times. I would appreciate if you could help me please!?
import java.awt.geom.*;// For Ellipse2D, etc.
import java.util.*;
import javax.swing.*; // For JPanel, etc.
import java.io.*;
import java.awt.*; // For Graphics, etc.
import java.lang.Object;
import java.util.Random;
public class hextopology extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("test");
}
public static void main(String[] args) throws Exception{
JFrame f = new JFrame();
f.add(new hextopology());
f.setSize(550,550);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
When you are doing resizing the window you are actually changing the window's properties, so your view elements should be painted again. That's why paintComponent() is getting called every time and as you have a print statement inside the method, it is printing as expected.
You are calling
f.setSize(550,550);
which is a resize, after the panel is created for the first time.
paintComponent
is called twice, because of this.

Shapes not showing up

I'm trying to make some shapes filled with colors. The shapes doesn't show up!
Somebody Help Please!
I have two classes "menu.java" and "draw.java"
Here is my code for the "menu.java"
import javax.swing.JFrame;
public class menu {
public static void main(String[] args) {
JFrame JF = new JFrame("Menu Bar");
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
draw DR = new draw();
JF.add(DR);
JF.setSize(500,300);
JF.setVisible(true);
JF.setLocationRelativeTo(null);
}
}
The code for "draw.java"
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class draw extends JPanel{
public void painComponent(Graphics GPHCS){
super.paintComponent(GPHCS);
this.setBackground(Color.WHITE);
GPHCS.setColor(Color.BLUE);
GPHCS.fillRect(25,25,100,30);
GPHCS.setColor(Color.GRAY);
GPHCS.fillRect(25,65,100,30);
GPHCS.setColor(new Color(190,81,215));
GPHCS.drawString("This is my text", 25, 120);
}
}
Here is a screenshot after running the program
Why does the shapes not showing up?!
Any answers would be appreciated. Thanks
The method is called paintComponent, not painComponent. So the method paintComponent does not get overridden as intended.
Use #Override tag before method to get notified of errors like these.

Transitions between states in a simple Java game

I am creating a simple slide-scrolling game which should have a user menu with two buttons (both are custom-made images) on launch - Play and Exit. The problem is that I don't how to link the first (Play) option to the frame containing the actual program (see code below) - i.e. when pressed, the game itself begins. I have the same issue with the Exit possibility.
Looking forward to any suggestions concerning the implementation of active buttons and smooth transitions.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Frame {
public Frame(){
JFrame frame = new JFrame();
frame.add(new Board());
frame.setTitle("2-D Test Game");
Image im = Toolkit.getDefaultToolkit().getImage("img.png");
frame.setIconImage (im);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,500);
frame.setVisible(true);
}
public static void main(String[] args){
new Frame();
}
}

Prevent JWindow from appearing on top of all windows

I am using JWindow in my project to display a UI that is undecorated and also doesn't appear in the task bar. But, the JWindow always seems to be on top of all other windows. I tried setting the setAlwaysOnTop to false, but it didn't seem to help.
Here's the code that can reproduce the problem :
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JWindow;
public class Test extends JWindow implements ActionListener {
public Test() {
setSize(300, 300);
setLocationRelativeTo(null);
setAlwaysOnTop(false);
JButton myButton = new JButton("Click Here");
myButton.addActionListener(this);
getContentPane().add(myButton);
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Click Here"))
JOptionPane.showMessageDialog(this, "This dialog box appears behind the JWindow!");
}
}
My OS is Linux and I'm using the Oracle JDK 6. Also, while I was testing my app on Windows, I was using JDialog for the UI and it was working fine. But, in Linux JDialog seems to appear in the task bar.
Any help as to how to solve this?
After you set the visibility of the window to True, you send it to the back like this:
setVisible(true);
toBack();
If, later, you want to bring it to the top of the stacking order, you simply call:
toFront();
More details here:
http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#toBack()
http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#toFront()

Categories