[英]How do I position and size JLabels in Java?
I want to be able to position my two JLabels but when I change the values in the position lines it does nothing. 我希望能够放置两个JLabel,但是当我更改位置线中的值时,它什么也不做。 Also when I run it only the second label is displayed. 同样,当我运行它时,仅显示第二个标签。
My code: 我的代码:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class cubeTimerClass {
public static void main(String[] args) {
window(); //Runs the window method
}
public static void window() {
//Create a window
JFrame window = new JFrame(); //Create the window object
window.setSize(900, 600); //Set the size of the window
window.setTitle("Cube Timer"); //Set the title of the window
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Tells the program to quit when user closes the window
window.setVisible(true); //Make the window visible
//Create a label
JLabel label1 = new JLabel(""); //Create the label1 object
label1.setText("Message 1"); //Set the text for label1
label1.setAlignmentX(0);
label1.setAlignmentY(0);
window.add(label1); //Place the label on the window
//Create a label
JLabel label2 = new JLabel(""); //Create the label2 object
label2.setText("Message 2"); //Set the text for label2
label2.setAlignmentX(0);
label2.setAlignmentY(50);
window.add(label2); //Place the label on the window
}
}
Check with this - 检查一下-
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = (JPanel) frame.getContentPane();
panel.setLayout(null);
JLabel label = new JLabel("Java");
panel.add(label);
Dimension size = label.getPreferredSize();
label.setBounds(90, 100, size.width, size.height);
frame.setSize(300, 200);
frame.setVisible(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.