防止Java GUI组件调整大小*紧急分配

时间:2013-08-02 03:26:34

标签: java swing user-interface resize

  

“为防止组件调整大小,请使用FlowLayout将组件添加到JPanel,然后将该面板添加到BorderLayout。这是防止调整大小的常用方法.FlowLayout面板将拉伸,但其中的组件不会。 “我找到了这个但是我对代码的实用性有困难。

public class abdul {

    public static void main(String[] args) {
        JFrame frame = new FutureValueFrame();
        frame.setVisible(true);
    }
}

class FutureValueFrame extends JFrame {

    public FutureValueFrame() {
        setTitle("Loan Calculator");
        setSize(300, 300);
        centerWindow(this);
        setResizable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new FutureValuePanel();
        this.add(panel);
    }

    private void centerWindow(Window w) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        setLocation((d.width - w.getWidth()) / 2, (d.height - w.getHeight()) / 2);
    }
}

class FutureValuePanel extends JPanel implements ActionListener {

    private JTextField paymentText, rateText, yearsText, loanText;
    private JLabel paymentLabel, rateLabel, yearsLabel, loanLabel;
    private JButton calculateButton, exitButton, paymentButton, loanButton;
    private int counter = 0;

    public FutureValuePanel() { // display panel 
        JPanel displayPanel = new JPanel();
        displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));


        loanLabel = new JLabel("Loan Amount:");
        displayPanel.add(loanLabel);
        //hello
        loanText = new JTextField(10);
        displayPanel.add(loanText);

        //////



        ///////
        rateLabel = new JLabel("Yearly Interest Rate:");
        displayPanel.add(rateLabel);

        rateText = new JTextField(10);
        displayPanel.add(rateText);


////////
        yearsLabel = new JLabel("Number of Years:");
        displayPanel.add(yearsLabel);

        yearsText = new JTextField(10);
        displayPanel.add(yearsText);



////////
        paymentLabel = new JLabel("Monthly Payment:");
        displayPanel.add(paymentLabel);
        //hello
        paymentText = new JTextField(10);
        displayPanel.add(paymentText);


// button panel
        JPanel buttonPanel = new JPanel();
        JPanel alphaPanel = new JPanel();
        ;
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        alphaPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

// calculate button 
        calculateButton = new JButton("Calculate");
        calculateButton.addActionListener(this);
        buttonPanel.add(calculateButton);

        paymentButton = new JButton("Monthly Payment");
        paymentButton.addActionListener(this);
        alphaPanel.add(paymentButton);


        loanButton = new JButton("Loan Amount");
        loanButton.addActionListener(this);
        alphaPanel.add(loanButton);


// exit button
        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);
        buttonPanel.add(exitButton);
// add panels to main panel
        this.setLayout(new BorderLayout());
        this.add(displayPanel, BorderLayout.CENTER);
        this.add(buttonPanel, BorderLayout.SOUTH);
        this.add(alphaPanel, BorderLayout.NORTH);
    }

    public void actionPerformed(ActionEvent e) {

        Object source = e.getSource();
        if (source == exitButton) {
            System.exit(0);
        }


        if (source == loanButton) {
            loanText.setEditable(false);
            loanText.setFocusable(false);
            loanText.setText(null);
            paymentText.setEditable(true);
            paymentText.setFocusable(true);
            counter = 1;
        }
        if (source == paymentButton) {
            paymentText.setEditable(false);
            paymentText.setFocusable(false);
            paymentText.setText(null);
            loanText.setEditable(true);
            loanText.setFocusable(true);
            counter = 2;
        }
        if (source == calculateButton) {
            NumberFormat currency = NumberFormat.getCurrencyInstance();
            //  paymentText.setText(currency.format(Double.parseDouble(loanText.getText())));
            //  
            //          paymentText.setText(the);
            //loanText.setText(" ");
            //  paymentText.setText(" ");

            if (counter == 1) {
                Double rate = Double.parseDouble(rateText.getText());
                Double years = Double.parseDouble(yearsText.getText());
                Double payment = Double.parseDouble(paymentText.getText());
                String result = currency.format(((1 - (1 / (Math.pow((1 + (rate / 12)), (years * 12))))) * payment * 12) / rate);
                loanText.setText(result.substring(1));
            }

            if (counter == 2) {

                Double loan = Double.parseDouble(loanText.getText());
                Double rate = Double.parseDouble(rateText.getText());
                Double years = Double.parseDouble(yearsText.getText());
                String retur = currency.format(((loan * (rate / 12)) / (1 - (1 / (Math.pow((1 + (rate / 12)), (years * 12)))))));
                paymentText.setText(retur.substring(1));
            }
        }
    }
}

1 个答案:

