【发布时间】:2016-03-03 01:23:27
【问题描述】:
我正在尝试设置 JTextField 的宽度,但没有一种方法可以改变它的大小我试图改变列的数量来改变 JTextField 的宽度,但它不起作用?
这是什么原因造成的?
我将如何更改宽度?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
public class main {
public static void main(String[] args) {
//Create and set up the window.
final JFrame frame = new JFrame("Layout 1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 1500, 1000);
frame.setBackground(new Color(255,255,255));
frame.setLayout(null);
Container pane = frame.getContentPane();
pane.setLayout(null);
JLayeredPane jlp = new JLayeredPane();
frame.setContentPane(jlp);
JPanel container = new JPanel();
//container.setBackground(new Color(241,241,241));
container.setBackground(Color.RED);
container.setBounds(0,0,frame.getWidth(), 75);
container.setLayout(null);
JPanel containerA = new JPanel();
containerA.setBackground(Color.blue);
containerA.setBounds(90 ,10,50,50);
JPanel containerB = new JPanel();
containerB.setBackground(Color.RED);
containerB.setBounds(95 ,20,50,50);
//This is the top textfield
JTextField textField = new JTextField(1000);
textField.setLayout(null);
textField.setPreferredSize(new Dimension(1000,1000));
textField.setBounds(0,0,2300,50);
textField.setColumns(1000);
textField.setSize(1000,10);
textField.invalidate();
textField.setMinimumSize(new Dimension(1000,100));
container.add(textField);
jlp.add(container, Integer.valueOf(2));
jlp.add(containerA, Integer.valueOf(1));
jlp.add(containerB, Integer.valueOf(3));
container.setVisible(true);
frame.invalidate();
frame.setVisible(true);
}
}
【问题讨论】:
-
我认为这是一个重复的问题,请参阅stackoverflow.com/questions/2897506/…了解更多详细信息。
-
你应该只在事件调度线程中操作你的组件
-
只是为了它,尝试将 PreferredSize 设置为与 MinimumSize 匹配,看看它是如何工作的。
-
从How to Use Text Fields 开始,了解创建使用布局管理器的结构更好的程序的基础知识。当您有效地使用布局管理器时,就无需尝试管理组件的大小。