BoxLayout組件
/**
?* @(#)TestBoxLayout.java
?*
?* TestBoxLayout application
?*
?* @author
?* @version 1.00 2007/1/25
?*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
public class TestBoxLayout extends JFrame implements ActionListener
{
?JTextField jtf=new JTextField();?
??? public TestBoxLayout()
??? {
??? ?this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
??? ?Container c=this.getContentPane();
??? ?
??? ?JPanel jpl1=new JPanel();
??? ?JPanel jpl2=new JPanel();
??? ?JPanel jpl3=new JPanel();
??? ?JPanel jpl4=new JPanel();
??? ?
??? ?//BoxLayout blo=new BoxLayout(jtf,BoxLayout.LINE_AXIS);
??? ?BoxLayout blo1=new BoxLayout(jpl1,BoxLayout.X_AXIS);
??? ?BoxLayout blo2=new BoxLayout(jpl2,BoxLayout.X_AXIS);
??? ?BoxLayout blo3=new BoxLayout(jpl3,BoxLayout.X_AXIS);
??? ?BoxLayout blo4=new BoxLayout(jpl4,BoxLayout.X_AXIS);
??? ?/*
??? ?blo1.maximumLayoutSize(jpl1);
??? ?blo2.maximumLayoutSize(jpl2);
??? ?blo3.maximumLayoutSize(jpl3);
??? ?blo4.maximumLayoutSize(jpl4);
??? ?*/
??? ?jpl1.setLayout(blo1);
??? ?jpl2.setLayout(blo2);
??? ?jpl3.setLayout(blo3);
??? ?jpl4.setLayout(blo4);
??? ?
??? ?JButton jb=null;
???
??? ?for(int i=0;i<4;i++)
??? ?{
??? ??String str=new String("123+");
??? ??Character ch=str.charAt(i);
??? ??jb=new JButton(ch.toString());
??? ??jb.setSize(100,100);
??? ??jpl1.add(jb);
??? ??jb.addActionListener(this);
??? ?}
??? ?for(int i=0;i<4;i++)
??? ?{
??? ??String str=new String("456-");
??? ??Character ch=str.charAt(i);
??? ??jb=new JButton(ch.toString());
??? ??jpl2.add(jb);
??? ??jb.addActionListener(this);
??? ?}
?? ???
??? ?for(int i=0;i<4;i++)
??? ?{
??? ??String str=new String("789*");
??? ??Character ch=str.charAt(i);
??? ??jb=new JButton(ch.toString());
??? ??jpl3.add(jb);
??? ??jb.addActionListener(this);
??? ?}
??? ?for(int i=0;i<4;i++)
??? ?{
??? ??String str=new String("0.=\\");
??? ??Character ch=str.charAt(i);
??? ??jb=new JButton(ch.toString());
??? ??jpl4.add(jb);
??? ??jb.addActionListener(this);
??? ?}
??? ?c.setLayout(new GridLayout(5,1));
??? ?c.add(jtf);
??? ?c.add(jpl1);
??? ?c.add(jpl2);
??? ?c.add(jpl3);
??? ?c.add(jpl4);
??? }
??? public static void main(String[] args)
??? {
??? ?
??? ?// TODO, add your application code
??? ?System.out.println("Hello World!");
??? ?TestBoxLayout mainFrame=new TestBoxLayout();
??? ?mainFrame.setTitle("TestBoxLayout");
??? ?mainFrame.setBounds(300,200,200,200);
??? ?mainFrame.setVisible(true);
??? }
?/**
? * Method actionPerformed
? *
? *
? * @param e
? *
? */
?public void actionPerformed(ActionEvent e)
?{
??// TODO: Add your code here
??jtf.setText(jtf.getText()+e.getActionCommand());
??//d.setText(jfd.getText()+e.getActionCommand());
?}
}
posted on 2007-01-26 15:16 大頭劍客 閱讀(849) 評論(0) 編輯 收藏 所屬分類: 學習筆記