java—当我单击主gui上的按钮时,会打开一个空白gui

eoigrqb6  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(476)

所以我走在一个密度计算器图形用户界面,我有一些问题。这是我的主页代码

package DensityCalc;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;

public class Home extends JFrame {

    private JPanel contentPane;
    static Home frame = new Home();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        /*UIManager um = new UIManager();
        um.put("OptionPane.background", Color.blue);
        um.put("Panel.background", Color.yellow);
        um.put("OptionPane.messageFont", new Font("Cinzel Decorative", Font.BOLD, 13));*/
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    JOptionPane.showMessageDialog(frame,"DENSITY CALCULATOR");

                    frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Home() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 205);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 434, 166);
        panel.setBackground(new Color(153, 204, 204));
        contentPane.add(panel);

        JButton btnNewButton_1 = new JButton("Mass");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                Masses l1frame = new Masses();
                l1frame.setVisible(true);

            }
        });
        panel.add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("Volume");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                vol window1 = new vol ();
                window1.setVisible(true);

            }
        });

        JButton btnNewButton = new JButton("Density");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                Den l1frame = new Den();
                l1frame.setVisible(true);

            }
        });
        panel.add(btnNewButton);
        panel.add(btnNewButton_2);

    }

}

当我运行它我得到这个
这是我的密度图形用户界面代码

package DensityCalc;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
import javax.swing.UIManager;

public class vol extends Home {

    private JFrame frame;
    private JTextField textfield1;
    private JTextField textfield2;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    vol window = new vol();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public vol() {
        initialize();
    }

    /**
     * Initialise the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setBackground(new Color(153, 204, 204));
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel speed = new JLabel("Mass (kg)");
        speed.setForeground(new Color(0, 0, 0));
        speed.setFont(new Font("Cinzel Decorative", Font.BOLD, 16));
        speed.setBackground(new Color(255, 0, 0));
        speed.setBounds(10, 57, 152, 40);
        frame.getContentPane().add(speed);

        textfield1 = new JTextField();
        textfield1.setFont(new Font("Tahoma", Font.PLAIN, 16));
        textfield1.setBackground(SystemColor.control);
        textfield1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        textfield1.setBounds(174, 56, 139, 40);
        frame.getContentPane().add(textfield1);
        textfield1.setColumns(10);

        JLabel lblNewLabel = new JLabel("vol (m³)");
        lblNewLabel.setFont(new Font("Cinzel Decorative", Font.BOLD, 16));
        lblNewLabel.setBounds(10, 131, 152, 52);
        frame.getContentPane().add(lblNewLabel);

        textfield2 = new JTextField();
        textfield2.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
        textfield2.setBackground(SystemColor.control);
        textfield2.setBounds(174, 100, 139, 39);
        frame.getContentPane().add(textfield2);
        textfield2.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("");
        lblNewLabel_2.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
        lblNewLabel_2.setToolTipText("Answer");
        lblNewLabel_2.setForeground(Color.DARK_GRAY);
        lblNewLabel_2.setBackground(UIManager.getColor("CheckBox.background"));
        lblNewLabel_2.setBounds(173, 151, 203, 29);
        frame.getContentPane().add(lblNewLabel_2);

        JButton ansButton = new JButton("Calculate");
        ansButton.setForeground(Color.DARK_GRAY);
        ansButton.setBackground(Color.WHITE);
        ansButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                double mass, vol, dens;
                try {
                    mass = Double.parseDouble(textfield1.getText());
                    dens = Double.parseDouble(textfield2.getText());
                    if (mass > 0 && dens > 0) { 
                        vol = mass / dens;

                        lblNewLabel_2.setText(Double.toString((double) vol));

                    }
                    else {
                        JOptionPane.showMessageDialog(null, "Please Enter Positive Numbers");
                    }
                }   
                catch(Exception e1) {
                JOptionPane.showMessageDialog(null, "Please Enter Valid Values");
                }
            }
        });
        ansButton.setFont(new Font("Cinzel Decorative", Font.BOLD, 16));
        ansButton.setBounds(0, 203, 162, 40);
        frame.getContentPane().add(ansButton);

        JLabel lblNewLabel_1 = new JLabel("Density (Kg/m³)");
        lblNewLabel_1.setFont(new Font("Cinzel Decorative", Font.BOLD, 16));
        lblNewLabel_1.setBackground(Color.ORANGE);
        lblNewLabel_1.setBounds(10, 105, 152, 30);
        frame.getContentPane().add(lblNewLabel_1);

        JButton btnNewButton = new JButton("Back");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Home f = new Home();
                f.setVisible(true);
            }
        });
        btnNewButton.setBounds(0, 0, 117, 29);
        frame.getContentPane().add(btnNewButton);

    }
}

当我运行它我得到这个
现在,当我打开主gui并单击音量按钮时,它不会将我带到音量gui,而是打开一个空白gui。但是当我运行volume gui并返回时,我被带回家,我按下home上的volume按钮,一个空白的gui就会打开。我不明白这个问题。我希望我能解释清楚

aelbi1ox

aelbi1ox1#

关于swing设计的一般意见:
类名应该以大写字符开头。你有两个班“家”和“卷”。保持一致。
不要使用空布局。不要使用setbounds()。swing设计用于布局管理器。
一个应用程序应该只有一个jframe。如果您需要子窗口,那么通常会使用模态窗口 JDialog .
变量名应该是有意义的。为什么你要叫一个按钮“btnnewbutton2”。如果你有多个按钮,你会忘记什么按钮1,2,或3参考了。像“volumebutton”这样的名字,如果更具描述性的话。
只应在向类添加功能时扩展类。您不应该扩展jframe。向框架添加组件不会更改框架的功能,因此不应扩展jframe。相反,main()方法应该只创建一个jframe,然后向该框架添加一个面板。
您对框架使用的静态变量不正确。不需要静态变量。
关于你的问题,我怀疑问题是你的“vol”类扩展了“home”类。这是不必要的。
“音量”对话框的功能独立于“主”框。也就是说,主框架应该只显示模态“音量”对话框。关闭“音量”对话框时,焦点将自动返回主帧。

相关问题