JAVA Swing 組件演示***

下面是Swing組件的演示:

package a_swing;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.BoundedRangeModel;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JToggleButton;
import javax.swing.JTree;
import javax.swing.Timer;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;public class Test extends JFrame {public Test() {MenuTest menuTest = new MenuTest();LeftPanel leftPanel = new LeftPanel();RightPanel rightPanel = new RightPanel();BottomPanel bottomPanel = new BottomPanel();CenterPanel centerPanel = new CenterPanel();Container c = this.getContentPane();this.setJMenuBar(menuTest);c.add(leftPanel, BorderLayout.WEST);c.add(rightPanel, BorderLayout.EAST);c.add(centerPanel, BorderLayout.CENTER);c.add(bottomPanel, BorderLayout.SOUTH);this.addWindowListener(new WindowAdapter() {public void WindowClosing(WindowEvent e) {dispose();System.exit(0);}});setSize(700, 500);setTitle("Swing 組件大全簡體版");setUndecorated(true);setLocation(200, 150);show();}class MenuTest extends JMenuBar {private JDialog aboutDialog;public MenuTest() {JMenu fileMenu = new JMenu("文件");JMenuItem exitMenuItem = new JMenuItem("退出", KeyEvent.VK_E);JMenuItem aboutMenuItem = new JMenuItem("關于..", KeyEvent.VK_A);fileMenu.add(exitMenuItem);fileMenu.add(aboutMenuItem);this.add(fileMenu);aboutDialog = new JDialog();initAboutDialog();exitMenuItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {dispose();System.exit(0);}});aboutMenuItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {aboutDialog.show();}});}public JDialog get() {return aboutDialog;}public void initAboutDialog() {aboutDialog.setTitle("關于");Container con = aboutDialog.getContentPane();Icon icon = new ImageIcon("sdmile.gif");JLabel aboutLabel = new JLabel("<html><b><font size=5>" + "<center>Swing!" + "<br>", icon, JLabel.CENTER);con.add(aboutLabel, BorderLayout.CENTER);aboutDialog.setSize(450, 225);aboutDialog.setLocation(300, 300);aboutDialog.addWindowListener(new WindowAdapter() {public void WindowClosing(WindowEvent e) {dispose();}});}}class LeftPanel extends JPanel {private int i = 0;public LeftPanel() {DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");DefaultMutableTreeNode select = new DefaultMutableTreeNode("select");DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("" + i);root.add(child);root.add(select);child.add(child1);JTree tree = new JTree(root);tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);tree.setRowHeight(20);tree.addTreeSelectionListener(new TreeSelectionListener() {public void valueChanged(TreeSelectionEvent e) {JTree tree = (JTree) e.getSource();DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();i++;selectNode.add(new DefaultMutableTreeNode("" + i));}});tree.setPreferredSize(new Dimension(100, 300));JScrollPane scrollPane = new JScrollPane(tree);this.add(scrollPane);}}class BottomPanel extends JPanel {private JProgressBar pb;public BottomPanel() {pb = new JProgressBar();pb.setPreferredSize(new Dimension(680, 20));Timer time = new Timer(1, new ActionListener() {int counter = 0;public void actionPerformed(ActionEvent e) {counter++;pb.setValue(counter);Timer t = (Timer) e.getSource();if (counter == pb.getMaximum()) {t.stop();counter = 0;t.start();}}});time.start();pb.setStringPainted(true);pb.setMinimum(0);pb.setMaximum(1000);pb.setBackground(Color.white);pb.setForeground(Color.red);this.add(pb);}public void setProcessBar(BoundedRangeModel rangeModel) {pb.setModel(rangeModel);}}class RightPanel extends JPanel {public RightPanel() {this.setLayout(new GridLayout(8, 1));JCheckBox checkBox = new JCheckBox("復選按鈕");JButton button = new JButton("打開文件");button.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {JFileChooser file = new JFileChooser();int resule = file.showOpenDialog(new JPanel());if (resule == file.APPROVE_OPTION) {String fileName = file.getSelectedFile().getName();String dir = file.getSelectedFile().getName();JOptionPane.showConfirmDialog(null, dir + "\\" + fileName, "選擇的文件", JOptionPane.YES_OPTION);}}});JToggleButton toggleButton = new JToggleButton("雙胎按鈕");ButtonGroup buttonGroup = new ButtonGroup();JRadioButton radioButton1 = new JRadioButton("單選按鈕1", false);JRadioButton radioButton2 = new JRadioButton("單選按鈕2", false);JComboBox comboBox = new JComboBox();comboBox.setToolTipText("點擊下拉列表增加選項");comboBox.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {JComboBox comboBox = (JComboBox) e.getSource();comboBox.addItem("程序員");comboBox.addItem("分析員");}});DefaultListModel litem = new DefaultListModel();litem.addElement("香蕉");litem.addElement("水果");JList list = new JList(litem);list.addListSelectionListener(new ListSelectionListener() {public void valueChanged(ListSelectionEvent e) {JList l = (JList) e.getSource();Object s = l.getSelectedValue();JOptionPane.showMessageDialog(null, s, "消息框", JOptionPane.YES_OPTION);}});buttonGroup.add(radioButton1);buttonGroup.add(radioButton2);add(button);add(toggleButton);add(checkBox);add(radioButton1);add(radioButton2);add(comboBox);add(list);this.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.LIGHT_GRAY, Color.blue));}}class CenterPanel extends JPanel {public CenterPanel() {JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP);JTextField textField = new JTextField("文本域,點擊打開<文件按鈕>可選擇文件");textField.setActionCommand("textField");JTextPane textPane = new JTextPane();textPane.setCursor(new Cursor(Cursor.TEXT_CURSOR));textPane.setText("編輯器,試著點擊文本區,試著拉動分隔條。");textPane.addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent e) {JTextPane textPane = (JTextPane) e.getSource();textPane.setText("編輯器點擊命令成功");}});JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, textField, textPane);JTable table = new JTable(10, 10);JPanel pane = new JPanel();pane.add(table.getTableHeader(), BorderLayout.NORTH);pane.add(table);tab.addTab("文本演示", splitPane);tab.addTab("表格演示", pane);tab.setPreferredSize(new Dimension(500, 600));this.add(tab);this.setEnabled(true);}}public static void main(String args[]) {new Test();}}

演示界面如下:

  

?

轉載于:https://www.cnblogs.com/jiangzhaowei/p/7448582.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/371381.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/371381.shtml
英文地址,請注明出處:http://en.pswp.cn/news/371381.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Spring 3.1緩存和@CacheEvict

我的上一個博客演示了Spring 3.1的Cacheable批注的應用&#xff0c; Cacheable批注用于標記返回值將存儲在緩存中的方法。 但是&#xff0c; Cacheable只是Spring的Guy為緩存而設計的一對注釋??中的一個&#xff0c;另一個是CacheEvict 。 像Cacheable一樣&#xff0c; Cache…

centos 獲取硬件序列號_如何在 Linux 上查找硬件規格

在 Linux 系統上有許多工具可用于查找硬件規格。-- Sk&#xff08;作者&#xff09;在 Linux 系統上有許多工具可用于查找硬件規格。在這里&#xff0c;我列出了四種最常用的工具&#xff0c;可以獲取 Linux 系統的幾乎所有硬件&#xff08;和軟件&#xff09;細節。好在是這些…

位置服務器管理器,查看 DIMM 位置

鍵入&#xff1a;-> show /System/Memory/DIMMs -t locationTarget | Property | Value-----------------------------------------------------------------------/System/Memory/DIMMs/ | location | CMIOU0/CM/CMP/BOB00/CH0/DIMM (CPU MemoryDIMM_0 | | IO Unit 0 Memor…

Spring –持久層–編寫實體并配置Hibernate

歡迎來到本教程的第二部分。 當您看到本文有多長時間時&#xff0c;請不要驚慌–我向您保證&#xff0c;這主要是簡單的POJO和一些生成的代碼。 在開始之前&#xff0c;我們需要更新我們的Maven依賴項&#xff0c;因為我們現在將使用Hibernate和Spring。 將以下依賴項添加到pom…

無線服務器主機名是,wifi默認服務器主機名

wifi默認服務器主機名 內容精選換一換以CentOS 7操作系統的彈性云服務器為例&#xff1a;登錄Linux彈性云服務器&#xff0c;查看“cloud-init”的配置文件。檢查“/etc/cloud/cloud.cfg”文件中“update_hostname”是否被注釋或者刪除。如果沒有被注釋或者刪除&#xff0c;則需…

pygame里面物體閃爍運動_利用自閃爍發光二極管探究小車在傾斜軌道上的運動規律...

2020年11月23日&#xff0c;周一&#xff0c;24小時安全值班。利用當班中午的時間&#xff0c;微主在創客空間測試了自閃爍發光二極管在勻加速運動中的效果&#xff0c;結果還比較滿意。將小車放置在傾斜的軌道上&#xff0c;將自閃爍發光二極管和紐扣電池構成頻閃光源&#xf…

python網絡爬蟲與信息提取 學習筆記day3

Day3&#xff1a; 只需兩行代碼解析html或xml信息 具體代碼實現:day3_1 注意BeautifulSoup的B和S需要大寫&#xff0c;因為python大小寫敏感 import requests r requests.get("http://python123.io/ws/demo.html") r.text demo r.text from bs4 import Beauti…

番石榴文件:Java文件管理

正如我在這里 &#xff0c; 這里 &#xff0c; 這里和這里所討論的那樣&#xff0c; Groovy和Java SE 7都為Java文件管理提供了改進。 但是&#xff0c;當特定的Java應用程序尚不能使用Java SE 7或Groovy進行文件管理時&#xff0c;仍然可以通過使用Guava的Files類獲得改進的文…

順序查找

順序查找屬于查找中較容易的一個方法&#xff0c;且對數據是否已經排序沒有要求&#xff0c;是很常用的一個查找算法。 但缺點是必須一個一個數字進行比較查找&#xff0c;查找所需步驟可能較多。 順序查找算法的思想是&#xff0c;將目標與待查找數據進行比較&#xff0c;若發…

王者榮耀微信哪個服務器人最少,王者榮耀:微信區王者人數銳減,大神們都去哪了?這些原因很真實...

原標題&#xff1a;王者榮耀&#xff1a;微信區王者人數銳減&#xff0c;大神們都去哪了&#xff1f;這些原因很真實王者榮耀&#xff1a;微信區王者人數銳減&#xff0c;大神們都去哪了&#xff1f;這些原因很真實大家好&#xff01;王者榮耀S16賽季已經開啟一月之余&#xff…

一個div壓在另一個div上面_【CSS小分享】用CSS畫一個新擬態風格鍵盤

什么是新擬態新擬態的英文名稱是“Neumorphism”&#xff0c;也有人稱為“Soft UI”。簡單講&#xff0c;新擬態是一種圖形樣式&#xff0c;其原理是通過模擬真實物體來為界面的UI元素賦予真實感。新擬態風格起源于dribbble&#xff0c;后面陸續被收錄在2020設計趨勢預測里面&a…

JBoss BRMS與JasperReports進行報告

介紹 Jasperreports是一個免費的可下載庫&#xff0c;可用于為Java EE應用程序生成豐富的報告。 本指南還提供了使用Jasper iReport設計器生成報告模板的步驟。 軟件需求 JBoss BRMS 5.3&#xff08;從客戶門戶網站http://access.redhat.com &#xff09; JasperReports 4.6…

java字符串 刪除指定字符的那些事

需求如下&#xff1a; 1.算出2周以前的時間&#xff0c;以正常日期格式返回 2.如果月份和日期前面有0需要去掉返回結果&#xff0c;比如&#xff1a;2017-08-15 就需要顯示2017-8-15。 Calendar calendar Calendar.getInstance();calendar.add(Calendar.DATE, -14);Date date…

Hibernate中Hql查詢

這篇隨筆將會記錄hql的常用的查詢語句&#xff0c;為日后查看提供便利。 在這里通過定義了三個類&#xff0c;Special、Classroom、Student來做測試&#xff0c;Special與Classroom是一對多&#xff0c;Classroom與Student是一對多的關系&#xff0c;這里僅僅貼出這三個bean的屬…

Java代碼質量工具–概述

最近&#xff0c;我有機會在本地IT社區聚會上介紹了該主題。 這是基本演示&#xff1a; Java代碼質量工具 以及更有意義的思維導圖&#xff1a; 但是&#xff0c;我認為我需要更深入地探討這一主題。 這篇博客文章應該像是在此方向上進行進一步調查的起點。 1. CodePro Anal…

利用yum升級Centos6的gcc版本,使其支持C++11

下面的可以在centos6下工作&#xff0c;centos7下有問題。可能是因為centos下的scl我是拷貝的文件&#xff0c;沒有完全驗證centos6下肯定沒問題。 https://my.oschina.net/u/583362/blog/682123 和https://www.quyu.net/info/876.html 拷貝其關鍵內容就是&#xff1a; 1.使用 …

cuda版本查看_ubuntu安裝CUDA

0 寫在前面安裝環境&#xff1a;ubuntu18.04以及GTX1050Ti筆記本為什么要安裝CUDA&#xff1f; 參考百科&#xff0c;CUDA是英偉達推出的集成技術&#xff0c;通過該技術可利用GeForce 8 以后的GPU或者較新的Quadro GPU進行計算。例如典型的tensorflow-GPU和pyCUDA安裝之前都要…

HTML 標簽列表(功能排序) HTML 參考手冊- (HTML5 標準)

HTML 標簽列表&#xff08;功能排序&#xff09; HTML 參考手冊- (HTML5 標準) 功能排序 New : HTML5 新標簽 標簽描述基礎 <!DOCTYPE> 定義文檔類型。<html>定義一個 HTML 文檔<title>為文檔定義一個標題<body>定義文檔的主體<h1> to <h6>…

idea新建scala文件_IDEA maven項目中新建.scala文件

本文首發于我的博客[IDEA maven項目中新建.scala文件]分為三步第一步、IDEA中安裝scala插件1、搜索安裝File-Sittings-Plugins-搜索安裝scala2、安裝完成重啟安裝完成之后點擊重啟idea第二步、下載、安裝、配置Scala1、下載安裝Scala SDK本體搜索引擎搜索Scala SDK或者點我去Sc…

Linux中執行shell腳本的4種方法總結

文章來源&#xff1a;http://www.jb51.net/article/53924.htm 這篇文章主要介紹了Linux中執行shell腳本的4種方法總結,即在Linux中運行shell腳本的4種方法,需要的朋友可以參考下 bash shell 腳本的方法有多種&#xff0c;現在作個小結。假設我們編寫好的shell腳本的文件名為hel…