Anc*_*end 3 java swing tooltip layout-manager
我正在尝试创建一个模拟记录存储的简单GUI.我还处于起步阶段.
当我尝试添加文本来描述用户期望在文本字段中输入的内容时,我遇到了麻烦.
另外,我也无法将每个文本字段放在自己的行上.换句话说,如果一行中有两个文本字段的空间,那么它将显示在一行中,并且我试图在其自己的行上显示每个文本字段.
这是我到目前为止所尝试的:
item2 = new JTextField("sample text");
Run Code Online (Sandbox Code Playgroud)
但是上面的代码只是在文本字段中添加了默认文本,这不是我需要的:/
我提前感谢所有的帮助.
public class MyClass extends JFrame{
private JTextField item1;
private JTextField item2;
public MyClass(){
super("Matt's World of Music");
setLayout(new FlowLayout());
item1 = new JTextField();
item2 = new JTextField();
add(item1);
add(item2);
thehandler handler = new thehandler();
item1.addActionListener(handler);
item2.addActionListener(handler);
}
}
Run Code Online (Sandbox Code Playgroud)
对于第一个问题,您需要使用JLabel来显示文本.构造函数是这样的:
JLabel label = new JLabel("Your text here");
Run Code Online (Sandbox Code Playgroud)
在GUI中工作得很好.
至于自己的东西,我推荐一个GridLayout.使用方便.
在构造函数中,在添加任何内容之前,您可以:
setLayout(new GridLayout(rows,columns,x_spacing,y_spacing));
Run Code Online (Sandbox Code Playgroud)
x_spacing并且y_spacing都是用于水平和垂直确定元素之间空间的整数.
然后添加就像你做的那样.摆弄它,你会得到它.
所以你的决赛看起来像:
setLayout(new GridLayout(2,2,10,10));
add(new JLabel("Text 1"));
add(text1);
add(new JLabel("text 2"));
add(text2);
Run Code Online (Sandbox Code Playgroud)