📄
字号:
例8.12 ComboBoxDemo.java的源代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxDemo extends JPanel{
JLabel picture;
String[] petStrings = {"Bird", "Cat", "Dog","Rabbit","Pig"};
JComboBox petList = new JComboBox(petStrings);
public ComboBoxDemo(){
petList.setSelectedIndex(4);
petList.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String petName = (String)petList.getSelectedItem();
picture.setIcon(new ImageIcon("images/" + petName + ".gif"));
}
});
picture = new JLabel(new ImageIcon("images/" + petStrings[petList.getSelectedIndex()] +".gif"));
picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
setLayout(new BorderLayout());
add(petList, BorderLayout.NORTH);
add(picture, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
public static void main(String[] args) {
JFrame frame = new JFrame("ComboBoxDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ComboBoxDemo());
frame.pack();
frame.setVisible(true);
}
}
例8.13 ListDemo.java的源代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ListDemo extends JFrame implements ListSelectionListener {
private JList list;
private DefaultListModel listModel;
private static final String addString = "添加";
private static final String moveString = "删除";
private JButton addButton,moveButton;
private JTextField swingName;
public ListDemo() {
super("ListDemo");
listModel = new DefaultListModel(); //创建列表框模型对象
listModel.addElement("JFrame");
listModel.addElement("JApplet");
listModel.addElement("JButton");
listModel.addElement("JLabel");
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
JScrollPane sp = new JScrollPane(list);//为列表框添加滚动条
JButton addButton = new JButton(addString);
addButton.setActionCommand(addString);
addButton.addActionListener(new AddListener());//注册自定义的监听器对象AddListener
moveButton = new JButton(moveString);
moveButton.setActionCommand(moveString);
moveButton.addActionListener(new MoveListener());//注册自定义的监听器对象moveListener
swingName = new JTextField(10);
swingName.addActionListener(new AddListener());
String name = listModel.getElementAt(list.getSelectedIndex()).toString();
swingName.setText(name);
JPanel buttonPane = new JPanel();
buttonPane.add(swingName);
buttonPane.add(addButton);
buttonPane.add(moveButton);
Container cp = getContentPane();
cp.add(sp,BorderLayout.CENTER);//也可写为cp.add(sp,”Center”);
cp.add(buttonPane,BorderLayout.SOUTH);//也可写为cp.add(buttonPane,”South”);
}
class MoveListener implements ActionListener{
public void actionPerformed(ActionEvent e){
44 int index = list.getSelectedIndex(); //获取列表框当前选项索引号
listModel.remove(index);
int size = listModel.getSize();
if (size == 0){
moveButton.setEnabled(false);
} else {
if (index == listModel.getSize())
index--;
list.setSelectedIndex(index);
}
}
}
class AddListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (swingName.getText().equals("")) {
Toolkit.getDefaultToolkit().beep();
return;
}
int index = list.getSelectedIndex();
int size = listModel.getSize();
if (index == -1 || (index+1 == size)) {
listModel.addElement(swingName.getText());
list.setSelectedIndex(size);
} else {
listModel.insertElementAt(swingName.getText(), index+1);
list.setSelectedIndex(index+1);
}
}
}
public void valueChanged(ListSelectionEvent e){
if (e.getValueIsAdjusting() == false){
if (list.getSelectedIndex() == -1){
moveButton.setEnabled(false);
swingName.setText("");
}else {
moveButton.setEnabled(true);
String name = list.getSelectedValue().toString();
swingName.setText(name);
}
}
}
public static void main(String s[]) {
JFrame frame = new ListDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
例8.14 ScrollBarSliderDemo.java的源代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class ScrollBarSliderDemo extends JPanel{
private JLabel sLabel;
private JScrollBar scroll;
private JSlider slider;
public ScrollBarSliderDemo(){
sLabel = new JLabel("这里提示您触发事件和当前值",JLabel.CENTER);
scroll = new JScrollBar(Adjustable.HORIZONTAL,50,0,0,100);
scroll.setUnitIncrement(2);
scroll.addAdjustmentListener(new MyScrollListener());
slider = new JSlider(JSlider.VERTICAL);
slider.setMajorTickSpacing(25);
slider.setMinorTickSpacing(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.addChangeListener(new MySliderListener());
setLayout(new BorderLayout());
add(sLabel,"Center");
add(scroll,"South");
add(slider,"East");
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));//创建空白边框
}
class MyScrollListener implements AdjustmentListener{
public void adjustmentValueChanged(AdjustmentEvent e){
sLabel.setText("触发滚动条事件,当前值为:" + scroll.getValue());
}
}
class MySliderListener implements ChangeListener{
public void stateChanged(ChangeEvent e){
sLabel.setText("触发滑动块事件,当前值为:" + slider.getValue());
}
}
public static void main(String[] args){
JFrame frame = new JFrame("ScrollBarSliderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new ScrollBarSliderDemo());
frame.setSize(400,300);
frame.setVisible(true);
}
}
例8.15 SimpleDialogDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleDialogDemo extends JPanel implements ActionListener{
5 static JFrame Frame; //定义一个静态窗口对象Frame
String title = "自定义对话框";
7 DialogDemo Dialog; //定义一个自定义DialogDemo类(88行)对象Dialog
private JLabel Label;
private ImageIcon icon = new ImageIcon("images/javalogo.gif");
final String messageDialog = "显示消息对话框";
final String confirmDialog = "显示确认对话框";
final String comboxDialog = "显示输入对话框";
final String optionDialog = "显示选项对话框";
final String modelDialog = "显示自定义对话框";
JButton[] Buttons;
public SimpleDialogDemo(){
final int numButtons = 5;
Dialog = new DialogDemo(Frame,title,false);//使用自定义类DialogDemo的构造方法
Label = new JLabel("显示用户操作结果");
Buttons = new JButton[numButtons];//创建一个按钮数组
Buttons[0] = new JButton(messageDialog);
Buttons[0].setActionCommand(messageDialog);//设置事件源的命令名称
Buttons[1] = new JButton(confirmDialog);
Buttons[1].setActionCommand(confirmDialog);
Buttons[2] = new JButton(comboxDialog);
Buttons[2].setActionCommand(comboxDialog);
Buttons[3] = new JButton(optionDialog);
Buttons[3].setActionCommand(optionDialog);
Buttons[4] = new JButton(modelDialog);
Buttons[4].setActionCommand(modelDialog);
setLayout(new GridLayout(0,1));//设置为网格布局,0行1列
for(int i = 0;i < numButtons;i++){
Buttons[i].addActionListener(this);
add(Buttons[i]);
}
add(Label);
}
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
if(command == messageDialog){
JOptionPane.showMessageDialog(Frame,"这是消息对话框");//显示消息对话框
}
else if(command == confirmDialog){
int n = JOptionPane.showConfirmDialog(Frame,
"来一杯茶吗?","确认对话框",JOptionPane.YES_NO_OPTION);//显示确认对话框
if(n == JOptionPane.YES_OPTION)
Label.setText("恩,谢谢!");
else if(n == JOptionPane.NO_OPTION)
Label.setText("哦,不用了");
}
else if(command == comboxDialog){
Object[] possibilities = {"蓝","黄","红"};
String s = (String)JOptionPane.showInputDialog(Frame,
"选择你喜欢的颜色","输入对话框", JOptionPane.PLAIN_MESSAGE,
icon,possibilities,"possibilities[0]");//显示输入对话框
if(s != null){
s = s.trim();
if(s.length() > 0)
Label.setText("我喜欢 " + s + " 色!");
}
}
else if(command == optionDialog){
String string1 = "我喜欢";
String string2 = "我不喜欢";
Object[] options = {string1,string2};
int n = JOptionPane.showOptionDialog(Frame,
"你喜欢绿色吗?","选项对话框",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,
null,options,string1);
if(n == JOptionPane.YES_OPTION)
Label.setText("喜欢");
else if(n == JOptionPane.NO_OPTION)
Label.setText("不喜欢");
}
else if(command == modelDialog){
Label.setText("自定义对话框");
Dialog.show();
}
}
public static void main(String[] args){
Frame = new JFrame("DialogDemo");
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.getContentPane().add(new SimpleDialogDemo());
Frame.setSize(300,200);
Frame.show();
}
}
class DialogDemo extends JDialog implements ActionListener{//创建继承于JDialog类的对话框类
public DialogDemo(JFrame Frame,String title,boolean modal){
super(Frame,title,modal);
JPanel Panel = new JPanel();
JButton open = new JButton("打开");
open.setActionCommand("open");
open.addActionListener(this);
JButton close = new JButton("关闭");
close.setActionCommand("close");
close.addActionListener(this);
JLabel Label = new JLabel("显示信息");
BorderLayout layout = new BorderLayout();
Panel.setLayout(layout);
Panel.add(open,BorderLayout.NORTH);
Panel.add(close,BorderLayout.CENTER);
Panel.add(Label,BorderLayout.SOUTH);
getContentPane().add(Panel);
pack();
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("open"))
JOptionPane.showMessageDialog(null,"这是一个模态对话框。");
else if(e.getActionCommand().equals("close"))
dispose();
}
}
例8.16 MenuDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MenuDemo extends JFrame implements ActionListener{
private JPopupMenu popup;
private JTextArea text;
private JScrollPane scrollPane;
private JMenuBar menuBar;
10 private JMenuItem menuItem;
11 private JMenu menu,subMenu;
12 private JCheckBoxMenuItem cbMenuItem;
private JRadioButtonMenuItem rbMenuItem1,rbMenuItem2;
public MenuDemo(){
super("MenuDemo");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane();
text = new JTextArea(5,30);
scrollPane = new JScrollPane(text);
cp.add(scrollPane,BorderLayout.CENTER);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
menu = new JMenu("文件");
menuBar.add(menu);
menuItem = new JMenuItem("新建");
menuItem.addActionListener(this);
menuItem.setActionCommand("新建");
menu.add(menuItem);
menuItem = new JMenuItem("打开");
menuItem.addActionListener(this);
menuItem.setActionCommand("打开");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem("退出");
menuItem.addActionListener(this);
menuItem.setActionCommand("exit");
menu.add(menuItem);
menu = new JMenu("编辑");
menuBar.add(menu);
menuItem = new JMenuItem("剪切",new ImageIcon("images/cut.gif"));
menuItem.addActionListener(this);
menuItem.setActionCommand("剪切");
menu.add(menuItem);
menu.addSeparator();
subMenu = new JMenu("选项");
menu.add(subMenu);
ButtonGroup group = new ButtonGroup();
rbMenuItem1 = new JRadioButtonMenuItem("单选框1");
rbMenuItem1.addActionListener(this);
rbMenuItem1.setActionCommand("单选框1");
rbMenuItem1.setSelected(true);
rbMenuItem2 = new JRadioButtonMenuItem("单选框2");
rbMenuItem2.addActionListener(this);
rbMenuItem2.setActionCommand("单选框2");
group.add(rbMenuItem1);
group.add(rbMenuItem2);
subMenu.add(rbMenuItem1);
subMenu.add(rbMenuItem2);
subMenu.addSeparator();
cbMenuItem = new JCheckBoxMenuItem("复选框");
cbMenuItem.addActionListener(this);
cbMenuItem.setActionCommand("复选框");
subMenu.add(cbMenuItem);
popup = createPopupMenu(new Object[] {"Cut", "Copy", "Paste"}, this);
text.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent evt) {
if (evt.isPopupTrigger())
popup.show(evt.getComponent(), evt.getX(), evt.getY());
}
});
}
public void actionPerformed(ActionEvent e){
String arg = e.getActionCommand();
if(arg.equals("exit")) System.exit(0);
text.append(" " + arg);
}
public static JPopupMenu createPopupMenu(Object[] items,Object target){
JPopupMenu pMenu = new JPopupMenu();
for(int i =0;i < items.length;i++){
if(items[i] == null)
pMenu.addSeparator();
else
pMenu.add(createMenuItem(items[i], target));
}
return pMenu;
}
public static JMenuItem createMenuItem(Object item, Object target){
JMenuItem r = null;
if (item instanceof String)
r = new JMenuItem((String)item);
else if (item instanceof JMenuItem)
r = (JMenuItem)item;
else return null;
if (target instanceof ActionListener)
r.addActionListener((ActionListener)target);
return r;
}
public static void main(String[] args){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new MenuDemo();
frame.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -