代码运行得很好,直到我试图重新定位JLabel,虽然它在我的代码中没有显示任何错误,但它拒绝运行它并给我以下错误...我试图将标签移动到JFrame窗口的左上角和我以前使用的方法应该完全没问题。至少据我所知。 [初学者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)
代码:
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();
}
});
}
}
答案 0 :(得分:3)
JLabel.setVerticalAlignment采用int,但int值应为JLabel.TOP,JLabel.MIDDLE或JLabel.BOTTOM。
此外,JLabel.setHorizontalAlignment将采用JLabel.LEFT,JLabel.CENTER和JLabel.RIGHT。
任何其他值都会导致非法参数异常。