用java创建显示一个窗口,按表格布局(5,1)加入五个按钮单击一一个按钮显示一种
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* JButton 布局与动作演示
* @author HARDNEEDL
*/
final class ButtonsDemo extends JFrame{
final static private Dimension SIZE = new Dimension(800,600);
public String getTitle() {return "ButtonsDemo";}
public Dimension getPreferredSize() {return SIZE;}
public Dimension getMinimumSize() {return SIZE;}
public Dimension getMaximumSize() {return SIZE;}
public Dimension getSize() {return SIZE;}
ButtonsDemo() throws HeadlessException {
init();
attachListeners();
doLay();
setVisible(true);
}
private void init(){
}
private void attachListeners(){
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
}
private void doLay(){
setLayout(new GridLayout(5,1,4,4));
Container container = getContentPane();
for (int i = 0; i < 5; i++)
container.add(new JButton(new ButtonAction(i)));
pack();
}
private class ButtonAction extends AbstractAction{
public ButtonAction(int i) {
super(Integer.toString(i));
}
public void actionPerformed(ActionEvent e) {
Component c = (Component) e.getSource();
JOptionPane.showMessageDialog(c, e);
}
}
public static void main(String... args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(ButtonsDemo::new);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |