JTextField-setColumns()方法不适用于我 [英] JTextField - setColumns() method not working for me

查看:296
本文介绍了JTextField-setColumns()方法不适用于我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

my problem is that my JTextField -setColumns(int)

"field1.setColumns(5);"

"field1.setColumns(5);"

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

...doesn't work. I'm guessing its a layout manager problem. However, I'm learning from an exercise book. The only layouts I know are flowLayout, borderLayout and gridlayout.

简而言之,每当"field2"触发一个ActionEvent(通过按Enter键)时,"field1"都应更改大小.

To explain this code in short, whenever "field2" triggers an ActionEvent (by pressing the enter key) "field1" should change size.

我已经在"actionPerformed"中放置了一个"System.out.println(检测到ActionEvent"),以证明正在触发一个actionevent,所以这不是问题.我什至打印了"field1.getColumn" ",它会显示正确的更改后的值5;但是,它不只是在运行时没有明显地更改大小.

I've placed a "System.out.println("ActionEvent detected") in the "actionPerformed" to prove an actionevent is being fired, so that isn't the problem. I've even printed "field1.getColumn" and it shows the correct changed value of 5, however... Its not just not visibly changing size at runtime.

我不是希望解决这个问题,而是希望有人可以解释这个问题.解决问题并不能帮助我学习,这是解决这些书本练习的全部目的.

Rather than a work around I was hoping somebody could explain the problem. A work around isn't going to help me learn, which is the whole purpose of tackling these book exercises.

如果它很重要,我正在用netbeans进行编码.预先感谢您的帮助.

Incase its important, I'm coding in netbeans. Thanks in advance for the help.

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请求(以删除任何视觉伪像).

It's working, you just need to force the container to layout its components again. This can be achieved by invoking revalidate and then issuing a repaint request (to remove any visual artifacts).

这篇关于JTextField-setColumns()方法不适用于我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