登录站点

用户名

密码

java 基础知识

已有 165 次阅读  2012-03-05 18:37   标签class  java  public  import  method 

(1)GUI组件的继承关系

import javax.swing.*;
import java.awt.*;


public class Test1 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  JButton jbtOK=new JButton("OK");
  System.out.println(jbtOK instanceof JButton);
  System.out.println(jbtOK instanceof AbstractButton);
  System.out.println(jbtOK instanceof JComponent);
  System.out.println(jbtOK instanceof Container);
  System.out.println(jbtOK instanceof Component);
  System.out.println(jbtOK instanceof Object);
 }
}

(2)

import javax.swing.*;
import java.awt.*;

public class My {
 public static void main(String[] args){
  J =new J("My");
    
  //Add a button into the
  java.awt.Container container=.getContentPane();
  JButton jbtOK=new JButton("OK");
  container.add(jbtOK);
  
  .setSize(400, 300);
  //框架居中
  Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
  int screenWidth=screenSize.width;
  int screenHeight=screenSize.height;
  
  int x=(screenWidth-.getWidth())/2;
  int y=(screenHeight-.getHeight())/2;
  .setLocation(x, y);
  
  .setVisible(true);
  .setDefaultCloseOperation(J.EXIT_ON_CLOSE);   
 }
}

(3)

import javax.swing.*;
import java.awt.*;

public class ShowFlowLayout extends J{
 public ShowFlowLayout(){
  //Get the content pane of the
  Container container=getContentPane();
  
  //Set FlowLayout,aligned left with horizontal gap 10
  //and vertical gap 20 between components
  container.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
  
  //Add buttons to the
  for(int i=1;i<=10;i++){
   container.add(new JButton("Component"+i));   
  }
 }
 
 public static void main(String[] args){
  ShowFlowLayout =new ShowFlowLayout();
  .setTitle("ShowFlowLayout");
  .setDefaultCloseOperation(J.EXIT_ON_CLOSE);
  .setSize(800, 300);
  .setVisible(true);
 }
}

(4)

import javax.swing.*;
import java.awt.*;

public class ShowBorderLayout extends J{

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  ShowBorderLayout =new ShowBorderLayout();
  .setTitle("ShowBorderLayout");
  .setDefaultCloseOperation(J.EXIT_ON_CLOSE);
  .setSize(300,200);
  .setVisible(true);

 }
 
 public ShowBorderLayout(){
  //Get the content pane of the
  Container container=this.getContentPane();
  
  //Set BorderLayout with horizontal gap 5 and vertical gap 10
  container.setLayout(new BorderLayout(5,10));
  
  //Add buttons to the
  container.add(new JButton("East"),BorderLayout.EAST);
  container.add(new JButton("South"),BorderLayout.SOUTH);
  container.add(new JButton("West"),BorderLayout.WEST);
  container.add(new JButton("North"),BorderLayout.NORTH);
  container.add(new JButton("Center"),BorderLayout.CENTER);
 }

}

 (5)

import javax.swing.*;
import java.awt.*;

public class TestColor extends J{
 public static void main(String[] args){
  TestColor =new TestColor();
  .setTitle("TestColor");
  .setSize(200, 400);
  .setDefaultCloseOperation(J.EXIT_ON_CLOSE);
  .setVisible(true);
  
  //获取系统全部字体
  GraphicsEnvironment e=GraphicsEnvironment.getLocalGraphicsEnvironment();
  String[] fontnames=e.getAvailableFontFamilyNames();
  for(int i=0;i<fontnames.length;i++){
   System.out.println(fontnames[i]);
  }
 }
 
 public TestColor(){
  Container container=this.getContentPane();
  
  JButton jbtOK=new JButton("确定");
  //颜色
  jbtOK.setBackground(new Color(128,100,100));
  jbtOK.setForeground(new Color(100,1,1));
  
  //字体
  Font font1=new Font("SansSerif",Font.BOLD,16);
  Font font2=new Font("Serif",Font.BOLD+Font.ITALIC,12);
  
  jbtOK.setFont(font2);
  
  container.add(jbtOK);
 }
}
(6)

import javax.swing.*;
import java.awt.*;

public class ShowGridLayout extends J {

