JAVA无法从静态上下文中引用非静态 变量 this 10
JAVA初学者,按一个视频写了如下代码,但发现有上述标题问题,烦请前辈指教,代码如下:importjava.awt.*;importjava.awt.event.*;im...
JAVA 初学者,按一个视频写了如下代码,但发现有上述标题问题,烦请前辈指教,代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Java类 {
public static void main (String[] args){
WelcomeFrame frame=new WelcomeFrame(); (出错行)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
class WelcomeFrame extends JFrame{
public static final int DEFAULT_Width=300;
public static final int DEFAULT_High=200;
public WelcomeFrame(){
setTitle("窗体标题:Welcome");
setSize(DEFAULT_Width,DEFAULT_High);
WelcomePanel panel=new WelcomePanel();
Container contentPane=this.getContentPane();
contentPane.add(panel);
}
}
class WelcomePanel extends JPanel{
public WelcomePanel(){
JLabel prompt=new JLabel("请输入您的名字");
final JTextField input=new JTextField(10);
final JTextField output=new JTextField(25);
JButton btnn=new JButton("Welcome");
add(prompt);
add(input);
add(output);
add(btnn);
btnn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
String s=input.getText();
output.setText("Hello,"+s+"欢迎参加Java考试");
}
});
}
}
} 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Java类 {
public static void main (String[] args){
WelcomeFrame frame=new WelcomeFrame(); (出错行)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
class WelcomeFrame extends JFrame{
public static final int DEFAULT_Width=300;
public static final int DEFAULT_High=200;
public WelcomeFrame(){
setTitle("窗体标题:Welcome");
setSize(DEFAULT_Width,DEFAULT_High);
WelcomePanel panel=new WelcomePanel();
Container contentPane=this.getContentPane();
contentPane.add(panel);
}
}
class WelcomePanel extends JPanel{
public WelcomePanel(){
JLabel prompt=new JLabel("请输入您的名字");
final JTextField input=new JTextField(10);
final JTextField output=new JTextField(25);
JButton btnn=new JButton("Welcome");
add(prompt);
add(input);
add(output);
add(btnn);
btnn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
String s=input.getText();
output.setText("Hello,"+s+"欢迎参加Java考试");
}
});
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询