[英]Why won't this JAVA GUI process past the first entry?
今天,我們在課堂上匯總了一個基本的GUI,用於計算兩點之間的距離。 老師和我的同學都無法弄清楚為什么此GUI無法處理。 提供給我們的代碼是一個框架,我們只是編輯了舊代碼以進行開發。 我將自己的工作與另外兩個學生的工作進行了比較,而他們的工作卻沒有我的。
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.text.DecimalFormat;
import java.awt.Color;
import java.awt.Font;
import java.lang.Math;
public class D2 extends JFrame
{
//****************************************
//** GUI Structure
//** Title: Holston Middle School
//** Weight prompt (jlabel, jtext)
//** planet pulldown
//** Weight on planet (jlabel, jtext)
//** calculate button
//****************************************
public JTextField entry1, entry2, entry3, entry4, output1;
public JLabel label1, label2, label3, label4, label5;
public JButton CalculateButton;
public String mtitle, cmessage;
public D2()
{
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
label1 = new JLabel("x1");
add(label1);
entry1 = new JTextField(8);
add(entry1);
setEnabled(true);
setVisible(true);
label2 = new JLabel("x2");
add(label2);
entry2 = new JTextField(8);
add(entry2);
setEnabled(true);
setVisible(true);
label3 = new JLabel("y1");
add(label3);
entry3 = new JTextField(8);
add(entry3);
setEnabled(true);
setVisible(true);
label4 = new JLabel("y2");
add(label4);
entry4 = new JTextField(8);
add(entry4);
setEnabled(true);
setVisible(true);
label5 = new JLabel("Distance");
add(label5);
output1 = new JTextField(8);
add(output1);
setEnabled(false);
setVisible(true);
CalculateButton = new JButton("Calculate");
add(CalculateButton);
CalculateButton.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String entry1string = entry1.getText();
Double e1value = Double.valueOf(entry1string);
String entry2string = entry2.getText();
Double e2value = Double.valueOf(entry2string);
String entry3string = entry3.getText();
Double e3value = Double.valueOf(entry3string);
String entry4string = entry4.getText();
Double e4value = Double.valueOf(entry4string);
String wmessage = "You selected ";
String wtitle = "Pop Up Box";
if (true) JOptionPane.showMessageDialog(null, wmessage, wtitle, JOptionPane.PLAIN_MESSAGE);
double distance = (Math.pow((e1value - e2value), 2) + Math.pow((e3value - e4value), 2));
DecimalFormat fmt = new DecimalFormat("####.##");
String outstring = fmt.format(distance);
output1.setText("");
output1.setText(outstring);
}//** actionPerformed
}); //** Action Listener
} //** D2 constructor
public static void main(String[] args)
{
D2 frame = new D2();
frame.setTitle("Distance Calculator");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 200);
frame.setBackground(Color.CYAN);
frame.getContentPane().setBackground(Color.lightGray);
frame.setVisible(true);
} //** main
} //** class
插入打印語句並將label1的值設置為另一個值不會導致GUI更改。 有什么幫助嗎?
在創建output1
您編寫了setEnabled(false);
。 您可能是說應該禁用output1
。 但是相反,您禁用了整個容器,因此所有元素都不是可編輯\\可單擊的。 要解決此問題,請將此屬性直接設置為控制:
output1 = new JTextField(8);
add(output1);
output1.setEnabled(false);
output1.setVisible(true);
希望這會有所幫助。
PS似乎您還需要通過添加Math.sqrt
來改進計算本身(如果我正確理解了您的想法):
double distance = Math.sqrt(Math.pow((e1value - e2value), 2) + Math.pow((e3value - e4value), 2));
@Mikita是正確的。 您還可以考慮在JFrame
添加一個JPanel
或至少將所有組件(JButton,JTextfields等)添加到JFrame
的ContentPane
中。
請參閱《 使用頂級容器》教程和此FlowLayout示例 。
getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
label1 = new JLabel("x1");
getContentPane().add(label1);
entry1 = new JTextField(8);
getContentPane().add(entry1);
要么
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 20));
label1 = new JLabel("x1");
panel.add(label1);
entry1 = new JTextField(8);
panel.add(entry1);
getContentPane().add(panel);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.