 /**
  * @param args
  */
 public static void main(String[] args) {
  ShowGridLayout =new ShowGridLayout();
  .setTitle("ShowGridLayout");
  .setSize(400, 200);
  .setDefaultCloseOperation(J.EXIT_ON_CLOSE);
  .setVisible(true);
 }
 
 public ShowGridLayout(){
  //Get the content pane of the
  Container container=this.getContentPane();
  
  //Set GridLayout ,4 rows,3 columns, and gaps 5 between
  //components horizontally and verically
  container.setLayout(new GridLayout(4,3,5,5));
  
  //Add buttons to the
  for(int i=1;i<=10;i++){
   container.add(new JButton("Component "+i));
  }
 }
}

(7)

import javax.swing.*;
import java.awt.*;

public class TestPanels extends J{
 public static void main(String[] args){
  TestPanels =new TestPanels();
  .setTitle("The front view of a Microwave oven");
  .setDefaultCloseOperation(J.EXIT_ON_CLOSE);
  .setSize(400, 250);
  .setVisible(true);
 }
 
 public TestPanels(){
  //Get the content pane of the
  Container container=this.getContentPane();
  
  //Set BorderLayout for the
  container.setLayout(new BorderLayout());
  
  //Create panel p1 for the buttons and set GridLayout
  JPanel p1=new JPanel();
  p1.setLayout(new GridLayout(4,3));
  
  //Add buttons to the panel
  for(int i=1;i<=9;i++){
   p1.add(new JButton(""+i));
  }
  
  p1.add(new JButton(""+0));
  p1.add(new JButton("Start"));
  p1.add(new JButton("Stop"));
  
  //Create panel p2 to hold a text field and p1
  JPanel p2=new JPanel(new BorderLayout());
  p2.add(new JTextField("Time to be displayed here"),BorderLayout.NORTH);
  p2.add(p1,BorderLayout.CENTER);
  
  //Add p2 and a button to the
  container.add(p2,BorderLayout.EAST);
  container.add(new JButton("Food to be placed here"),BorderLayout.CENTER);  
 } 
}

(8)

/**
 * 字符串居中
 */
import javax.swing.*;
import java.awt.*;

public class CenterMessage extends JPanel{
 public static void main(String[] args){
  J =new J("CenterMessage");
  CenterMessage m=new CenterMessage();
  m.setBackground(Color.white);
  m.setFont(new Font("Californian FB",Font.BOLD,30));
  .getContentPane().add(m);
  .setDefaultCloseOperation(J.EXIT_ON_CLOSE);
  .setSize(600, 300);
  .setVisible(true);
  
 }
 
 protected void paintComponent(Graphics g){
  super.paintComponent(g);
  
  //Get font metrics for the current font
  FontMetrics fm=g.getFontMetrics();
  
  //Find the center location to display
  int stringWidth=fm.stringWidth("Welcome to Java");
  int stringAscent=fm.getAscent();
  
  //Get the position of the leftmost charater in the baseline
  int xCoordinate=getWidth()/2-stringWidth/2;
  int yCoordinate=getHeight()/2+stringAscent/2;
  
  g.drawString("Welcome to Java", xCoordinate, yCoordinate);
 }

}
(9)

import javax.swing.*;
import java.awt.*;

public class MessagePanel extends JPanel {
 private String message="Welcome to Java";
 private int xCoordinate=20;
 private int yCoordinate=20;
 private boolean centered;
 private int interval=10;
 
 public MessagePanel(){
  
 }
 
 public MessagePanel(String message){
  this.message=message;
 }
 
 public String getMessage() {
  return message;
 }
 public void setMessage(String message) {
  this.message = message;
  this.repaint();
 }
 public int getXCoordinate() {
  return xCoordinate;
 }
 public void setXCoordinate(int coordinate) {
  xCoordinate = coordinate;
  this.repaint();
 }
 public int getYCoordinate() {
  return yCoordinate;
 }
 public void setYCoordinate(int coordinate) {
  yCoordinate = coordinate;
  this.repaint();
 }
 public boolean isCentered() {
  return centered;
 }
 public void setCentered(boolean centered) {
  this.centered = centered;
  this.repaint();
 }
 public int getInterval() {
  return interval;
 }
 public void setInterval(int interval) {
  this.interval = interval;
  this.repaint();
 }
 
