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

📄 test.java

📁 《Java2图形设计卷II:Swing》配套光盘源码
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.util.*;

public class Test extends JFrame {
	private JTextArea textArea = createTextArea();
	private Action[] actions = textArea.getActions();

	private JList actionList = createActionList(actions);
	private JSplitPane splitPane = new JSplitPane(
								JSplitPane.HORIZONTAL_SPLIT, 
								new JScrollPane(textArea),
								new JScrollPane(actionList));
	public Test() {
		Container contentPane = getContentPane();

		splitPane.setDividerLocation(150);

		contentPane.add(splitPane, BorderLayout.CENTER);
		contentPane.add(GJApp.getStatusArea(), 
						BorderLayout.SOUTH);

	}
	private JList createActionList(Action[] actions) {
		DefaultListModel model = new DefaultListModel();
		final JList list = new JList(model);

		list.setSelectionMode(
							ListSelectionModel.SINGLE_SELECTION);

		for(int i=0; i < actions.length; ++i) {
			model.addElement(actions[i]);
		}

		list.setCellRenderer(new DefaultListCellRenderer() {
			public Component getListCellRendererComponent(
									JList list, Object value,
									int index, boolean isSelected,
									boolean cellHasFocus) {
				super.getListCellRendererComponent(list, value,
					index, isSelected, cellHasFocus);

				Action action = (Action)value;
				setText((String)action.getValue(Action.NAME));

				return this;
			}
		});
		list.addListSelectionListener(new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent e) {
				if(!e.getValueIsAdjusting()) {
					Action source = 
					   (Action)actionList.getSelectedValue();

					textArea.requestFocus();
					source.actionPerformed(null);

					GJApp.showStatus("Action: " + 
							(String)source.getValue(Action.NAME));
				}
			}
		});

		return list;
	}
	private JTextArea createTextArea() {
		JTextArea textArea = new JTextArea(
			  "line one\nline two\nline three\nline four");

		textArea.setFont(new Font("Dialog", Font.ITALIC, 24));
		textArea.getCaret().setBlinkRate(0);
		return textArea;
	}
	public static void main(String args[]) {
		GJApp.launch(new Test(), 
					"Text Component Actions",300,300,450,300);
	}
}
class GJApp extends WindowAdapter {
	static private JPanel statusArea = new JPanel();
	static private JLabel status = new JLabel(" ");
	static private ResourceBundle resources;

	public static void launch(final JFrame f, String title,
							  final int x, final int y, 
							  final int w, int h) {
		launch(f,title,x,y,w,h,null);	
	}
	public static void launch(final JFrame f, String title,
							  final int x, final int y, 
							  final int w, int h,
							  String propertiesFilename) {
		f.setTitle(title);
		f.setBounds(x,y,w,h);
		f.setVisible(true);

		statusArea.setBorder(BorderFactory.createEtchedBorder());
		statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		statusArea.add(status);
		status.setHorizontalAlignment(JLabel.LEFT);

		f.setDefaultCloseOperation(
							WindowConstants.DISPOSE_ON_CLOSE);

		if(propertiesFilename != null) {
			resources = ResourceBundle.getBundle(
						propertiesFilename, Locale.getDefault());
		}

		f.addWindowListener(new WindowAdapter() {
			public void windowClosed(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	static public JPanel getStatusArea() {
		return statusArea;
	}
	static public void showStatus(String s) {
		status.setText(s);
	}
	static Object getResource(String key) {
		if(resources != null) {
			return resources.getString(key);
		}
		return null;
	}
}

⌨️ 快捷键说明

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