当前位置: 技术问答>java相关
我写的程序有点问题,谁能帮我看看?(点击applet按钮,弹出一个窗口)
来源: 互联网 发布时间:2015-05-29
本文导语: //我写一个applet小程序,想点击OK按钮时,弹出一个窗口,但却不能成功,谁能帮我看看? import javax.swing.*; import java.awt.*; import java.util.*; import java.awt.event.*; public class Test extends JApplet { JButton button1 = new JButton...
//我写一个applet小程序,想点击OK按钮时,弹出一个窗口,但却不能成功,谁能帮我看看?
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class Test extends JApplet
{
JButton button1 = new JButton("OK");
JButton button2 = new JButton("Exit");
JTextField txt=new JTextField(25);
public void init()
{
FlowLayout layout = new FlowLayout();
Container cp = getContentPane();
button1.addActionListener(a1);
cp.setLayout(layout);
cp.add(button1);
cp.add(button2);
cp.add(txt);
}
class Button1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt.setText("I loveyou");
MyFrame frame1=new MyFrame("abcdddd");
}
}
Button1Listener a1=new Button1Listener();
}
class MyFrame extends JFrame
{
JButton button1=new JButton("Buttonx");
JButton button2=new JButton("ButtonX");
JTextField txt=new JTextField(25);
MyFrame(String title)
{
super(title);
Container cp=getContentPane();
FlowLayout layout=new FlowLayout();
cp.setLayout(layout);
cp.add(button1);
cp.add(button2);
cp.add(txt);
}
}
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class Test extends JApplet
{
JButton button1 = new JButton("OK");
JButton button2 = new JButton("Exit");
JTextField txt=new JTextField(25);
public void init()
{
FlowLayout layout = new FlowLayout();
Container cp = getContentPane();
button1.addActionListener(a1);
cp.setLayout(layout);
cp.add(button1);
cp.add(button2);
cp.add(txt);
}
class Button1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
txt.setText("I loveyou");
MyFrame frame1=new MyFrame("abcdddd");
}
}
Button1Listener a1=new Button1Listener();
}
class MyFrame extends JFrame
{
JButton button1=new JButton("Buttonx");
JButton button2=new JButton("ButtonX");
JTextField txt=new JTextField(25);
MyFrame(String title)
{
super(title);
Container cp=getContentPane();
FlowLayout layout=new FlowLayout();
cp.setLayout(layout);
cp.add(button1);
cp.add(button2);
cp.add(txt);
}
}
|
MyFrame frame1=new MyFrame("abcdddd");
之后还要
frame1.show();
呢或者
frame1.setVisible(true);
之后还要
frame1.show();
呢或者
frame1.setVisible(true);
|
对,没有setVisible();
|
在Java中MyFrame frame1=new MyFrame("abcdddd");这不过是实例化了一个窗口,没有show()或者setVisible()方法窗口是不会自动显露出来的。在Java的界面设计中还有许多类似的情况!