【发布时间】:2014-01-22 12:34:52
【问题描述】:
这个问题似乎很常见,但我找不到任何解决方案。 好吧,也许如果我在编程方面不是那么笨拙,我会理解一切并解决问题。
不管怎样,代码如下:
package kalk;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
/**
*
* @author BADASS BOSS
*/
public class Kalkulator extends javax.swing.JFrame {
/**
* Creates new form Kalkulator
*/
private double liczba1, liczba2;
private double wynik=0;
private int nrdzialania=0;
private boolean dopierwszej=true;
private Toolkit glownytoolkit;
//dzialania
//1-dodawanie
//2-odejmowanie
//3-mnozenie
//4-dzielenie
private double pobierzliczbe(String s)
{
double temp;
temp = 0;
try
{
temp = Double.valueOf(s);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, "FATAL ERROR!!" + e,"Coś się zepsuło!!!!", JOptionPane.ERROR_MESSAGE);
}
return temp;
}
private void robdzialanie()
{
String bufor;
liczba1 = pobierzliczbe(jTextField1.getText());
liczba2 = pobierzliczbe(jTextField3.getText());
bufor = "";
if (nrdzialania==1) wynik = liczba1 + liczba2;
else if (nrdzialania==2) wynik = liczba1 - liczba2;
else if (nrdzialania==3) wynik = liczba1 * liczba2;
else if (nrdzialania==4)
{
if (liczba2==0) bufor="FATAL ERROR!! Nie można dzielić przez zero!!!";
else wynik = liczba1 / liczba2;
}
if (nrdzialania!=0) bufor = String.valueOf(wynik);
else bufor = "FATAL ERROR!! Argument jest pusty albo niepoprawny!!";
jTextField4.setText(bufor);
}
public Kalkulator() {
initComponents();
glownytoolkit = Toolkit.getDefaultToolkit();
}
private void dajnasrodek()
{
int x;
int y;
int szerokość_ekranu;
int wysokość_ekranu;
int wysokość_ramki;
int szerokość_ramki;
szerokość_ekranu = glownytoolkit.getScreenSize().width;
wysokość_ekranu = glownytoolkit.getScreenSize().height;
szerokość_ramki = this.getSize().width;
wysokość_ramki = this.getSize().height;
x = (szerokość_ekranu - szerokość_ramki)/2;
y = (wysokość_ekranu - wysokość_ramki)/2;
this.setLocation(x, y);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
////some unnecessary stuff I guess
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
//Kalkulator gc = new Kalkulator();
//gc.dajnasrodek();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
}
}
);
//Kalkulator gc = new Kalkulator();
//gc.dajnasrodek();
//ActionEvent klik;
//jButton18ActionPerformed();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
///some unnecessary stuff
}
对波兰语感到抱歉,但我希望变量的名称无论如何都不是那么重要。
好吧,这就是交易:当我尝试从 main 方法中引用 dajnasrodek(); 时,我得到了如标题所示的错误 - non static method cannot be referenced from a static context。我试图将dajnasrodek() 方法更改为静态,但这并没有那么好,因为出现了一些其他错误。
有什么聪明的主意吗?任何形式的帮助将不胜感激!提前非常感谢!
【问题讨论】:
-
那么错误在哪一行?我会尝试自己编译它,但我没有针对您使用的任何字符集进行设置,而且我不会为了回答这个问题而搞乱我的设置。 (无论如何,在我输入答案之前它可能会被关闭)
-
这就是我的预期。好吧,我想从 main 方法中引用 dajnasrodek() 方法,但我什至不知道该放在哪里……你至少知道我的意思吗?因为我是编程初学者,所以我似乎很不清楚。
-
或者,简单地说——方法 dajnasrodek() 的意思就是“屏幕居中”,我要做的就是让程序在启动后自动居中屏幕。任何想法如何做到这一点? dajnasrodek() 方法是我想运行的算法,但到目前为止没有运气
-
不,您似乎在描述编译错误。但我看到的只是应该编译的代码。如果您需要帮助解决编译错误,您需要向我们展示有错误的代码,而不是其他没有错误的代码。
标签: java swing methods static jframe