我现在只使用Java大约一年了,刚刚学习了GUI的基础知识。我正在编写一个计算2,3,4或5点之间质量中心的程序(用户选项)。根据用户想要输入的点数,一些可编辑的JTextField似乎可以获得坐标和这些坐标质量的输入。不幸的是,当被要求计算质心时,按钮将不会显示我想要的内容。它现在编写的方式是我编译/运行没有空字符串错误的唯一方法。我相信它与我如何初始化变量/它们在构造函数之间初始化的位置有关,但我不能在我的生活中弄清楚问题究竟在哪里。任何帮助将不胜感激!我附上了代码 - 我知道它很长,但你永远不知道什么是有用的。谢谢!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class ComboBox extends JFrame
/*************VARIABLES*******************************************************/
{
private final int WIDTH = 1500;
private final int HEIGHT = 1500;
private JPanel northPanel;
private JPanel centerPanel;
private JPanel blankPanel;
private JPanel pointsPanel;
private JPanel selectedPointsPanel;
private JComboBox pointsBox;
private JTextField selectedPoints;
private JLabel selection;
private String choose = "Choose an option...";
private String twoPoints = "2 points";
private String threePoints = "3 points";
private String fourPoints = "4 points";
private String fivePoints = "5 points";
private String[] points = {choose, twoPoints, threePoints, fourPoints, fivePoints};
private JPanel coordinatesPanel;
private JPanel cPanel;
private JTextField xField1;
private JTextField xField2;
private JTextField xField3;
private JTextField xField4;
private JTextField xField5;
private JTextField yField1;
private JTextField yField2;
private JTextField yField3;
private JTextField yField4;
private JTextField yField5;
private JLabel instructions;
private JLabel X;
private JLabel Y;
private JLabel blankLabel;
private JPanel massPanel;
private JTextField mass1 = new JTextField(10);
private JTextField mass2 = new JTextField(10);
private JTextField mass3 = new JTextField(10);
private JTextField mass4 = new JTextField(10);
private JTextField mass5 = new JTextField(10);
private JLabel instructions2;
Boolean calcBool = false;
private JButton calcButton = new JButton("Calculate");
private JButton resetButton = new JButton("Reset");
private JPanel displayPanel;
private double centerX;
private double centerY;
private JLabel display;
private double x1, x2, x3, x4, x5;
private double y1, y2, y3, y4, y5;
private double m1, m2, m3, m4, m5;
/**********************************WINDOW************************************/
public ComboBox()
{
super("Choose an option");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3,2));
setLocationRelativeTo(null);
setResizable(true);
setSize(WIDTH, HEIGHT);
buildPointsPanel();
buildCoordinatesPanel();
buildMassPanel();
buildDisplayPanel();
buildBlankPanel();
add(pointsPanel);
add(coordinatesPanel);
add(massPanel);
add(blankPanel);
add(calcButton);
add(blankPanel);
add(resetButton);
add(blankPanel);
add(displayPanel);
calcButton.addActionListener(new CalcButtonListener());
pointsBox.addActionListener(new ComboBoxListener());
//resetButton.addActionListener(new ResetButtonListener());
pack();
setVisible(true);
}
/************************BUILD ALL THE PANELS**********************/
private void buildBlankPanel()
{
blankPanel = new JPanel();
}
private void buildPointsPanel()
{
pointsPanel = new JPanel();
pointsPanel.setLayout(new GridLayout(3,1));
pointsBox = new JComboBox(points);
pointsBox.addActionListener(new ComboBoxListener());
pointsPanel.add(pointsBox);
selection = new JLabel("You selected: ");
selectedPoints = new JTextField(10);
selectedPoints.setEditable(false);
pointsPanel.add(selection);
pointsPanel.add(selectedPoints);
}
private void buildCoordinatesPanel()
{
coordinatesPanel = new JPanel();
coordinatesPanel.setLayout(new GridLayout(6,2));
instructions = new JLabel("Please enter the X and Y values of your points below.");
JLabel blank = new JLabel("");
X = new JLabel("X values");
Y = new JLabel("Y values");
blankLabel = new JLabel("");
coordinatesPanel.add(instructions);
coordinatesPanel.add(blankLabel);
coordinatesPanel.add(X);
coordinatesPanel.add(Y);
xField1 = new JTextField(10);
xField1.setEditable(true);
yField1 = new JTextField(10);
yField1.setEditable(true);
xField2 = new JTextField(10);
xField2.setEditable(true);
yField2 = new JTextField(10);
yField2.setEditable(true);
xField3 = new JTextField(10);
xField3.setEditable(true);
yField3 = new JTextField(10);
yField3.setEditable(true);
xField4 = new JTextField(10);
xField4.setEditable(true);
yField4 = new JTextField(10);
yField4.setEditable(true);
xField5 = new JTextField(10);
xField5.setEditable(true);
yField5 = new JTextField(10);
yField5.setEditable(true);
}
private void buildMassPanel()
{
massPanel = new JPanel();
instructions2 = new JLabel("Please enter the masses of your points");
massPanel.add(instructions2);
mass1.setEditable(true);
mass2.setEditable(true);
mass3.setEditable(true);
mass4.setEditable(true);
mass5.setEditable(true);
}
private void buildDisplayPanel()
{
displayPanel = new JPanel();
//display = new JLabel("The center of mass is located at (" + centerX + "," + centerY
+").");
//displayPanel.add(display);
}
/********************************COMBOBOX LISTENER****************************/
private class ComboBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{ //The following asks the user to select the number of points they want and stores
it
String select =(String) pointsBox.getSelectedItem();
selectedPoints.setText(select);
//The following determines how many text fields to display depending on how many
points the user wants
if (select==twoPoints)
{
coordinatesPanel.add(xField1);
coordinatesPanel.add(yField1);
coordinatesPanel.add(xField2);
coordinatesPanel.add(yField2);
massPanel.add(mass1);
massPanel.add(mass2);
centerX = ((m1*x1)+(m2*x2)/(m1+m2));
centerY = ((m1*y1)+(m2*y2)/(m1+m2));
}
if (select==threePoints)
{
coordinatesPanel.add(xField1);
coordinatesPanel.add(yField1);
coordinatesPanel.add(xField2);
coordinatesPanel.add(yField2);
coordinatesPanel.add(xField3);
coordinatesPanel.add(yField3);
massPanel.add(mass1);
massPanel.add(mass2);
massPanel.add(mass3);
centerX = ((m1*x1)+(m2*x2)+(m3*x3)/(m1+m2+m3));
centerY = ((m1*y1)+(m2*y2)+(m3*y3)/(m1+m2+m3));
}
if (select==fourPoints)
{
coordinatesPanel.add(xField1);
coordinatesPanel.add(yField1);
coordinatesPanel.add(xField2);
coordinatesPanel.add(yField2);
coordinatesPanel.add(xField3);
coordinatesPanel.add(yField3);
coordinatesPanel.add(xField4);
coordinatesPanel.add(yField4);
massPanel.add(mass1);
massPanel.add(mass2);
massPanel.add(mass3);
massPanel.add(mass4);
centerX = ((m1*x1)+(m2*x2)+(m3*x3)+(m4*x4)/(m1+m2+m3+m4));
centerY = ((m1*y1)+(m2*y2)+(m3*y3)+(m4*y4)/(m1+m2+m3+m4));
}
if (select==fivePoints)
{
coordinatesPanel.add(xField1);
coordinatesPanel.add(yField1);
coordinatesPanel.add(xField2);
coordinatesPanel.add(yField2);
coordinatesPanel.add(xField3);
coordinatesPanel.add(yField3);
coordinatesPanel.add(xField4);
coordinatesPanel.add(yField4);
coordinatesPanel.add(xField5);
coordinatesPanel.add(yField5);
massPanel.add(mass1);
massPanel.add(mass2);
massPanel.add(mass3);
massPanel.add(mass4);
massPanel.add(mass5);
centerX = ((m1*x1)+(m2*x2)+(m3*x3)+(m4*x4)+(m5*x5)/(m1+m2+m3+m4+m5));
centerY = ((m1*y1)+(m2*y2)+(m3*y3)+(m4*y4)+(m5*y5)/(m1+m2+m3+m4+m5));
}
if (select==choose)
{
JOptionPane.showMessageDialog(null, "Please select a valid option");
}
}
}
/********************************CALC BUTTON LISTENER******************************/
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
display = new JLabel("The center of mass is located at (" + centerX + "," + centerY
+").");
displayPanel.add(display);
}
}
/******************************MAIN METHOD***************************/
public static void main(String[] args)
{
new ComboBox();
}
答案 0 :(得分:0)
你应该做的是实例化你声明它的display
。
JLabel display = new JLabel(" ");
然后将其添加到GUI,无论您在代码中添加所有其他组件。比在你的监听器类中,只需设置文本
public void actionPerformed(ActionEvent e){
display.setText("some text");
}
你这样做的方式可能搞乱你想要的GUI格式。但是,如果您这样做,则在添加任何新组件后,您需要revalidate()
和repaint()
。我建议使用前者。
答案 1 :(得分:0)
不知道你的具体问题在哪里,但看看你的代码,我发现了一些潜在的错误。在java中,您将对象与equals()
和==
进行比较。==
仅供参考。
因此,将用字符串==
与equals
进行比较的所有行都更改为select==fivePoints
。
例如更改
select.equals(fivePoints)
到
private JTextField mass1 = new JTextField(10);
private JTextField mass2 = new JTextField(10);
private JTextField mass3 = new JTextField(10);
private JTextField mass4 = new JTextField(10);
private JTextField mass5 = new JTextField(10);
拥有像
这样的属性Collection
不是最佳方式,您可以将它们存储在private List<JTextField> masses;
或数组中。
revalidate
始终在添加组件调用repaint
和 display = new JLabel("The center of mass is located at (" + centerX + "," + centerY
+").");
displayPanel.add(display);
displayPanel.revalidate();
displayPanel.repaint();
时。
{{1}}