2013-12-09 146 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();