JTextField-setColumns()方法不适用于我

cworner1:

我的问题是我的JTextField -setColumns(int)

“ field1.setColumns(5);”

...不起作用。我猜它的布局管理器问题。但是,我正在从练习本中学习。我知道的唯一布局是flowLayout,borderLayout和gridlayout。

简而言之,要解释此代码,无论何时“ field2”触发ActionEvent(通过按Enter键),“ field1”都应更改大小。

我已经在“ actionPerformed”中放置了一个“ System.out.println(“已检测到ActionEvent”),以证明正在触发一个actionevent,所以这不是问题。我什至打印了“ 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请求(除去任何视觉伪像)来实现。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章