第9章 反转按钮、复选框和单选框

简介:  第9章 反转按钮、复选框和单选框  本章介绍三种Swing按钮:反转按钮、复选按钮和单选按钮,它们分别由JToggleButton、JCheckBox和JRadioButton类代表。

 

9章 反转按钮、复选框和单选框

  本章介绍三种Swing按钮:反转按钮、复选按钮和单选按钮,它们分别由JToggleButtonJCheckBoxJRadioButton类代表。
  这三种按钮都是AbstractButton类的最终扩展。AbstractButton类实现了这三个类提供的几乎所有功能。有关AbstractButton类的更多信息,请参见第8标签与按钮

9.1 JToggleButton

 

9-1 JToggleButton的一个简单例子

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

public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
ImageIcon icon = new ImageIcon("togglebuttonImage.gif");

JToggleButton button = new JToggleButton(icon);
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
}
}

9.1.1 JToggleButton属性

 

9.1.2 JToggleButton事件

 

9-2 用一个子项监听器来处理反转按钮的选取操作

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

public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
ImageIcon icon = new ImageIcon("togglebuttonImage.gif");
final JToggleButton button = new JToggleButton(icon);

contentPane.setLayout(new FlowLayout());
contentPane.add(button);

button.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int state = e.getStateChange();
String s;

if(state == ItemEvent.SELECTED) s = "selected";
else s = "deselected";

showStatus("Item Event: " + s);
}
});
}
}

9.1.3 JToggleButton类总结

 

9-3 创建反转按钮

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

public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
Icon icon = new ImageIcon("beach_umbrella.gif");
JToggleButton button_1 = new JToggleButton(),
button_2 = new JToggleButton(icon),
button_3 = new JToggleButton(icon, true),
button_4 = new JToggleButton("beach"),
button_5 = new JToggleButton("beach",true),
button_6 = new JToggleButton("beach",icon),
button_7 = new JToggleButton("beach",icon,
true);
contentPane.setLayout(new FlowLayout());
contentPane.add(button_1);
contentPane.add(button_2);
contentPane.add(button_3);
contentPane.add(button_4);
contentPane.add(button_5);
contentPane.add(button_6);
contentPane.add(button_7);
}
}

9.1.4 AWT兼容

 

9.2 按钮框

 

9-4 为相互排斥的选取行为使用一个按钮组

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

public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
ButtonGroup group = new ButtonGroup();

JToggleButton[] buttons = new JToggleButton[] {
new JToggleButton(new ImageIcon("ballot_box.gif")),
new JToggleButton(new ImageIcon("palette.gif")),
new JToggleButton(new ImageIcon("light_bulb1.gif")),
new JToggleButton(new ImageIcon("dining.gif")),
new JToggleButton(new ImageIcon("scissors.gif")),
new JToggleButton(new ImageIcon("tricycle.gif")),
};
Box box = Box.createVerticalBox();

for(int i=0; i < buttons.length; ++i) {
group.add(buttons[i]);
box.add(Box.createVerticalStrut(5));
box.add(buttons[i]);
}
box.add(Box.createVerticalStrut(5));
contentPane.add(box);
}
}

9.3 复选框

 

9-5 运行中的Swing复选框

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

public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();
contentPane.add(new PrintOptionsPanel(this), "Center");
}
}
class PrintOptionsPanel extends JPanel {
JCheckBox oddPages, evenPages, collate, lastFirst;
Listener listener = new Listener();
JApplet applet;

public PrintOptionsPanel(JApplet applet) {
this.applet = applet;
oddPages = new JCheckBox("Odd Pages");
evenPages = new JCheckBox("Even Pages");
collate = new JCheckBox("Collate");
lastFirst = new JCheckBox("Last Page First");

oddPages.addItemListener (listener);
evenPages.addItemListener(listener);
collate.addItemListener (listener);
lastFirst.addItemListener(listener);

add(oddPages);
add(evenPages);
add(collate);
add(lastFirst);
}
class Listener implements ItemListener {
public void itemStateChanged(ItemEvent event) {
applet.showStatus(
"Odd Pages: " + oddPages.isSelected() + ", " +
"Even Pages: " + evenPages.isSelected() + ", " +
"Collate: " + collate.isSelected() + ", " +
"Last Page First: " + lastFirst.isSelected());
}
}
}

9.3.1 JCheckBox属性

 

9.3.2 JCheckBox事件

 

9.3.3 JCheckBox类总结

 

9-6 实例化复选框

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

public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
Icon icon = new ImageIcon("bulb.gif");
JCheckBox[] checkboxes = new JCheckBox[] {
new JCheckBox(),
new JCheckBox(icon),
new JCheckBox(icon, true),
new JCheckBox("idea!"),
new JCheckBox("idea!",true),
new JCheckBox("idea!",icon),
new JCheckBox("idea!",icon, true)
};
contentPane.setLayout(new FlowLayout());

