- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的问题是我的 JTextField -setColumns(int)
“field1.setColumns(5);”
...不起作用。我猜这是一个布局管理器问题。但是,我正在从一本练习册中学习。我知道的唯一布局是 flowLayout、borderLayout 和 gridlayout。
简而言之,只要“field2”触发 ActionEvent(通过按回车键),“field1”就应该改变大小。
我在“actionPerformed”中放置了一个“System.out.println(”ActionEvent detected”) 以证明正在触发一个 Action 事件,所以这不是问题。我什至打印了“field1.getColumn "并且它显示了正确的更改值 5,但是......它不仅在运行时没有明显改变大小。
我希望有人能解释这个问题,而不是解决问题。变通并不能帮助我学习,而这正是解决这些书本练习的全部目的。
如果它很重要,我将使用 netbeans 进行编码。预先感谢您的帮助。
public class Exercise13_11 extends JFrame implements ActionListener
{
private JTextField textField1, textField2;
private JLabel label1, label2;
private JRadioButton rButton1, rButton2, rButton3;
public static void main(String[] args)
{
JFrame frame = new Exercise13_11();
frame.setTitle("Exercise 13.11");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(450, 200);
frame.setVisible(true);
}
public Exercise13_11()
{
// North Panel aligned and filled.
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
northPanel.add(label1 = new JLabel("Text Field"));
northPanel.add(textField1 = new JTextField(20));
northPanel.setToolTipText("Demonstrate JTextField");
getContentPane().add(northPanel, BorderLayout.CENTER);
// South panel now being filled...
JPanel southPanel = new JPanel();
southPanel.setLayout(new FlowLayout());
JPanel alignmentPanel = new JPanel();
alignmentPanel.setBorder(
new javax.swing.border.TitledBorder("Horizontal Alignment"));
alignmentPanel.add(rButton1 = new JRadioButton("Left"));
alignmentPanel.add(rButton2 = new JRadioButton("Center"));
alignmentPanel.add(rButton3 = new JRadioButton("Right"));
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(rButton1);
buttonGroup.add(rButton2);
buttonGroup.add(rButton3);
JPanel columnPanel = new JPanel();
columnPanel.setBorder(new javax.swing.border.EtchedBorder());
columnPanel.setLayout(new FlowLayout());
columnPanel.add(label2 = new JLabel("Column Size"));
columnPanel.add(textField2 = new JTextField(10));
southPanel.add(alignmentPanel);
southPanel.add(columnPanel);
getContentPane().add(southPanel, BorderLayout.SOUTH);
textField1.addActionListener(this);
rButton1.addActionListener(this);
rButton2.addActionListener(this);
rButton3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == textField1)
{
textField1.setColumns(5);
}
else if (e.getSource() == rButton1)
textField1.setHorizontalAlignment(textField1.LEFT);
else if (e.getSource() == rButton2)
textField1.setHorizontalAlignment(textField1.CENTER);
else if (e.getSource() == rButton3)
textField1.setHorizontalAlignment(textField1.RIGHT);
}
最佳答案
它的工作,你只需要强制容器重新布局它的组件。这可以通过调用 revalidate
来实现。然后发出 repaint
请求(以移除任何视觉伪像)。
关于java - JTextField - setColumns() 方法对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8885860/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!