当前位置:实例文章 » 其他实例» [文章]18 常用控件--按钮组

18 常用控件--按钮组

发布人:shili8 发布时间:2025-02-13 03:42 阅读次数:0

**18 常用控件--按钮组**

在软件开发中,控件是用户界面的基本组成部分。按钮是最常用的控件之一,它可以用于触发事件、执行操作或显示信息。在本文中,我们将介绍18种常用的控件,特别是按钮组。

###1. 按钮(Button)

按钮是最基础的控件之一,可以用于触发事件、执行操作或显示信息。它通常有两种状态:正常和激活。

java// Java代码示例import javax.swing.*;
public class ButtonExample {
 public static void main(String[] args) {
 JFrame frame = new JFrame("Button Example");
 JButton button = new JButton("Click Me!");
 button.addActionListener(e -> System.out.println("Button clicked!"));
 frame.add(button);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,200);
 frame.setVisible(true);
 }
}


###2. 单选按钮(JRadioButton)

单选按钮用于选择一个选项。它通常与其他控件一起使用,形成一个组。

java// Java代码示例import javax.swing.*;
public class RadioButtonExample {
 public static void main(String[] args) {
 JFrame frame = new JFrame("Radio Button Example");
 JRadioButton radio1 = new JRadioButton("Option1");
 JRadioButton radio2 = new JRadioButton("Option2");
 JButton button = new JButton("Submit");
 button.addActionListener(e -> System.out.println("Selected option: " + (radio1.isSelected() ? "Option1" : "Option2")));
 frame.add(radio1);
 frame.add(radio2);
 frame.add(button);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,200);
 frame.setVisible(true);
 }
}


###3. 复选按钮(JCheckBox)

复选按钮用于选择多个选项。它通常与其他控件一起使用,形成一个组。

java// Java代码示例import javax.swing.*;
public class CheckBoxExample {
 public static void main(String[] args) {
 JFrame frame = new JFrame("Check Box Example");
 JCheckBox check1 = new JCheckBox("Option1");
 JCheckBox check2 = new JCheckBox("Option2");
 JButton button = new JButton("Submit");
 button.addActionListener(e -> System.out.println("Selected options: " + (check1.isSelected() ? "Option1" : "") + ", " + (check2.isSelected() ? "Option2" : "")));
 frame.add(check1);
 frame.add(check2);
 frame.add(button);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,200);
 frame.setVisible(true);
 }
}


###4. 下拉列表(ComboBox)

下拉列表用于选择一个选项。它通常有多个选项,可以通过点击下拉箭头展开。

java// Java代码示例import javax.swing.*;
public class ComboBoxExample {
 public static void main(String[] args) {
 JFrame frame = new JFrame("Combo Box Example");
 String[] options = {"Option1", "Option2", "Option3"};
 JComboBox combo = new JComboBox<>(options);
 JButton button = new JButton("Submit");
 button.addActionListener(e -> System.out.println("Selected option: " + combo.getSelectedItem()));
 frame.add(combo);
 frame.add(button);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,200);
 frame.setVisible(true);
 }
}


###5. 文本框(TextField)

文本框用于输入文本。它通常有一个光标,用户可以在其中输入内容。

java// Java代码示例import javax.swing.*;
public class TextFieldExample {
 public static void main(String[] args) {
 JFrame frame = new JFrame("Text Field Example");
 JTextField field = new JTextField(20);
 JButton button = new JButton("Submit");
 button.addActionListener(e -> System.out.println("Input: " + field.getText()));
 frame.add(field);
 frame.add(button);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,200);
 frame.setVisible(true);
 }
}


###6. 文本区域(TextArea)

文本区域用于输入多行文本。它通常有一个光标,用户可以在其中输入内容。

java// Java代码示例import javax.swing.*;
public class TextAreaExample {
 public static void main(String[] args) {
 JFrame frame = new JFrame("Text Area Example");
 JTextArea area = new JTextArea(10,20);
 JButton button = new JButton("Submit");
 button.addActionListener(e -> System.out.println("Input: " + area.getText()));
 frame.add(area);
 frame.add(button);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,200);
 frame.setVisible(true);
 }
}


###7. 滚动条(ScrollPane)

滚动条用于显示超出视图范围的内容。它通常与其他控件一起使用,形成一个组。

java// Java代码示例import javax.swing.*;
public class ScrollPaneExample {
 public static void main(String[] args) {
 JFrame frame = new JFrame("Scroll Pane Example");
 JTextArea area = new JTextArea(10,20);
 JScrollPane scroll = new JScrollPane(area);
 JButton button = new JButton("Submit");
 button.addActionListener(e -> System.out.println("Input: " + area.getText()));
 frame.add(scroll);
 frame.add(button);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,200);
 frame.setVisible(true);
 }
}


###8. 表格(JTable)

表格用于显示数据。它通常有多行和多列,可以通过点击单元格编辑内容。

java// Java代码示例import javax.swing.*;
public class TableExample {
 public static void main(String[] args) {
 JFrame frame = new JFrame("Table Example");
 String[] columns = {"Column1", "Column2"};
 Object[][] data = {{"Row1, Column1", "Row1, Column2"}, {"Row2, Column1", "Row2, Column2"}};
 JTable table = new JTable(data, columns);
 JButton button = new JButton("Submit");
 button.addActionListener(e -> System.out.println("Selected row: " + (table.getSelectedRow() != -1 ? "Row " + (table.getSelectedRow() +1) : "No row selected")));
 frame.add(new JScrollPane(table));
 frame.add(button);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,200);
 frame.setVisible(true);
 }
}


###9. 列表(JList)

列表用于显示数据。它通常有多行,可以通过点击项编辑内容。

java// Java代码示例import javax.swing.*;
public class ListExample {
 public static void main(String[] args) {
 JFrame frame = new JFrame("List Example");
 String[] items = {"Item1", "Item2", "Item3"};
 JList list = new JList<>(items);
 JButton button = new JButton("Submit");
 button.addActionListener(e -> System.out.println("Selected item: " + (list.getSelectedValue() != null ? list.getSelectedValue() : "No item selected")));
 frame.add(new JScrollPane(list));
 frame.add(button);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,200);
 frame.setVisible(true);
 }
}


###10. 日历(JCalendar)

日历用于显示日期。它通常有多行和多列,可以通过点击单元格编辑内容。

java// Java代码示例import javax.swing.*;
public class CalendarExample {
 public static void main(String[] args) {
 JFrame frame = new JFrame("Calendar Example");
 JCalendar calendar = new JCalendar();
 JButton button = new JButton("Submit");
 button.addActionListener(e -> System.out.println("Selected date: " + (calendar.getSelectedDate() != null ? calendar.getSelectedDate().toString() : "No date selected")));
 frame.add(calendar);
 frame.add(button);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,200);
 frame.setVisible(true);
 }
}


###11. 时钟(JClock)

时钟用于显示时间。它通常有多行和多列,可以通过点击单元格编辑内容。

java// Java代码示例import javax.swing.*;
public class ClockExample {
 public static void main(String[] args) {
 JFrame frame = new JFrame("Clock Example");
 JClock clock = new JClock();
 JButton button = new JButton("Submit");
 button.addActionListener(e -> System.out.println("Current time: " + (clock.getCurrentTime() != null ? clock.getCurrentTime().toString() : "No time selected")));
 frame.add(clock);
 frame.add(button);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,200);
 frame.setVisible(true);
 }
}


###12. 进度条(JProgressBar)

进度条用于显示进度。它通常有多行和多列,可以通过点击单元格编辑内容。

java// Java代码示例import javax.swing

相关标签:
其他信息

其他资源

Top