 protected void paintComponent(Graphics g){
  super.paintComponent(g);
  if(centered){
   //Get font metrics for the current font
   FontMetrics fm=g.getFontMetrics();
   
   //Find the center location to display
   int stringWidth=fm.stringWidth(message);
   int stringAscent=fm.getAscent();
   //Get the position of the leftmost character in the baseline
   xCoordinate=getWidth()/2-stringWidth/2;
   yCoordinate=getHeight()/2+stringAscent/2;
  }
  g.drawString(message, xCoordinate, yCoordinate);
 }
 
 public void moveLeft(){
  xCoordinate-=interval;
  this.repaint();
 }
 
 public void moveRight(){
  xCoordinate+=interval;
  this.repaint();
 }
 
 public void moveUp(){
  yCoordinate-=interval;
  this.repaint();
 }
 
 public void moveDown(){
  yCoordinate+=interval;
  this.repaint();
 }
 
 //Override get method for preferredSize
 public Dimension getPreferedSize(){
  return new Dimension(200,30);
 }
}

 

 

import javax.swing.*;
import java.awt.*;

public class TestMessagePanel extends J {
 public static void main(String[] args){
  TestMessagePanel =new TestMessagePanel();
  .setTitle("TestMessagePanel");
  .setSize(600, 300);
  .setDefaultCloseOperation(J.EXIT_ON_CLOSE);
  .setVisible(true);
 }
 
 public TestMessagePanel(){
  MessagePanel messagePanel1=new MessagePanel("Welcome to Java");
  MessagePanel messagePanel2=new MessagePanel("Java is fun");
  MessagePanel messagePanel3=new MessagePanel("Java is cool");
  MessagePanel messagePanel4=new MessagePanel("I love Java");
  
  messagePanel1.setFont(new Font("SansSerif",Font.ITALIC,20));
  messagePanel2.setFont(new Font("Courier",Font.BOLD,20));
  messagePanel3.setFont(new Font("Times",Font.ITALIC,20));
  messagePanel4.setFont(new Font("Californian",Font.PLAIN,20));
  
  messagePanel1.setBackground(Color.red);
  messagePanel2.setBackground(Color.cyan);
  messagePanel3.setBackground(Color.green);
  messagePanel4.setBackground(Color.white);
  
  messagePanel1.setCentered(true);
  
  this.getContentPane().setLayout(new GridLayout(2,2));
  this.getContentPane().add(messagePanel1);
  this.getContentPane().add(messagePanel2);
  this.getContentPane().add(messagePanel3);
  this.getContentPane().add(messagePanel4);  
 }
}

(10)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class TestActionEvent extends J{
 private JButton jbtOK=new JButton("OK");
 private JButton jbtCancel=new JButton("Cancel");
 
 public TestActionEvent(){
  //Set the Window title
  this.setTitle("TestActionEvent");
  
  //Set FlowLayout manager to arrange the components inside the
  this.getContentPane().setLayout(new FlowLayout());
  
  //Add buttons to the
  this.getContentPane().add(jbtOK);
  this.getContentPane().add(jbtCancel);
  
  //Create a listener object
  ButtonListener btListener=new ButtonListener();
  
  //Register listeners
  jbtOK.addActionListener(btListener);
  jbtCancel.addActionListener(btListener);
 }
 
 public static void main(String[] args){
  TestActionEvent =new TestActionEvent();
  .setDefaultCloseOperation(J.EXIT_ON_CLOSE);
  .setSize(600, 400);
  .setVisible(true);
 }
}

class ButtonListener implements ActionListener{
 public void actionPerformed(ActionEvent e) {
  System.out.println("The "+e.getActionCommand()+" button is clicked at "+new java.util.Date(e.getWhen()));
  
 }
}

(11)

import javax.swing.*;
import java.awt.event.*;

public class TestWindowEvent extends J implements WindowListener{
 
 public static void main(String[] args){
  TestWindowEvent =new TestWindowEvent();
  .setTitle("TestWindowEvent");
  .setSize(500,300);
  .setDefaultCloseOperation(J.EXIT_ON_CLOSE);
  .setVisible(true);  
 }
 
 public TestWindowEvent(){
  this.addWindowListener(this);//register listener
 }

 public void windowActivated(WindowEvent arg0) {
  System.out.println("windowActivated");
 }

