2013-12-09 259 views
-2

我一直在使用Java大约一年,刚刚进入了GUI的基础知识。我正在编写一个程序,它将计算2,3,4或5个点(用户的选项)之间的质量中心。根据用户想要输入的点数,许多可编辑的JTextFields显示为坐标和这些坐标的质量的输入。不幸的是,当被要求计算质量中心时,按钮不会显示我想要的。现在编写它的方式是我得到它编译/运行没有空字符串错误的唯一方法。我相信它与我如何初始化变量/它们在构造函数之间初始化的位置有关,但是我不能在我的生活中找出问题出在哪里。任何帮助将不胜感激!我附上了代码 - 我知道它很长,但你永远不知道什么是有用的。谢谢!空按钮监听器

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

请只发布您代码的相关部分。此外你最终得到的结果是什么? – Lestat

回答

0

你应该做的是实例化display,在那里你有它宣布。

JLabel display = new JLabel(" "); 

然后将其添加到GUI,无论您在代码中添加所有其他组件。在你的听众班,只要设置文字

public void actionPerformed(ActionEvent e){ 
    display.setText("some text"); 
} 

你这样做的方式可能会搞乱你想要的GUI格式。如果你要这样做,在添加任何新组件后需要revalidate()repaint()。我建议使用前者。

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; 

始终在您添加组件调用revalidaterepaint

display = new JLabel("The center of mass is located at (" + centerX + "," + centerY 
    +")."); 
displayPanel.add(display); 
displayPanel.revalidate(); 
displayPanel.repaint();