当前位置: 首页 > 知识库问答 >
问题:

阻止Java GUI组件调整大小*紧急分配[关闭]

孙自怡
2023-03-14

要防止组件调整大小,请使用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个答案

麻学博
2023-03-14

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

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

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

查看布局和使用布局管理器的可视化指南

 类似资料:
  • 问题内容: 我正在使用一些动态生成的数据输入形式来管理组件。布局大致如下: 我将2个平行组用于水平布局,并将一个顺序组用于垂直布局。在大多数情况下,一切都很好。 我想将标签的最大宽度(只是的实例)限制为父级宽度的。如果将JFrame其设置为固定大小,这将是微不足道的,但是我必须处理它的调整大小。 我正在从接收事件,JFrame但是一旦收到这样的事件,我就该做什么了。 我试过这种方法没有任何运气:

  • 我试图创建JPanel与可拖动的交叉显示后,鼠标点击。一切都很好,但是当我调整JPanel的大小时,交叉消失了。我试图重写JPanel中的paintComponent方法,但所有交叉都在坐标(0,0)处。我该怎么修好它?

  • 我是一个相当新的编码,已经在它的几个小时现在一个月。在过去的几个小时里,我一直被困在我附上的两张图片中的问题上。我已经尝试了搜索答案和各种方法,如最小宽度,显示:Flex,调整字体大小从自动缩放与vw和使用REM。我只想让我的文本留在笔记本电脑屏幕内,无论屏幕大小,我正在查看它。我知道我可以使用简单的方式,只是photoshop的照片与文本,但我想学习如何做与编码为未来的项目以及。我确实想让图片缩

  • 免责声明:我是新来的。 TLDR:我用一个按钮将JPanel动态添加到“封闭”JPanel中。所有面板均使用MiGLayout。外部面板仅包含一个单元格,添加内部面板时会创建更多行,从上到下(“流动”约束)。内部面板由两行组成,但第二行是动态显示的(“隐藏模式3”布局约束,设置可见(false),直到满足条件为止。) 正在发生的事情是,当添加一个内部面板,并使其第二行可见时,它与它下面的面板重叠。

  • 我的Java Swing GUI有问题。首先,我创建了一个带有GridBagLayout的面板,并将所有标签添加到其中。然而,我还在另一个JPanel的右侧创建了一个面板,其中添加了一个按钮和两个滑块,它们应该与标签匹配。 问题是JLabels比另一个面板右侧的组件小,这使得它看起来像这样...... 例如,水选项——JSLIDER(JSLIDER看起来要大得多) 我试图通过将添加到更大的值来使组

  • 我想让JFrame上的组件随着窗口的大小调整而调整大小。 这怎么能做到。 当前,当我使JFrame变小时,组件会保留在相同的位置,然后随着框架变小而消失。 理想情况下,我希望设置一个最小的JFrame大小,它不允许表单变小,但当我将其变大时,组件应该随JFrame移动。 补充:如果我想使用绝对布局管理器,该怎么办。这是我老板比较喜欢的布局经理。 代码: 公共类TestFrame扩展JFrame{p