for(int i=0; i < checkboxes.length; ++i) {
checkboxes[i].setBorderPainted(true);
contentPane.add(checkboxes[i]);

if(checkboxes[i].getIcon() != null) {
System.out.println("setting selected icon");
checkboxes[i].setSelectedIcon(
new ImageIcon("bulb_bright.gif"));
}
}
}
}

9.4 单选钮

 

9-7 运行中的单选钮

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

public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();

contentPane.add(new PrintRangePanel(100, 101));
}
}
class PrintRangePanel extends JPanel {
JRadioButton printAll, printRange;
JLabel startPage, endPage;
JTextField startField, endField;

public PrintRangePanel(int start, int end) {
ButtonGroup group= new ButtonGroup();

printAll = new JRadioButton("Print All");
printRange = new JRadioButton("Print Range");

startPage = new JLabel("Start Page:");
endPage = new JLabel("End Page:");

startField = new JTextField(Integer.toString(start));
endField = new JTextField(Integer.toString(end));

add(printAll); add(printRange);
add(startPage); add(startField);
add(endPage); add(endField);

printRange.setSelected(true);

group.add(printAll);
group.add(printRange);

printRange.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(printRange.isSelected()) {
startField.setEnabled(true);
endField.setEnabled (true);
startPage.setEnabled (true);
endPage.setEnabled (true);

//startPage.repaint();
//endPage.repaint();

startField.requestFocus();
}
}
});
printAll.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(printAll.isSelected()) {
startField.setEnabled(false);
endField.setEnabled (false);
startPage.setEnabled (false);
endPage.setEnabled (false);

startPage.repaint();
endPage.repaint();
}
}
});
}
}

9.4.1 JRadioButton属性

 

9.4.2 JRadioButton事件

 

9.4.3 JRadioButton类总结

 

9-8 实例化单选按钮

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

public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
Icon icon = new ImageIcon("bulb.gif");
JRadioButton[] radioButtons = new JRadioButton[] {
new JRadioButton(),
new JRadioButton(icon),
new JRadioButton(icon, true),
new JRadioButton("idea!"),
new JRadioButton("idea!",true),
new JRadioButton("idea!",icon),
new JRadioButton("idea!",icon, true)
};
contentPane.setLayout(new FlowLayout());

for(int i=0; i < radioButtons.length; ++i) {
radioButtons[i].setBorderPainted(true);
contentPane.add(radioButtons[i]);

if(radioButtons[i].getIcon() != null) {
System.out.println("setting selected icon");
radioButtons[i].setSelectedIcon(
new ImageIcon("bulb_bright.gif"));
}
}
}
}

9.4.4 AWT兼容

 

9.5 本章回顾

 

 

目录
相关文章
|
6月前
|
前端开发
复选框样式修改(复选框变为圆形)
复选框样式修改(复选框变为圆形)
|
1月前
|
大数据 云计算
复选框
【10月更文挑战第1天】复选框。
30 2
|
3月前
点击全选,使所有复选框被选中
点击全选,使所有复选框被选中
47 0
|
6月前
单选全选框如何实现
单选全选框如何实现
40 1
单选、全选、反选、获得所有选中的checkbox
单选、全选、反选、获得所有选中的checkbox
|
前端开发 C# 数据安全/隐私保护
ApeForms | C# - WinForm临时对话框(消息框、提示框、输入框、密码框、单选框、多选框等)
ApeForms一款基于WinForm实现的开源免费商用的UI库,其中提供了一套便于用户交互的临时对话框组件(Dialog),可用于快速创建开发中常见的交互对话框,例如:消息框、提示框、输入框、单选框、多选框等。 ApeFree.ApeDialogs并不是一个包含Dialog实现的UI库,而是一套Dialog标准(提供了一套临时弹出对话框的实现标准)。 Dialog是界面开发过程中必不可少的组件,无论是桌面开发、Web前端开发、移动端开发,甚至命令行应用程序中的一次“Yes or No”的输入都可以看做是一次Dialog弹框。
549 0
ApeForms | C# - WinForm临时对话框(消息框、提示框、输入框、密码框、单选框、多选框等)
|
XML Java API
按钮和复选框控件
按钮和复选框控件
88 0
|
Windows
Qt之单选按钮和复选按钮(QRadioButton、QCheckBox)
Qt之单选按钮和复选按钮(QRadioButton、QCheckBox)
355 0
|
JavaScript 前端开发
单选与全选
结合table表格和checkbox复选框实现单选与全选功能(本文只考虑功能实现,样式不要在意,虽然极丑,但我看不到 φ(>ω<*) )。
单选与全选