如何解决JTextfield 没有像我想要的那样显示
我是 Java 新手,找不到简单的解决方案。在下图中,您可以看到我的问题所在。我想编写一个程序,在那里我可以创建播放列表并保护它们,但我已经陷入困境,因为 JTextfield
太小了。
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
JFrame w = new Layout();
w.setVisible(true);
w.setSize(600,600);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setLocationRelativeTo(null);
w.setResizable(true);
}
}
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Layout extends JFrame{
JTextField input;
Layout(){
//title and layout type
super("ThePlay");
setLayout(new FlowLayout());
JButton s; //search button
JButton h; //home button
JButton mp; //my playlist button
JButton cp; //create playlist button
JButton fs; //favorite songs button
//frame constructor
Container mainContainer = this.getContentPane();
mainContainer.setLayout(new BorderLayout(8,6));
mainContainer.setBackground(Color.GRAY);
this.getRootPane().setBorder(BorderFactory.createMatteBorder(4,4,Color.GRAY));
//input constructor
input = new JTextField();
input.setBounds(300,50,150,25);
//up frame
JPanel topPanel = new JPanel();
topPanel.setBorder(new LineBorder(Color.BLACK,3));
topPanel.setBackground(Color.GRAY);
topPanel.setLayout(new FlowLayout(6));
//bot frame
JPanel downPanel = new JPanel();
downPanel.setBorder(new LineBorder(Color.BLACK,3));
downPanel.setBackground(Color.GRAY);
downPanel.setLayout(new FlowLayout(5));
//create buttons
s = new JButton("Search");
h = new JButton("Home");
mp = new JButton("My Playlists");
cp = new JButton("Create Playlist");
fs = new JButton("Favorite Songs");
//add buttons to the frame
mainContainer.add(topPanel,BorderLayout.NORTH); //top frame location
mainContainer.add(downPanel,BorderLayout.CENTER); //bottom frame location
topPanel.add(input);
topPanel.add(s);
topPanel.add(h);
topPanel.add(mp);
topPanel.add(cp);
topPanel.add(fs);
}
}
解决方法
您可以通过这种方式为 JTextField
指定大小:
input = new JTextField(20);
传递给 JTextField 构造函数的整数参数,在 例如,表示字段中的列数。这个数字是 与字段当前字体提供的指标一起使用 计算字段的首选宽度。它不限制数量 用户可以输入的字符。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。