Reputation: 11
The code was running just fine until i tried to re-position a JLabel and although it shows no errors in my code it refuses to run it and gives me the following error... I am trying to move the label to the upper left corner of the JFrame window and the method i used to do so should be perfectly fine. At least to my knowledge. [Beginner Java Dev]
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: verticalAlignment
at javax.swing.JLabel.checkVerticalKey(JLabel.java:627)
at javax.swing.JLabel.setVerticalAlignment(JLabel.java:713)
at io.Arimore.Launcher.createAndShowGUI(Launcher.java:27)
at io.Arimore.Launcher.access$0(Launcher.java:13)
at io.Arimore.Launcher$1.run(Launcher.java:46)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Code:
package io.Arimore;
import java.awt.*;
import javax.swing.*;
/* FrameDemo.java requires no other files. */
public class Launcher {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize(); //Gets the user screen size
int scrnHigh = d.height; //Pulls out the High
int scrnWide = d.width; //Pulls out the Wide
//Create and set up the window.
JFrame frame = new JFrame("Welcome to Arimore.io");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("Arimore.io - v0.01A");
emptyLabel.setPreferredSize(new Dimension(175, 100));
int quatrainsY = (d.height / 2);
int elabelH = (0 - quatrainsY);
int elabelW = (0 - (d.width / 2));
emptyLabel.setVerticalAlignment(elabelH);
emptyLabel.setHorizontalAlignment(elabelW);
frame.getContentPane().add(emptyLabel);
//Display the window.
frame.pack();
//setSize(scrnWide *9/10, scrnHigh *9/10);//sets screen to 0.9 size
frame.setSize(scrnWide, scrnHigh);//sets screen to FULL size
//setLocation(scrnWide *1/20, scrnHigh *1/20);//CENTER it for 9/10th size
frame.setLocation(0, 0);//
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Upvotes: 1
Views: 73
Reputation: 216
JLabel.setVerticalAlignment takes an int, but the int values should be JLabel.TOP, JLabel.MIDDLE, or JLabel.BOTTOM.
Also, JLabel.setHorizontalAlignment will take JLabel.LEFT, JLabel.CENTER, and JLabel.RIGHT.
Any other values will cause an illegal argument exception.
Upvotes: 3