gpt4 book ai didi

java - JTextfield 不像我想要的那样显示

转载 作者:行者123 更新时间:2023-12-04 16:24:11 25 4
gpt4 key购买 nike

我是 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, 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);
}
}

The issue is shown here

最佳答案

你可以通过这种方式给 JTextField 一个大小:

input = new JTextField(20);

来自documentation :

The integer argument passed to the JTextField constructor, 20 in theexample, indicates the number of columns in the field. This number isused along with metrics provided by the field's current font tocalculate the field's preferred width. It does not limit the number ofcharacters the user can enter.

关于java - JTextfield 不像我想要的那样显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68594431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com