gpt4 book ai didi

java - JTextField - setColumns() 方法对我不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 02:52:23 24 4
gpt4 key购买 nike

我的问题是我的 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/

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