如何更改JTextArea的大小

时间:2013-05-29 05:16:07

标签: java swing jtextarea

当我点击按钮时,我想用三个单选按钮来改变JTextArea的大小。

if(rd_7inch.isSelected())
{
    jScrollPane2.setSize(200,200);
    txt_sysnp.setSize(5,20);
}if(rd_9inch.isSelected())
{
    jScrollPane2.setSize(200,200);
    txt_sysnp.setSize(5,25);
}if(rd_10inch.isSelected())
{
    jScrollPane2.setSize(200,200);
    txt_sysnp.setSize(5,30);
}

2 个答案:

答案 0 :(得分:2)

重要的一点是,每当您更新UI时,您必须在该面板或容器上调用revalidate(),以便您的更改得到应用。

您也可以通过setSize()方法执行此操作。

public void showDialog(){
         btnUp.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dimSPane.setSize(new Dimension(400,50));
                pane.revalidate();
            }
        });
       btnUp.setSize(new Dimension(100,24));
         btnDn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dimSPane.setSize(new Dimension(200,25));
                pane.revalidate();
            }
        });
       btnDn.setSize(new Dimension(100,24));
        dimSPane.setSize(new Dimension(200,25));
        Dimension dimtfield = new Dimension();
        dimtfield.setSize(new Dimension(200,25));
        spane.setMinimumSize(dimSPane);
        spane.setMaximumSize(dimSPane);
        spane.setPreferredSize(dimSPane);
        tfield.setMinimumSize(dimtfield);
        tfield.setMaximumSize(dimtfield);
        tfield.setPreferredSize(dimtfield);
        pane.add(spane);
        pane.add(tfield);
        pane.add(btnUp);
        pane.add(btnDn);
        JDialog dlg =new JDialog();dlg.add(pane);
        dlg.pack();
                dlg.show();
    }

enter image description here enter image description here

答案 1 :(得分:1)

您的组件很可能在布局管理器的控制之下。

您可以建议更改大小的唯一方法是使用setColumnssetRows并使用符合其组件首选大小的布局管理器

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TextAreaSize {

    public static void main(String[] args) {
        new TextAreaSize();
    }

    public TextAreaSize() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextArea ta;

        public TestPane() {

            setLayout(new GridBagLayout());

            JRadioButton btnSmall = new JRadioButton(new SizeAction("Small", 2, 10));
            JRadioButton btnMed = new JRadioButton(new SizeAction("Medium", 4, 15));
            JRadioButton btnLarge = new JRadioButton(new SizeAction("Large", 12, 24));
            ButtonGroup bg = new ButtonGroup();
            bg.add(btnSmall);
            bg.add(btnMed);
            bg.add(btnLarge);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTHWEST;
            add(btnSmall, gbc);
            gbc.gridy++;
            add(btnMed, gbc);
            gbc.gridy++;
            add(btnLarge, gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.gridheight = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.CENTER;
            ta = new JTextArea();
            add(new JScrollPane(ta), gbc);

            btnSmall.doClick();

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        public class SizeAction extends AbstractAction {

            private int rows;
            private int columns;

            public SizeAction(String name, int rows, int columns) {
                putValue(NAME, name);
                this.rows = rows;
                this.columns = columns;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                ta.setRows(rows);
                ta.setColumns(columns);
                revalidate();
            }
        }
    }        
}