-1

我有三个单选按钮,当我单击按钮时,我想使用它们来更改 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);
}
4

2 回答 2

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();
    }

在此处输入图像描述 在此处输入图像描述

于 2013-05-29T05:52:43.917 回答
1

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

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

在此处输入图像描述

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();
            }
        }
    }        
}
于 2013-05-29T05:36:56.657 回答