When creating an AWT Frame in a Java program running on MacOS, the maximize button in the frame's title bar maximises the window:
public static void main(String args[]) {
new Frame();
}
When I create a Swing JFrame instance instead, the same button toggles the window into fullscreen mode, which is the standard behaviour on MacOS. The button even looks different visually when I hover over it:
public static void main(String args[]) {
new JFrame();
}
Is there any way how I can replicate JFrame's "maximize to fullscreen" behaviour with AWT's Frame? I scanned the code of the entire JFrame class and wasn't able to identify any logic that would toggle the Frame's behaviour in such a way.
Try this code :
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
public class MyFrame extends Frame {
MyFrame() {
for (Window w : Window.getWindows()) {
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().setFullScreenWindow(w);
}
}
public static void main(String args[]) {
MyFrame f = new MyFrame();
f.setVisible(true);
}
}
Related
I am looking for some sort of WindowMotionListener or something of that sort, so when I drag a window by the title bar (or otherwise) I can get location information the same way I would with a MouseMotionListener. I'm trying to make a Swing component that works sort of like MFC does, where you can drag a window into a location and have it snap into it (example: https://imgur.com/LWSXv9x).
So far, I've gotten the snapping down and the dragging when the user drags the frame by it's contents, but not by title bar which would be much easier for the end user. Is there any way to do get a sort of window drag event? Would I have to make my own windows decorations and add a "drag" even to them?
Any help would be greatly appreciated
I want to get events when the window is moved. ... my main issue is that there is no way to determine if a user is dragging a jframe around
Well, what about a ComponentListener on the JFrame?...
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Main {
private static void createAndShowGUI() {
final JFrame frame = new JFrame("Drag frame");
frame.addComponentListener(new ComponentAdapter() {
#Override
public void componentMoved(final ComponentEvent e) {
System.out.println("New frame location: " + frame.getLocation());
}
#Override
public void componentResized(final ComponentEvent e) {
System.out.println("New frame size: " + frame.getSize());
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JLabel("Drag the title bar and see the logs..."));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(Main::createAndShowGUI);
}
}
I am using Ubuntu and when running basic Swing program the output is not perfectly visible. The output could not see the components like label or button.The code is
import javax.swing.*;
import java.io.*;
public class Swing1 extends JFrame
{
public static void main(String args[])
{
JFrame jr=new JFrame("Hello World!");
JLabel l=new JLabel("FirstOne");
JButton b=new JButton("End");
// l.setBackground(Color.BLACK);
jr.add(l);
jr.add(b);
jr.setVisible(true);
jr.setSize(800,300);
jr.setLayout(null);
jr.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
I am getting output screen but label not displayed nor button my os is ubuntu.
You should not use jr.setLayout(null), because that means you would have to use absolute positioning.
You can look at Java's available Layout Managers to see which layout is appropriate for you.
You can use a FlowLayout, like jr.setLayout(new FlowLayout()), and that way your label and button will be visible.
import javax.swing.*;
import java.io.*;
import java.awt.*; //So you can use the Layout Managers
public class Swing1 extends JFrame
{
public static void main(String args[])
{
JFrame jr=new JFrame("Hello World!");
JLabel l=new JLabel("FirstOne");
JButton b=new JButton("End");
jr.add(l);
jr.add(b);
jr.setVisible(true);
jr.setSize(800,300);
jr.setLayout(new FlowLayout());
jr.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
So I'm making a game and It's working quite well, but I just need to have a start menu with 3 buttons. A Play, Instructions and Exit button. The problem is that I want it in a different frame than the game itself, if you press on Start the frame should switch to the game frame. Can someone help me with this?
This is a part of my code:
package frametest;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;
public class FrameTest extends JFrame implements ActionListener {
public JPanel createContentPane() {
//Jpanels and stuff
}
public JPanel createMainMenu() {
//Start menu JPanels and stuff
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Poker Game");
FrameTest demo = new FrameTest();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1080,640);
frame.setVisible(false);
JFrame menu = new JFrame("Poker Game");
menu.setContentPane(demo.createMainMenu());
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu.setSize(1080,640);
menu.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
You must have a setting frame class for your main function and essential for frame and you should have another class for game controlling and a class for menu.
You should get instant of game controller in frame and do the same in controller for menu then you can have separate frame that you want.
please use interfaces to have a clean code.
I have a weird problem with modal dialogs and undecorated JFrame.
If I create a main undecorated JFrame then I display a modal dialog thanks to the JOptionPane, everything goes well. The modal dialog stays always on top and I can't click on the main fame.
But, if create another JFrame (or another JDialog), the modal dialog still prevent me to interact with the main frame, but now the modal dialog is not always on top and go underneath the main frame when I click on it.
This problem doesn't happen:
if the main frame is decorated
or if the second frame is not visible
EDIT
I use jdk1.7.0.0_09 on Linux Suse.But I have the same result with jre 1.6.0_32
The code I used to test:
public static void main(String[] args) {
// creates main frame and set visible to true
final JFrame mainFrame = new JFrame();
mainFrame.setUndecorated(true); // if I comment this line, everything goes well
mainFrame.add((new JPanel()).add(new JButton("OK")));
mainFrame.setSize(new Dimension(500, 500));
mainFrame.setVisible(true);
// creates a dialog and set visible to true
JFrame anotherFrame = new JFrame();
anotherFrame.setVisible(true); // or if I comment this line, everything goes well too
// display a modal dialog
JOptionPane.showMessageDialog(mainFrame, "A message");
}
But, if create another JFrame (or another JDialog), the modal dialog
still prevent me to interact with the main frame, but now the modal
dialog is not always on top and go underneath the main frame when I
click on it.
not true at all, both are not accesible untill JOptioPane is visible
JOptionPane or JDialod.setModal(true) block mouse or key events to the alll windows invoked from currnet JVM
there must be something else that isn't clear from your question, rest of code, minor could be Java version and Native OS
code for Java6 (winxp), works for me on Win7 / Java7(x.x_011)
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Main {
private JFrame mainFrame = new JFrame();
private JFrame anotherFrame = new JFrame();
public Main() {
mainFrame.setUndecorated(true);
mainFrame.add((new JPanel()).add(new JButton("OK")));
mainFrame.setSize(new Dimension(100, 60));
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
anotherFrame.setVisible(true);
anotherFrame.setLocation(110, 0);
anotherFrame.setSize(new Dimension(100, 60));
anotherFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(mainFrame, "A message");
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Main main = new Main();
}
});
}
}
I originally developed the following code on Win XP. When you click the program icon in the XP taskbar, the parent frame remains iconified and the JDialog returns to focus, which is the behavior I want. However, when clicking the program's taskbar icon on Win 7, the parent JFrame changes its state back to Normal and shows behind the app-modal JDialog. I've tried overriding the JFrame's setExtendedState() method to intercept the frame's state change with no luck.
Is there a workaround for this, or is there a flaw my logic I need to address?
import java.awt.*;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class TestLogin extends JFrame {
public TestLogin() {
this.setSize(300, 300);
iconify(this);
setLocationRelativeTo(null);
this.setTitle("I'm a Frame!");
this.setVisible(true);
LoginScreen login = new LoginScreen(this);
}
public static void main(String [] args) {
TestLogin frame = new TestLogin();
}
public static void iconify(Frame frame) {
int state = frame.getExtendedState();
// Set the iconified bit
state |= Frame.ICONIFIED;
// Iconify the frame
frame.setExtendedState(state);
}
public static void deiconify(Frame frame) {
int state = frame.getExtendedState();
// Clear the iconified bit
state &= ~Frame.ICONIFIED;
// Deiconify the frame
frame.setExtendedState(state);
}
public class LoginScreen extends JDialog {
private JFrame root;
public LoginScreen(JFrame root) {
super(root);
this.root = root;
setLocationRelativeTo(null);
this.setTitle("I'm a Dialog!");
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
this.setSize(200, 200);
setVisible(true);
}
}
}
It looks like a bug in the java paradigm of "write once, run anywhere". If that is to include windows 7, then you might contact oracle and fill a bug report.
Regards,
Stéphane