java :JButton.addActionListener(new ActionListener()为啥没有生效?
publicclassMainFrameextendsJFrameimplementsActionListener{privateJPaneljpanel1=newJPa...
public class MainFrame extends JFrame implements ActionListener
{
private JPanel jpanel1=new JPanel(); //声明面板容器
private JButton JButton=new JButton("确定"); //声明按钮
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth=screenSize.width;
int srceenHeight=screenSize.height;
private Image image;
public MainFrame()
{
//设置窗体相关属性
//设置窗口标题
this.setTitle("--个人通讯录--");
//设置窗口不可拖动大小
this.setResizable(true);
//设置窗口大小
this.setSize(screenWidth,srceenHeight);
//设置窗口居于屏幕中央
//setLocation((screenWidth-getWidth())/2,(srceenHeight-getHeight())/2);
//设置组件的相关属性
//设置面板容器的布局策略为空
jpanel1.setLayout(null);
//设置面板容器背景色
JButton.setBounds(440, 500, 500, 255);
//设置按钮的文本颜色
JButton.setForeground(Color.RED);
//设置标签和两个按钮的字体
JButton.setFont(new Font("宋体",Font.PLAIN,100));
//为按钮添加动作事件监听器
JButton.addActionListener(this);
jpanel1.add(JButton);
//将面板容器添加到内容窗格
this.add(jpanel1);
//为窗口添加关闭响应事件
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
image = new ImageIcon("a.jpg").getImage();
//显示窗体
this.setVisible(true);
JButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
final JFrame addframe=new JFrame("huanying");
JButton addbt=new JButton("增加联系人");
addframe.setSize(600,300);
addframe.setLocation(400,400);
addframe.setLayout(new GridLayout(3,1));
//frame.setLayout(new FlowLayout());
addframe.add(addbt);
addbt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
final JFrame addframe=new JFrame("添加");
final JTextField nametext=new JTextField();
JLabel namelabel=new JLabel("姓名");
addframe.add(namelabel);
addframe.add(nametext);
nametext.setBounds(100, 0,400, 100);
namelabel.setBounds(0, 0, 100, 100);
addframe.setLayout(new GridLayout(10,1));
//addframe.setLayout(new FlowLayout());
addframe.setSize(600, 300);
addframe.setVisible(true);
}
});
}
});
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image, 400, 100, null);
}
//主方法
public static void main(String []args)
{
new MainFrame();//创建登陆窗体
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
} 展开
{
private JPanel jpanel1=new JPanel(); //声明面板容器
private JButton JButton=new JButton("确定"); //声明按钮
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth=screenSize.width;
int srceenHeight=screenSize.height;
private Image image;
public MainFrame()
{
//设置窗体相关属性
//设置窗口标题
this.setTitle("--个人通讯录--");
//设置窗口不可拖动大小
this.setResizable(true);
//设置窗口大小
this.setSize(screenWidth,srceenHeight);
//设置窗口居于屏幕中央
//setLocation((screenWidth-getWidth())/2,(srceenHeight-getHeight())/2);
//设置组件的相关属性
//设置面板容器的布局策略为空
jpanel1.setLayout(null);
//设置面板容器背景色
JButton.setBounds(440, 500, 500, 255);
//设置按钮的文本颜色
JButton.setForeground(Color.RED);
//设置标签和两个按钮的字体
JButton.setFont(new Font("宋体",Font.PLAIN,100));
//为按钮添加动作事件监听器
JButton.addActionListener(this);
jpanel1.add(JButton);
//将面板容器添加到内容窗格
this.add(jpanel1);
//为窗口添加关闭响应事件
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
image = new ImageIcon("a.jpg").getImage();
//显示窗体
this.setVisible(true);
JButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
final JFrame addframe=new JFrame("huanying");
JButton addbt=new JButton("增加联系人");
addframe.setSize(600,300);
addframe.setLocation(400,400);
addframe.setLayout(new GridLayout(3,1));
//frame.setLayout(new FlowLayout());
addframe.add(addbt);
addbt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
final JFrame addframe=new JFrame("添加");
final JTextField nametext=new JTextField();
JLabel namelabel=new JLabel("姓名");
addframe.add(namelabel);
addframe.add(nametext);
nametext.setBounds(100, 0,400, 100);
namelabel.setBounds(0, 0, 100, 100);
addframe.setLayout(new GridLayout(10,1));
//addframe.setLayout(new FlowLayout());
addframe.setSize(600, 300);
addframe.setVisible(true);
}
});
}
});
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image, 400, 100, null);
}
//主方法
public static void main(String []args)
{
new MainFrame();//创建登陆窗体
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询