 public void windowClosed(WindowEvent arg0) {
  System.out.println("windowClosed");
 }

 public void windowClosing(WindowEvent arg0) {
  System.out.println("windowClosing");
 }

 public void windowDeactivated(WindowEvent arg0) {
  System.out.println("windowDeactivated");
 }

 public void windowDeiconified(WindowEvent arg0) {
  System.out.println("windowDeiconified");
 }

 public void windowIconified(WindowEvent arg0) {
  System.out.println("windowIconified");
 }

 public void windowOpened(WindowEvent arg0) {
  System.out.println("windowOpened");
 }

}

(12)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestMultipleListener extends J implements ActionListener{
 private JButton jbtOK=new JButton("OK");
 private JButton jbtCancel=new JButton("Cancel");
 
 public TestMultipleListener(){
  this.setTitle("TestMultipleListener");
  
  this.getContentPane().setLayout(new FlowLayout());
  
  this.getContentPane().add(jbtOK);
  this.getContentPane().add(jbtCancel);
  
  //Register the as listeners
  jbtOK.addActionListener(this);
  jbtCancel.addActionListener(this);
  
  //Register a second listener for buttons
  SecondListener secondListener=new SecondListener();
  jbtOK.addActionListener(secondListener);
  jbtCancel.addActionListener(secondListener);
 }

 public void actionPerformed(ActionEvent e) {
  System.out.print("First listener: ");
  
  if(e.getActionCommand().equals("OK")){
   System.out.println("the OK button is clicked");
  }else if(e.getActionCommand().equals("Cancel")){
   System.out.println("the Cancel button is clicked");
  }
 }

 public static void main(String[] args){
  TestMultipleListener =new TestMultipleListener();
  .setDefaultCloseOperation(J.EXIT_ON_CLOSE);
  .setSize(400, 200);
  .setVisible(true);
 }
}

class SecondListener implements ActionListener{
 public void actionPerformed(ActionEvent e) {
  System.out.print("Second listener: ");
  
  if(e.getActionCommand().equals("OK")){
   System.out.println("the OK button is clicked");
  }else if(e.getActionCommand().equals("Cancel")){
   System.out.println("the Cancel button is clicked");
  }
 }
}

(13)

/**
 * zhongsanmu
 * @author Administrator
 *
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class IntegerDivision extends J implements ActionListener{
 private JTextField jtfNum1,jtfNum2,jtfResult;
 
 private JButton jbtDiv=new JButton("Divide");
 
 public IntegerDivision(){
  //panel p1 to hold text fields and labels
  JPanel p1=new JPanel();
  p1.setLayout(new FlowLayout());
  p1.add(new JLabel("Number 1"));
  p1.add(jtfNum1=new JTextField(3));
  p1.add(new JLabel("Number 2"));
  p1.add(jtfNum2=new JTextField(3));
  p1.add(new JLabel("Result"));
  p1.add(jtfResult=new JTextField(4));
  jtfResult.setEditable(false);//
  jtfResult.setHorizontalAlignment(SwingConstants.RIGHT);
  
  this.getContentPane().add(p1,BorderLayout.CENTER);
  this.getContentPane().add(jbtDiv,BorderLayout.SOUTH);
  
  //register listener
  jbtDiv.addActionListener(this);
 }

 public void actionPerformed(ActionEvent e) {
  if(e.getSource()==jbtDiv){
   //Get numbers
   int num1=Integer.parseInt(jtfNum1.getText().trim());
   int num2=Integer.parseInt(jtfNum2.getText().trim());
   
   try {
    int result = num1 / num2;
    //set result in jtfResult
    jtfResult.setText(String.valueOf(result));
   } catch (RuntimeException ex) {
    JOptionPane.showMessageDialog(this, ex.getMessage(),"Operation error",JOptionPane.ERROR_MESSAGE);
   }   
  }  
 }
 
 public static void main(String[] args){
  IntegerDivision =new IntegerDivision();
  .setTitle("IntegerDivision");
  .setSize(500, 100);
  .setDefaultCloseOperation(J.EXIT_ON_CLOSE);
  .setVisible(true);
 }

}

上一篇: C语言深入浅出 :回味经典 下一篇: 星盘和古希腊人的投影几何

分享 举报