好的,当我运行它时,它运行良好,但它没有弹出答案。
我做错了什么?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class AF implements ActionListener{
double length;
double width;
double answer;
JTextField twidth;
JTextField tlength;
void AFWindow() {
JFrame AFwindow = new JFrame();
JPanel pan2 = new JPanel(new GridBagLayout());
AFwindow.setVisible(true);
AFwindow.setSize(250, 150);
AFwindow.setResizable(false);
GridBagConstraints c = new GridBagConstraints();
AFwindow.add(pan2);
pan2.setBackground(Color.LIGHT_GRAY);
c.gridx = 0;
c.gridy = 5;
tlength = new JTextField();
tlength.setText(" Length ");
pan2.add(tlength, c);
c.gridx = 0;
c.gridy = 0;
twidth = new JTextField();
twidth.setText(" Width ");
pan2.add(twidth, c);
JButton Find = new JButton("Ok");
c.gridx = 0;
c.gridy = -5;
pan2.add(Find);
Find.addActionListener(this);
Find.setActionCommand("ok");
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("ok")){
try {
this.length = Double.parseDouble(tlength.getText());
this.width = Double.parseDouble(twidth.getText());
}
catch(NumberFormatException ex){
System.out.println("There was an issue!");
}
}
}
int area = (int) (length * width);
public void answer(){
JFrame answer = new JFrame();
answer.setVisible(true);
answer.setBackground(Color.yellow);
JPanel pan2 = new JPanel();
JLabel howanswer = new JLabel("Your answer is" + area + "We got this by multiplying the length and width");
pan2.add(howanswer);
}
}
答案 0 :(得分:2)
该代码有多个问题。忘了记录它们,但是看看这段代码是否有修复的开头。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class AF implements ActionListener{
double length;
double width;
JTextField twidth;
JTextField tlength;
JFrame AFwindow;
void AFWindow() {
AFwindow = new JFrame();
JPanel pan2 = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
AFwindow.add(pan2);
pan2.setBackground(Color.LIGHT_GRAY);
pan2.setBorder(new EmptyBorder(100,100,100,100));
c.gridx = 0;
c.gridy = 5;
tlength = new JTextField();
tlength.setText(" Length ");
pan2.add(tlength, c);
c.gridx = 0;
c.gridy = 0;
twidth = new JTextField();
twidth.setText(" Width ");
pan2.add(twidth, c);
JButton Find = new JButton("Ok");
c.gridx = 0;
c.gridy = -5;
pan2.add(Find);
Find.addActionListener(this);
Find.setActionCommand("ok");
AFwindow.pack();
AFwindow.setLocationByPlatform(true);
AFwindow.setVisible(true);
}
int area;
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("ok")){
try {
this.length = Double.parseDouble(tlength.getText());
this.width = Double.parseDouble(twidth.getText());
area = (int) (length * width);
answer();
}
catch(NumberFormatException ex){
ex.printStackTrace();
// System.out.println("There was an issue!");
// Can you vague that up for me?!?!
JOptionPane.showMessageDialog(AFwindow, ex);
}
}
}
public void answer(){
JPanel pan2 = new JPanel();
JLabel howanswer = new JLabel("Your answer is " + area + " We got this by multiplying the length and width");
pan2.add(howanswer);
JOptionPane.showMessageDialog(AFwindow, pan2);
}
public static void main(String[] args) {
System.out.println("Hi!");
new AF().AFWindow();
}
}
答案 1 :(得分:1)
按下“确定”按钮时,您是否希望弹出答案框?如果是这样,您需要调用答案方法:
if (e.getActionCommand().equalsIgnoreCase("ok")){
try {
this.length = Double.parseDouble(tlength.getText());
this.width = Double.parseDouble(twidth.getText());
answer(); // <--- This is missing
}
此外,您还需要做一些其他事情:
通常,在向框架添加组件后,应在GUI构造方法的末尾调用JFrame.setVisible。我在下面的示例中已经显示了这一点,但您也应该将setVisible(..)移动到AFwindow()方法的底部。
当您在任何方法之外执行int area = (int) (length * width);
时,此语句将被解释为实例变量初始化,并在您实例化AF对象时发生。当对象被实例化时,您还没有为length指定任何值。因此,区域将始终为0.将该语句移动到答案函数的内部。
您忘记在answer方法中将实际组件添加到框架中。因此,当您将框架设置为可见时,只会显示一个空框架。
以下是一些适当更改的示例:
public void answer(){
int area = (int) (length * width); // <--- Move this inside answer method
JFrame answer = new JFrame();
answer.setBackground(Color.yellow);
JPanel pan2 = new JPanel();
JLabel howanswer = new JLabel("Your answer is" + area + "We got this by multiplying the length and width");
pan2.add(howanswer);
answer.add(pan2); // <--- Don't forget to add your components to the frame
answer.pack(); // <--- Resize the frame to a suitable size
answer.setVisible(true); // <--- Set the frame to visible AFTER you have added the components
}
答案 2 :(得分:0)
此代码适用于我:
public class Test {
public static void main(String[] args){
JFrame test = new JFrame();
test.setVisible(true);
test.setSize(100,100);
test.setResizable(false);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我认为您可以尝试减少代码并尝试以这种方式找到问题点。您可以进行一些日志记录以确保调用setVisible(true)
。此外,我建议在退出GUI后使用setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
终止线程(如果不想执行某些退出后操作)。