java添加两个按钮_java – 从另一个类向一个JButton添加一个ac...

所以我有两个类testPanel和testFrame.所有按钮都在testPanel类中.我想将ActionListeners添加到testFrame类的Jbuttons中.我该怎么做呢?

パ:

public class testPanel extends JPanel{

JLabel codeLbl = new JLabel("Code");

JLabel titleLbl = new JLabel("Title");

JLabel priceLbl = new JLabel("Price");

JTextField codeTxt = new JTextField(20);

JTextField titleTxt = new JTextField(20);

JTextField priceTxt = new JTextField(20);

JButton addBtn = new JButton("Add");

JButton updateBtn = new JButton("Update");

JButton delBtn = new JButton("Delete");

JButton exitBtn = new JButton("Exit");

JButton firstBtn = new JButton("First");

JButton prevBtn = new JButton("Previous");

JButton nextBtn = new JButton("Next");

JButton lastBtn = new JButton("Last");

JPanel info = new JPanel();

JPanel buttons = new JPanel();

public testPanel(){

info.setLayout(new GridLayout(3,2));

info.add(codeLbl);

info.add(codeTxt);

info.add(titleLbl);

info.add(titleTxt);

info.add(priceLbl);

info.add(priceTxt);

buttons.setLayout(new GridLayout(2,4));

buttons.add(addBtn);

buttons.add(updateBtn);

buttons.add(delBtn);

buttons.add(exitBtn);

buttons.add(firstBtn);

buttons.add(prevBtn);

buttons.add(nextBtn);

buttons.add(lastBtn);

JPanel container = new JPanel();

container.setLayout(new BorderLayout());

container.add(BorderLayout.CENTER, info);

container.add(BorderLayout.SOUTH, buttons);

add(container);

}

}

testFrame:

public class testFrame extends JFrame{

JPanel p = new testPanel();

public testFrame(){

super("BLAH");

this.getContentPane().add(p);setVisible(true);

pack();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args){

new testFrame();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值