简体   繁体   English

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

[英]JTextField - setColumns() method not working for me

my problem is that my JTextField -setColumns(int) 我的问题是我的JTextField -setColumns(int)

"field1.setColumns(5);" “field1.setColumns(5);”

...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. 我所知道的唯一布局是flowLayout,borderLayout和gridlayout。

To explain this code in short, whenever "field2" triggers an ActionEvent (by pressing the enter key) "field1" should change size. 简而言之,只要“field2”触发ActionEvent(通过按回车键),“field1”就应该改变大小。

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. 我在“actionPerformed”中放置了一个“System.out.println(”ActionEvent detect“)来证明一个actionevent正在被触发,所以这不是问题。我甚至打印了”field1.getColumn“并且它显示正确的更改值5,但是......它不仅不会在运行时明显改变大小。

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. 解决这个问题不会帮助我学习,这是解决这些书籍练习的全部目的。

Incase its important, I'm coding in netbeans. 重要的是,我在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);
 }

} }

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). 这可以通过调用revalidate然后发出repaint请求(删除任何可视化工件)来实现。

方法.setColumns() (与JFormattedTextField组件一起使用)无法工作,因为对于放置了JFormattedTextFields的容器使用了TitledBorder

<container.setBorder(javax.swing.BorderFactory.createTitledBorder("central"));>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM