⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jtwolist.java

📁 设计模式Java版
💻 JAVA
字号:
//Demonstratiob of simple Two-list program
//using JFC controls

import java.awt.*;
import java.awt.event.*;
//swing classes
import com.sun.java.swing.text.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.tree.*;
import com.sun.java.swing.border.*;


public class JTwoList extends JFrame
   implements ActionListener
{
   private JButton Add, MoveRight, MoveLeft;
   private JawtList leftList, rightList;
   private TextField txt;

   public JTwoList()
   {
      super("Two Lists");
      setLF();          //set look and feel
      setCloseClick();  //Window exits on close-click
      setGUI();
   }
   //--------------------------------------------
   private void setGUI()
   {
      getContentPane().setLayout(new GridLayout(1,2));  //two columns
      setBackground(Color.lightGray);
      JPanel pLeft = new JPanel();
      JPanel pRight = new JPanel();
      getContentPane().add(pLeft);
      getContentPane().add(pRight);
      pLeft.setLayout(new BorderLayout());
      
      JPanel pTop = new JPanel();
      pLeft.add("North", pTop);
      txt = new TextField(15);
      pTop.add(txt);
      Add = new JButton("Insert");
      Add.setMargin(new Insets(0,0,0,0));

      pTop.add(Add);
      
      JPanel rBorder = new JPanel();
      rBorder.setLayout(new GridLayout(2,1));
      MoveRight = new JButton("Add --->");
      MoveLeft = new JButton("<--- Remove");
      
      JPanel rbTop = new JPanel();
      rbTop.add(MoveRight);
      rBorder.add(rbTop);
      
      JPanel rbBot = new JPanel();
      rbBot.add(MoveLeft);
      rBorder.add(rbBot);
      pLeft.add("East", rBorder);

      leftList = new JawtList(15);
      pLeft.add("Center", leftList);

      rightList = new JawtList(15);
      pRight.setLayout(new BorderLayout());
      pRight.add("Center", rightList);
      
      //Add button action listenes
      Add.addActionListener(this);
      MoveRight.addActionListener(this);
      MoveLeft.addActionListener(this);

      setSize(new Dimension(400, 300));
      setVisible(true);
   }
   //-----------------------------------------  
   private void setCloseClick()
   {
      //create window listener to respond to window close click
      addWindowListener(new WindowAdapter() 
       {
	    public void windowClosing(WindowEvent e) {System.exit(0);}
	    });
   }
   //------------------------------------------
   private void setLF()
   {
   // Force SwingApp to come up in the System L&F
	String laf = UIManager.getSystemLookAndFeelClassName();
	try {
       UIManager.setLookAndFeel(laf);
   	 }
       catch (UnsupportedLookAndFeelException exc) 
         {System.err.println("Warning: UnsupportedLookAndFeel: " + laf);}
       catch (Exception exc) {System.err.println("Error loading " + laf + ": " + exc);
	   }
   }

   //---------------------------------------------
   public void actionPerformed(ActionEvent e)
   {
      JButton b = (JButton)e.getSource();
      if(b == Add)
         addName();
      if(b == MoveRight)
         moveNameRight();
      if(b == MoveLeft)
         moveNameLeft();
   }
   //--------------------------------------------
   private void addName()
   {
      if (txt.getText().length() > 0) 
         {
         leftList.add(txt.getText());
         txt.setText("");
         }
   }
   //--------------------------------------------
   private void moveNameRight()
   {
     String sel[] = leftList.getSelectedItems();
     
     if (sel != null)        
     {
     rightList.add(sel[0]);
     leftList.remove(sel[0]);
     }
   }
   //--------------------------------------------
   public void moveNameLeft()
   {
    String sel[] = rightList.getSelectedItems();
     if (sel != null)        
     {
     leftList.add(sel[0]);
     rightList.remove(sel[0]);
     }
   }
   //--------------------------------------------
   static public void main(String argv[])
   {
      new JTwoList();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -