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



所以我在密度计算器GUI上行走,遇到了一些问题。这是我的主页代码

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

}

}

当我运行它时,我会得到这个

这是我的密度gui 代码

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。但是,当我运行音量gui并返回时,我被带回家,我按下主页上的音量按钮,打开的是一个空白gui。我不理解这个问题。我希望我解释得很好

关于Swing设计的一般评论:

  1. 类名应该以大写字符开头。你有两个班"主页";以及";vol";。保持一致。

  2. 不要使用空布局。不要使用setBounds((。Swing设计用于布局管理器。

  3. 一个应用程序应该只有一个JFrame。如果您需要子窗口,那么通常会使用模式JDialog

  4. 变量名称应该是有意义的。你为什么要叫一个按钮";btnNewButton_2";。如果你有多个按钮,你也会忘记按钮1、2或3指的是什么。像";volumeButton";如果更具描述性的话。

  5. 只有在向类添加功能时,才应扩展该类。您不应该扩展JFrame。向框架添加组件不会更改框架的功能,因此不应该扩展JFrame。相反,main((方法应该简单地创建一个JFrame,然后在该框架中添加一个面板。

  6. 您不正确地将静态变量用于框架。不需要静态变量。

关于你的问题,我怀疑问题是你的";vol";类扩展了";主页";班这是不必要的。

";"体积";对话框的功能独立于";主页";框架也就是说,主页框应该简单地显示模式";"体积";对话关闭"音量"对话框时,焦点将自动返回到主画面。

相关内容

最新更新