我正在根據用戶輸入yes或no來做出反應的窗口。我遇到了一個問題,在這部分代碼:問題與java中的對象?
public class Mywindow {
public static void main (String [] args){
windowcontinued object = new windowcontinued();
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //ISSUE HERE
object.setSize(700, 1024); //ISSUE HERE
object.setVisible(true); //ISSUE HERE
}
}
這些簡單的操作,應確保在關閉時的程序終止,是具有一定規模,並且是可見的。出於某種原因,雖然,在所有三個操作,我得到錯誤說:
The method setDefaultCloseOperation(int) is undefined for the type windowcontinued
The method setSize(int, int) is undefined for the type windowcontinued
The method setVisible(boolean) is undefined for the type windowcontinued
雖然,所有的這些應該已經導入後確定。下面是完整的代碼:
主要代碼:
package Myguipackage;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Mywindow {
public static void main (String [] args){
windowcontinued object = new windowcontinued();
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
object.setSize(700, 1024);
object.setVisible(true);
}
}
其他類:
package Myguipackage;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Windowcontinued{
public JLabel text;
public JTextField textfield;
public windowcontinued(){ //Constructor
/* super("CHAT");
setLayout(new FlowLayout()); */
text = new JLabel ("Would you like to talk to me?");
text.add(text);
textfield = new JTextField ("Yes or no? Erase this and type in your answer!");
}
class hear implements ActionListener{
public void actionPerformed(ActionEvent event){
String string = "";
if(event.getSource()==textfield)
string = String.format("You typed in %s", event.getActionCommand());
JOptionPane.showMessageDialog(null, string); //Open a new window that displays String string, which has changed based on which field you hit enter on.
}
}
}
使用後FDSA的建議extends關鍵字,這裏是新代碼:
包Myguipackage;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class mywindow extends JFrame{
public static void main (String [] args){
windowcontinued object = new windowcontinued();
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
object.setSize(700, 1024);
object.setVisible(true);
}
}
雖然繼續的class window仍然是一樣的。
你應該嘗試尊重Java命名約定,並且你的代碼幾乎不可讀,你應該修復它。 –
你是否在你的'windowcontinued'代碼中看到你調用的任何方法?你爲什麼期望他們在那裏? – RealSkeptic
這些都是基於代碼中上面的導入。 –