📄 43.txt
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.geom.*;
import java.util.*;
// 窗口事件 和 文本框事件
public class EventTest
{
public static void main(String[] args)
{
EventFrame frame = new EventFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
frame.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
// define Frame
class EventFrame extends JFrame
{
Container contentPane;
public EventFrame()
{
setSize(WIDTH, HEIGHT);
setTitle("窗口测试");
// create a panel
EventPanel panel = new EventPanel();
// add panel to frame
contentPane = getContentPane();
//JScrollPane scrollpane = new JScrollPane (panel.showField);
contentPane.add(panel,BorderLayout.CENTER);
}
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
}
// define panel
class EventPanel extends JPanel
{
//private String s ;
JTextField textEditor;
JTextField pswField;
JTextArea showField;
public int n = 0;
public EventPanel()
{
JButton yellowButton = new JButton("Background Turn to Yellow");
JButton blueButton = new JButton("Backgound Turn to Blue");
textEditor = new JTextField("",15);
pswField = new JTextField("",15);
showField = new JTextArea (5,30);
showField.setLineWrap(true);
JScrollPane scrollpane = new JScrollPane (showField);
//Container contentPane = showField.getcogetContentPane();
//contentPane.add(scrollpane,BorderLayout.CENTER);
//add(copyButton);
//add(pasteButton);
add(yellowButton);
add(blueButton);
add(textEditor);
add(pswField);
makeButton("Copy L",0);
makeButton("Copy R",1);
add(showField);
makeButton("Clear",8);
makeButton("Exit",9);
/// Create action
ColorAction yellowAction = new ColorAction(Color.yellow); //ColorAction inside class
ColorAction blueAction = new ColorAction(Color.blue);
//TextAction copyAction = new TextAction(textEditor.getText(),false);
//TextAction pasteAction = new TextAction(textEditor.getText(),true);
/// association actions with buttons
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
}
// make button
public void makeButton(String name,final int i)
{
JButton button = new JButton(name);
add(button);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (i==0)
{
n++;
String s = showField.getText();
pswField.setText(textEditor.getText());
showField.setText(s+n+".复制左边到右边:"+textEditor.getText()+'\n');
}
else if(i==1)
{
n++;
String s = showField.getText();
textEditor.setText(pswField.getText());
showField.setText(s+n+".复制右边到左边:"+textEditor.getText()+'\n');
}
else if (i==8)
{
n = 0;
showField.setText("");
}
else if (i==9)
System.exit(0);
}
});
}
// define ActionListener
////* color
class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
repaint();
}
private Color backgroundColor;
}
//*/
// text copy paste
class TextAction implements ActionListener
{
public TextAction(String s,boolean b)
{
text=s;
bool=b;
}
public void actionPerformed(ActionEvent event)
{
if(bool)
pswField.setText(text);
}
private String text;
private boolean bool;
}
}
//*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -