我目前正在开展一个项目,并且我试图将我的标签设置在某个地方,这就是我的代码目前的样子;
import javax.swing.*;
import java.awt.*;
public class First extends JFrame
{
private JLabel label;
private JTextField textfield;
JLabel ZKey;
public First()
{
ZKey=new JLabel("ZKey");
ZKey.setLayout(null);
ZKey.setLocation(55, 55);
add(ZKey);
}
public static void main (String args[])
{
First gui = new First ();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(500,500);
gui.setVisible(true);
gui.setTitle("Keep Track Of What You Press");
}
}
我觉得我错过了一些相当愚蠢的东西。
答案 0 :(得分:0)
使用JPanel,将JLabel添加到面板,将面板添加到JFrame。标签现在显示在顶部和中心:
import javax.swing.*;
import java.awt.*;
public class First extends JFrame
{
private JLabel label;
private JTextField textfield;
JLabel ZKey;
JPanel panel;
public First()
{
panel=new JPanel();
ZKey=new JLabel("ZKey");
ZKey.setLayout(null);
ZKey.setLocation(55, 55);
panel.add(ZKey);
add(panel);
}
public static void main (String args[])
{
First gui = new First ();
//gui.setLayout(null);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(500,500);
gui.setVisible(true);
gui.setTitle("Keep Track Of What You Press");
}
}