答案 0 :(得分:4)

只需使用更合适的布局管理器......

问题不在于面板调整大小,而在于布局管理器处理该事件的方式

enter image description here enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class BadLayout {

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

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

                JFrame frame = new FutureValueFrame();
                frame.setVisible(true);
            }
        });
    }

    public class FutureValueFrame extends JFrame {

        public FutureValueFrame() {
            setTitle("Loan Calculator");
            centerWindow(this);
            setResizable(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new FutureValuePanel();
            this.add(panel);
            pack();
        }

        private void centerWindow(Window w) {
            Toolkit tk = Toolkit.getDefaultToolkit();
            Dimension d = tk.getScreenSize();
            setLocation((d.width - w.getWidth()) / 2, (d.height - w.getHeight()) / 2);
        }
    }

    class FutureValuePanel extends JPanel implements ActionListener {

        private JTextField paymentText, rateText, yearsText, loanText;
        private JLabel paymentLabel, rateLabel, yearsLabel, loanLabel;
        private JButton calculateButton, exitButton, paymentButton, loanButton;
        private int counter = 0;

        public FutureValuePanel() { 
            JPanel displayPanel = new JPanel(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);

            loanLabel = new JLabel("Loan Amount:");
            displayPanel.add(loanLabel, gbc);
            //hello
            gbc.gridx++;
            loanText = new JTextField(10);
            displayPanel.add(loanText, gbc);

            //////


            gbc.gridx++;
            ///////
            rateLabel = new JLabel("Yearly Interest Rate:");
            displayPanel.add(rateLabel, gbc);

            gbc.gridx++;
            rateText = new JTextField(10);
            displayPanel.add(rateText, gbc);


////////
            gbc.gridx = 0;
            gbc.gridy = 1;
            yearsLabel = new JLabel("Number of Years:");
            displayPanel.add(yearsLabel, gbc);

            gbc.gridx++;
            yearsText = new JTextField(10);
            displayPanel.add(yearsText, gbc);



////////
            gbc.gridx++;
            paymentLabel = new JLabel("Monthly Payment:");
            displayPanel.add(paymentLabel, gbc);
            //hello
            gbc.gridx++;
            paymentText = new JTextField(10);
            displayPanel.add(paymentText, gbc);


// button panel
            JPanel buttonPanel = new JPanel();
            JPanel alphaPanel = new JPanel();

            buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
            alphaPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

// calculate button 
            calculateButton = new JButton("Calculate");
            calculateButton.addActionListener(this);
            buttonPanel.add(calculateButton);

            paymentButton = new JButton("Monthly Payment");
            paymentButton.addActionListener(this);
            alphaPanel.add(paymentButton);


            loanButton = new JButton("Loan Amount");
            loanButton.addActionListener(this);
            alphaPanel.add(loanButton);


// exit button
            exitButton = new JButton("Exit");
            exitButton.addActionListener(this);
            buttonPanel.add(exitButton);
// add panels to main panel
            this.setLayout(new BorderLayout());
            this.add(displayPanel, BorderLayout.CENTER);
            this.add(buttonPanel, BorderLayout.SOUTH);
            this.add(alphaPanel, BorderLayout.NORTH);
        }

        public void actionPerformed(ActionEvent e) {

            Object source = e.getSource();
            if (source == exitButton) {
                System.exit(0);
            }


            if (source == loanButton) {
                loanText.setEditable(false);
                loanText.setFocusable(false);
                loanText.setText(null);
                paymentText.setEditable(true);
                paymentText.setFocusable(true);
                counter = 1;
            }
            if (source == paymentButton) {
                paymentText.setEditable(false);
                paymentText.setFocusable(false);
                paymentText.setText(null);
                loanText.setEditable(true);
                loanText.setFocusable(true);
                counter = 2;
            }
            if (source == calculateButton) {
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                if (counter == 1) {
                    Double rate = Double.parseDouble(rateText.getText());
                    Double years = Double.parseDouble(yearsText.getText());
                    Double payment = Double.parseDouble(paymentText.getText());
                    String result = currency.format(((1 - (1 / (Math.pow((1 + (rate / 12)), (years * 12))))) * payment * 12) / rate);
                    loanText.setText(result.substring(1));
                }

                if (counter == 2) {

                    Double loan = Double.parseDouble(loanText.getText());
                    Double rate = Double.parseDouble(rateText.getText());
                    Double years = Double.parseDouble(yearsText.getText());
                    String retur = currency.format(((loan * (rate / 12)) / (1 - (1 / (Math.pow((1 + (rate / 12)), (years * 12)))))));
                    paymentText.setText(retur.substring(1));
                }
            }
        }
    }
}

结帐A Visual Guide to LayoutsUsing layout managers