【发布时间】:2012-01-29 11:21:56
【问题描述】:
我有两个问题:
1 .为什么当我执行代码时,当我单击按钮 P 时它不显示 println。 2. 我尝试使用 JLabel 制作背景,效果很好,但它并没有涵盖所有的 JFrame。我用 try and catch 尝试了 JFrame,但它没有显示它。
JPanel L1 = new JPanel();
JButton P = new JButton("Open");
JButton P1 = new JButton("Cancel");
Dimension D = new Dimension(80 , 30);
Container C = getContentPane();
JLabel Label2 = new JLabel(new
super.setTitle("Ismail Application");
//Buttons
//Button 1
P.setToolTipText("Click To Open");
P.setPreferredSize(D);
//Button 2
P1.addActionListener(this);
P1.setToolTipText("Click to exit program");
P1.setPreferredSize(D);
//Adding Components
L1.add(P, BorderLayout.WEST);
L1.add(P1, BorderLayout.EAST);
add(L1, BorderLayout.SOUTH);
P1.addActionListener(this);
P.addActionListener(this);
//Labels
Label2.setLayout(null);
Label2.setSize(400,300);
Label2.setToolTipText("This is the Background");
add(Label2, BorderLayout.NORTH);
}
public void actionPerformed (ActionEvent e)
{
if(e.getSource() == P)
{
System.out.println("not working");
}
if(e.getSource() == P1){
}
}
希望大家帮忙
谢谢
【问题讨论】:
-
您的代码不完整或无法编译。请至少提供一个工作示例。
-
@ismail zaidi : 请遵守 Java 命名约定,阅读代码片段时太难理解了。问候
标签: java swing jframe jlabel actionlistener