Java Swing程序設(shè)計實戰(zhàn)
更新時間:2021年05月18日 15:11:02 作者:Tomas·Wilson
今天教大家怎么用JavaSwing工具包實現(xiàn)一個程序的界面設(shè)計,文中有非常詳細(xì)的代碼示例及注釋,對正在學(xué)習(xí)Java的小伙伴們很有幫助,需要的朋友可以參考下
一、按鈕組件
1.1 提交按鈕組件
package swing; import java.awt.*; import java.awt.event.*; import java.net.*; import javax.swing.*; public class JButtonTest extends JFrame { /** * */ private static final long serialVersionUID = 1L; public JButtonTest() { URL url = JButtonTest.class.getResource("imageButton.jpg"); Icon icon = new ImageIcon(url); setLayout(new GridLayout(3, 2, 5, 5)); // 設(shè)置網(wǎng)格布局管理器 Container c = getContentPane(); // 創(chuàng)建容器 for (int i = 0; i < 5; i++) { // 創(chuàng)建按鈕,同時設(shè)置按鈕文字與圖標(biāo) JButton J = new JButton("button" + i, icon); c.add(J); // 在容器中添加按鈕 if (i % 2 == 0) { J.setEnabled(false); // 設(shè)置其中一些按鈕不可用 } } JButton jb = new JButton(); // 實例化一個沒有文字與圖片的按鈕 jb.setMaximumSize(new Dimension(90, 30)); // 設(shè)置按鈕與圖片相同大小 jb.setIcon(icon); // 為按鈕設(shè)置圖標(biāo) jb.setHideActionText(true); jb.setToolTipText("圖片按鈕"); // 設(shè)置按鈕提示為文字 jb.setBorderPainted(false); // 設(shè)置按鈕邊界不顯示 jb.addActionListener(new ActionListener() { // 為按鈕添加監(jiān)聽事件 public void actionPerformed(ActionEvent e) { // 彈出確認(rèn)對話框 JOptionPane.showMessageDialog(null, "彈出對話框"); } }); c.add(jb); // 將按鈕添加到容器中 setTitle("創(chuàng)建帶文字與圖片的按鈕"); setSize(350, 150); setVisible(true); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } public static void main(String args[]) { new JButtonTest(); } }
1.2 復(fù)選框組件
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CheckBoxTest extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel panel1 = new JPanel(); private JPanel panel2 = new JPanel(); private JTextArea jt = new JTextArea(3, 10); private JCheckBox jc1 = new JCheckBox("1"); private JCheckBox jc2 = new JCheckBox("2"); private JCheckBox jc3 = new JCheckBox("3"); public CheckBoxTest() { Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(panel1, BorderLayout.NORTH); final JScrollPane scrollPane = new JScrollPane(jt); panel1.add(scrollPane); c.add(panel2, BorderLayout.SOUTH); panel2.add(jc1); jc1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (jc1.isSelected()) jt.append("復(fù)選框1被選中\(zhòng)n"); } }); panel2.add(jc2); jc2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (jc2.isSelected()) jt.append("復(fù)選框2被選中\(zhòng)n"); } }); panel2.add(jc3); jc3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (jc3.isSelected()) jt.append("復(fù)選框3被選中\(zhòng)n"); } }); setSize(200, 160); setVisible(true); setTitle("復(fù)選框的使用"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new CheckBoxTest(); } }
二、列表組件
2.1 JComboBox類
import java.awt.*; import javax.swing.*; public class JComboBoxModelTest extends JFrame { private static final long serialVersionUID = 1L; JComboBox<String> jc = new JComboBox<>(new MyComboBox()); JLabel jl = new JLabel("請選擇證件"); public JComboBoxModelTest() { setSize(new Dimension(160, 180)); setVisible(true); setTitle("在窗口中設(shè)置下拉列表框"); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(jl); cp.add(jc); } public static void main(String[] args) { new JComboBoxModelTest(); } } class MyComboBox extends AbstractListModel<String> implements ComboBoxModel<String> { /** * */ private static final long serialVersionUID = 1L; String selecteditem = null; String[] test = { "身份證", "軍人證", "學(xué)生證", "工作證" }; public String getElementAt(int index) { return test[index]; } public int getSize() { return test.length; } public void setSelectedItem(Object item) { selecteditem = (String) item; } public Object getSelectedItem() { return selecteditem; } public int getIndex() { for (int i = 0; i < test.length; i++) { if (test[i].equals(getSelectedItem())) return i; } return 0; } }
2.2 列表框組件
import java.awt.*; import javax.swing.*; public class JListTest extends JFrame { /** * */ private static final long serialVersionUID = 1L; public JListTest() { Container cp = getContentPane(); cp.setLayout(null); JList<String> jl = new JList<>(new MyListModel()); JScrollPane js = new JScrollPane(jl); js.setBounds(10, 10, 100, 100); cp.add(js); setTitle("在這個窗體中使用了列表框"); setSize(200, 150); setVisible(true); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } public static void main(String args[]) { new JListTest(); } } class MyListModel extends AbstractListModel<String> { /** * */ private static final long serialVersionUID = 1L; private String[] contents = { "列表1", "列表2", "列表3", "列表4", "列表5", "列表6" }; public String getElementAt(int x) { if (x < contents.length) return contents[x++]; else return null; } public int getSize() { return contents.length; } }
三、文本組件
3.1 文本框組件
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTextFieldTest extends JFrame { /** * */ private static final long serialVersionUID = 1L; public JTextFieldTest() { setSize(250, 100); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); Container cp = getContentPane(); getContentPane().setLayout(new FlowLayout()); final JTextField jt = new JTextField("aaa", 20); final JButton jb = new JButton("清除"); cp.add(jt); cp.add(jb); jt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO 自動生成方法存根 jt.setText("觸發(fā)事件"); } }); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { jt.setText(""); jt.requestFocus(); } }); setVisible(true); } public static void main(String[] args) { new JTextFieldTest(); } }
3.2 密碼框
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTextFieldTest extends JFrame { /** * */ private static final long serialVersionUID = 1L; public JTextFieldTest() { setSize(250, 100); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); Container cp = getContentPane(); getContentPane().setLayout(new FlowLayout()); // final JTextField jt=new JTextField("aaa",20); JPasswordField jp = new JPasswordField("", 20); jp.setEchoChar('*'); final JButton jb = new JButton("清除"); cp.add(jp); cp.add(jb); jp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO 自動生成方法存根 jp.setText("觸發(fā)事件"); } }); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { jp.setText(""); jp.requestFocus(); } }); setVisible(true); } public static void main(String[] args) { new JTextFieldTest(); } }
3.3 文本域組件
import java.awt.*; import javax.swing.*; public class JTextAreaTest extends JFrame { public JTextAreaTest() { setSize(200, 100); setTitle("定義自動換行的文本域"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container cp = getContentPane(); JTextArea jt = new JTextArea("文本域", 6, 6); jt.setLineWrap(true); cp.add(jt); setVisible(true); } public static void main(String[] args) { new JTextAreaTest(); } }
四、常用時間監(jiān)聽器
4.1 動作事件監(jiān)聽器
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SimpleEvent extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JButton jb = new JButton("我是按鈕,點擊我"); public SimpleEvent() { setLayout(null); setSize(200, 100); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); Container cp = getContentPane(); cp.add(jb); jb.setBounds(10, 10, 100, 30); jb.addActionListener(new jbAction()); setVisible(true); } class jbAction implements ActionListener { public void actionPerformed(ActionEvent arg0) { jb.setText("我被單擊了"); } } public static void main(String[] args) { new SimpleEvent(); } }
4.2 焦點事件監(jiān)聽器
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FocusEventTest extends JFrame { /** * */ private static final long serialVersionUID = 1L; public FocusEventTest() { setSize(250, 100); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container cp = getContentPane(); getContentPane().setLayout(new FlowLayout()); final JLabel label = new JLabel(); getContentPane().add(label); JTextField jt = new JTextField("請單擊其他文本框", 10); JTextField jt2 = new JTextField("請單擊我", 10); cp.add(jt); cp.add(jt2); jt.addFocusListener(new FocusListener() { // 組件失去焦點時調(diào)用的方法 public void focusLost(FocusEvent arg0) { JOptionPane.showMessageDialog(null, "文本框失去焦點"); } // 組件獲取鍵盤焦點時調(diào)用的方法 public void focusGained(FocusEvent arg0) { } }); setVisible(true); } public static void main(String[] args) { new FocusEventTest(); } }
到此這篇關(guān)于JavaSwing實現(xiàn)程序界面設(shè)計的文章就介紹到這了,更多相關(guān)JavaSwing程序界面設(shè)計內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- Java 程序設(shè)計總復(fù)習(xí)題(java基礎(chǔ)代碼)
- Java面向?qū)ο蟪绦蛟O(shè)計:類的定義,靜態(tài)變量,成員變量,構(gòu)造函數(shù),封裝與私有,this概念與用法詳解
- Java面向?qū)ο蟪绦蛟O(shè)計:繼承,多態(tài)用法實例分析
- Java面向?qū)ο蟪绦蛟O(shè)計:抽象類,接口用法實例分析
- java程序設(shè)計語言的優(yōu)勢及特點
- java門禁系統(tǒng)面向?qū)ο蟪绦蛟O(shè)計
- Java網(wǎng)絡(luò)編程之TCP程序設(shè)計
- Java面向?qū)ο蟪绦蛟O(shè)計多態(tài)性示例
- Android中通過RxJava進(jìn)行響應(yīng)式程序設(shè)計的入門指南
- Java程序設(shè)計之12個經(jīng)典樣例
相關(guān)文章
Jrebel License Server 激活 IDEA-Jrebel-在線-
這篇文章主要介紹了Jrebel License Server 激活 IDEA-Jrebel-在線-離線-均適用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12java-collection中的null,isEmpty用法
這篇文章主要介紹了java-collection中的null,isEmpty用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Java for循環(huán)Map集合優(yōu)化實現(xiàn)解析
這篇文章主要介紹了Java for循環(huán)Map集合優(yōu)化實現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01