我希望能够定位我的两个JLabel但是当我更改位置线中的值时它什么都不做。此外,当我运行它时,只显示第二个标签。
我的代码:
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
}
}
答案 0 :(得分:0)
检查 -
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);