简体   繁体   中英

Resizing the window

I have the following program. When I change the column size, and click on any of the center, left or right alignment button, if the column size is too high then everything changes. I want the window to increase in size and not have anything changed in the arrangement of the panels. Could some one throw some insight.

package workingwithjtextfields;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.border.*;


public class WorkingwithJTextFields extends JFrame {

    private JTextField jtfMessage = new JTextField(100);
    private JTextField jtfColumn = new JTextField(5);
    private JRadioButton jrbLeft, jrbCenter, jrbRight;

    public static void main(String[] args) //Main program begins here.
    {
        JFrame frame = new WorkingwithJTextFields();//Instantiating an object.
        //frame.pack();
        frame.setTitle("Exercise 17.11");//Setting the frame title.
        frame.setSize(500, 110);//Setting the size.
        frame.setLocationRelativeTo(null);//Setting the location.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Default closing options.
        frame.setVisible(true);//Setting visibility to true.

    }//End of main program

    public WorkingwithJTextFields() {

        // jtfMessage.setColumns(100);
        final JPanel parent = new JPanel();
        parent.setLayout(new GridLayout(2, 1, 3, 3));

        final JPanel p1 = new JPanel();
        p1.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 0));

        p1.add(new JLabel("TextField", SwingConstants.CENTER));

        jtfMessage = new JTextField("Type anything", SwingConstants.RIGHT);
        jtfMessage.setHorizontalAlignment(SwingConstants.CENTER);
        jtfMessage.setColumns(30);
        p1.add(jtfMessage);

        parent.add(p1);

        JPanel jpRadioButtons = new JPanel();
        jpRadioButtons.setLayout(new GridLayout(1, 3));
        jpRadioButtons.add(jrbLeft = new JRadioButton("Left"));
        jpRadioButtons.add(jrbCenter = new JRadioButton("Center"));
        jpRadioButtons.add(jrbRight = new JRadioButton("Right"));
        jpRadioButtons.setBorder(new TitledBorder("Horizontal Border"));

        final JPanel p2 = new JPanel();
        p2.setLayout(new GridLayout(1, 2, 1, 1));

        p2.add(jpRadioButtons);

        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(1, 1, 1, 1));
        p3.add(new JLabel("Column Size"));

        jtfColumn = new JTextField("60", SwingConstants.RIGHT);
        jtfColumn.setHorizontalAlignment(SwingConstants.CENTER);

        p3.add(jtfColumn);
        Border lineBorder = new LineBorder(Color.LIGHT_GRAY, 1);
        p3.setBorder(lineBorder);
        p2.add(p3);
        parent.add(p2);
        add(parent);

        jrbLeft.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jtfMessage.setHorizontalAlignment(SwingConstants.LEFT);
                        jrbCenter.setSelected(false);
                        jrbRight.setSelected(false);
                        jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText()));
                        p1.revalidate();
                        p1.repaint();

                    }
                }
        );

        jrbCenter.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jtfMessage.setHorizontalAlignment(SwingConstants.CENTER);
                        jrbLeft.setSelected(false);
                        jrbRight.setSelected(false);
                        jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText()));
                    }
                }
        );

        jrbRight.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jtfMessage.setHorizontalAlignment(SwingConstants.RIGHT);
                        jrbCenter.setSelected(false);
                        jrbLeft.setSelected(false);
                        jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText()));
                    }

                }
        );

    }

}

Java应用程序使用LayoutManagers来控制它,如果要100%控制它,则必须使用非标准组件,例如Netbeans的AbsoluteLayout,但是您必须在应用程序中包含它的.jar。

When you change the column size of the field, you will need to call pack on the fields window to cause it to resize the window to the preferred size of the components that it contains

For example...

jrbLeft.addActionListener(
        new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jtfMessage.setHorizontalAlignment(SwingConstants.LEFT);
                jrbCenter.setSelected(false);
                jrbRight.setSelected(false);
                jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText()));
                p1.revalidate();
                SwingUtilities.getWindowAncestor(p1).pack();
                SwingUtilities.getWindowAncestor(p1).setLocationRelativeTo(null);
            }
        }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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