18——Swing程序设计

编程入门 行业动态 更新时间:2024-10-23 06:29:41

18——Swing<a href=https://www.elefans.com/category/jswz/34/1771020.html style=程序设计"/>

18——Swing程序设计

例题1

package shiba;import java.awt.*;
import javax.swing.*;
public class JFreamTest {public static void main(String[] args) {JFrame jf=new JFrame();jf.setTitle("创建一个JFrame窗体");Container container=jf.getContentPane();JLabel jl =new JLabel("这是一个JFrame窗体");jl.setHorizontalAlignment(SwingConstants.CENTER);container.add(jl);jf.setSize(300,150);jf.setLocation(320,240);jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);jf.setVisible(true);}
}

结果

 

例题2

package shiba;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class MyJDialog extends JDialog {	public MyJDialog(MyFrame frame) {super(frame,"第一个JDialog窗体",true);Container container=getContentPane();container.add(new JLabel("这是一个对话框"));setBounds(120,120,100,100);}
}
public class MyFrame extends JFrame{public MyFrame() {Container container =getContentPane();container.setLayout(null);JButton bl = new JButton("弹出对话框");bl.setBounds(10,10,100,21);bl.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {MyJDialog dialog = new MyJDialog(MyFrame.this);dialog.setVisible(true);}});container.add(bl);setSize(200,200);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);setVisible(true);}
public static void main(String args[]) {new MyFrame();
}}

结果 

例题3

package shiba;import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;public class Demo {public static void main(String[] args) {Object o[]= {new JButton("是的"),new JButton("再想想")};Icon icon =new ImageIcon("src/注意.png");JOptionPane.showOptionDialog(null,"你做好准备了吗?","注意了!",JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION,icon, o, null);}
}

结果

 

例题4 

package shiba;import javax.swing.JOptionPane;public class Demo4 {public static void main(String[] args) {int answer =JOptionPane.showConfirmDialog(null,"确定离开吗?","标题",JOptionPane.YES_NO_CANCEL_OPTION);}
}

结果

 

例题5 

package shiba;import javax.swing.JOptionPane;public class Demo5 {public static void main(String[] args) {String name =JOptionPane.showInputDialog(null,"请输入您的名字");}
}

结果

 

例题6 

package shiba;import javax.swing.JOptionPane;public class Demo6 {public static void main(String[] args) {JOptionPane.showConfirmDialog(null,"您与服务器断开了连接","发生错误",JOptionPane.ERROR_MESSAGE);}
}

结果

 例题7

package shiba;import java.awt.Container;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;public class AbsolutePosition extends JFrame {public AbsolutePosition() {setTitle("本窗体使用绝对布局");setLayout(null);setBounds(0,0,300,150);Container c=getContentPane();JButton b1 =new JButton("按钮1");JButton b2 =new JButton("按钮2");b1.setBounds(10, 30, 80, 30);b2.setBounds(60,70, 100, 20);c.add(b1);c.add(b2);setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new AbsolutePosition();}
}

结果

例题8

package shiba;import java.awt.Container;
import java.awt.FlowLayout;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;public class FlowLayoutPosition	extends JFrame {public FlowLayoutPosition() {setTitle("本窗体使用流布局管理器");Container c =getContentPane();setLayout(new FlowLayout(FlowLayout.RIGHT,10,10));for(int i=0;i<10;i++) {c.add(new JButton("button"+i));}setSize(3300,200);setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);setFocusable(true);}public static void main(String[] args) {new FlowLayoutPosition();}
}

结果

例题9 

package shiba;import java.awt.BorderLayout;
import java.awt.Container;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;public class BorderLayoutPosition extends JFrame {public BorderLayoutPosition() {setTitle("这个窗体使用边界布局管理器");Container c=getContentPane();setLayout(new BorderLayout());JButton centerBtn =new JButton("中");JButton northBtn =new JButton("北");JButton southBtn =new JButton("南");JButton westBtn =new JButton("西");JButton eastBtn =new JButton("东");c.add(centerBtn,BorderLayout.CENTER);c.add(northBtn,BorderLayout.NORTH);c.add(southBtn,BorderLayout.SOUTH);c.add(westBtn,BorderLayout.WEST);c.add(eastBtn,BorderLayout.EAST);setSize(350,200);setVisible(true);setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);}public static void main(String[] args) {new BorderLayoutPosition();}
}

结果

例题10

package shiba;import java.awt.Container;
import java.awt.GridLayout;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;public class GridLayoutPostiton extends JFrame {public GridLayoutPostiton() {Container c=getContentPane();setLayout(new GridLayout(7,3,5,5));for(int i=0;i<20;i++) {c.add(new JButton("button"+i));}setSize(300,300);setTitle("这是一个使用网格布局管理器的窗体");setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new GridLayoutPostiton();}
}

结果

 

例题11

package shiba;import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;public class JPanelTest extends JFrame {public JPanelTest() {Container c =getContentPane();//将整个容器设置为2行2列的网格布局,组件水平间隔10像素,垂直间隔10像素c.setLayout(new GridLayout(2,2,10,10));//初始化一个面板,此面板使用1行4列的网格布局,组件水平间隔10像素,垂直间隔10像素JPanel p1 =new JPanel(new GridLayout(1,4,10,10));//初始化一个面板,此面板使用边界布局JPanel p2 =new JPanel(new BorderLayout());//初始化一个面板,此面板使用1行2列的网格布局,组件水平间隔10像素,垂直间隔10像素JPanel p3 =new JPanel(new GridLayout(1,2,10,10));//初始化一个面板,此面板使用2行1列的网格布局,组件水平间隔10像素,垂直间隔10像素JPanel p4 =new JPanel(new GridLayout(2,1,10,10));//给每个面板都添加边框和标题,使用BorderFactory 工厂类生成带标题的边框对象p1.setBorder(BorderFactory.createTitledBorder("面板1"));p2.setBorder(BorderFactory.createTitledBorder("面板2"));p3.setBorder(BorderFactory.createTitledBorder("面板3"));p4.setBorder(BorderFactory.createTitledBorder("面板4"));//向面板1中添加按钮p1.add(new JButton("b1"));p1.add(new JButton("b1"));p1.add(new JButton("b1"));p1.add(new JButton("b1"));//向面板2中添加按钮p2.add(new JButton("b2"),BorderLayout.WEST);p2.add(new JButton("b2"),BorderLayout.EAST);p2.add(new JButton("b2"),BorderLayout.NORTH);p2.add(new JButton("b2"),BorderLayout.SOUTH);p2.add(new JButton("b2"),BorderLayout.CENTER);//向面板3中添加按钮p3.add(new JButton("b3"));p3.add(new JButton("b3"));//向面板4中添加按钮p4.add(new JButton("b4"));p4.add(new JButton("b4"));//向容器中添加面板c.add(p1);c.add(p2);c.add(p3);c.add(p4);setTitle("在这个窗体中使用了面板");setSize(500,300);//窗体宽高setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//关闭动作}public static void main(String[] args) {JPanelTest test=new JPanelTest();test.setVisible(true);}
}

结果

 例题12

import java.awt.Container;import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;public class JScrollPaneTest extends JFrame{public JScrollPaneTest() {Container c = getContentPane();JTextArea ta = new JTextArea(20, 50);JScrollPane sp = new JScrollPane(ta);c.add(sp);setTitle("带滚动条的文字编译器");setSize(400, 200);setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);}public static void main(String[] args) {JScrollPaneTest test = new JScrollPaneTest();test.setVisible(true);}
}

结果

 

 例题13

package shiba;import javax.swing.*;
import java.awt.*;public class JlabelTest extends JFrame {public JlabelTest(){Container container = getContentPane();JLabel jl = new JLabel("JFrame窗体");container.add(jl);setSize(200,100);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);setVisible(true);}public static void main(String args[]){new JlabelTest();}
}

结果

例题14

package shiba;import java.awt.Container;import javax.print.DocFlavor.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;public class MyImageIcon extends JFrame{public MyImageIcon() {Container container =getContentPane();JLabel jl =new JLabel("这是一个JFrame窗体");java.URL url =MyImageIcon.class.getResource("pic.png");Icon icon =new ImageIcon(url);jl.setIcon(icon);jl.setHorizontalAlignment(SwingConstants.CENTER);jl.setOpaque(true);container.add(jl);setSize(300,200);setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new MyImageIcon();}
}

结果

 

例题15

package shiba;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class JButtonTest extends JFrame {public JButtonTest(){Icon icon = new ImageIcon("/src/20220909223037.png");setLayout(new GridLayout(3,2,5,5));Container c =getContentPane();JButton btn[] = new JButton[6];for(int i =0;i< btn.length;i++){btn[i] = new JButton();c.add(btn[i]);}btn[0].setText("不可用");btn[0].setEnabled(false);btn[1].setText("有背景色");btn[1].setBackground(Color.YELLOW);btn[2].setText("無邊框");btn[2].setBorderPainted(false);btn[3].setText("有邊框");btn[3].setBorder(BorderFactory.createLineBorder(Color.RED));btn[4].setIcon(icon);btn[4].setToolTipText("圖片按鈕");btn[5].setText("可點擊");btn[5].addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {JOptionPane.showMessageDialog(JButtonTest.this, "點擊按鈕");}});setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);setTitle("創建不用樣式按鈕");setBounds(100,100,400,200);}public static void main(String[] args){new JButtonTest();}
}

结果 

例题16

package shiba;
import javax.swing.*;public class RadioButtonTest extends JFrame {public RadioButtonTest(){setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setTitle("單選按鈕的使用");setBounds(100,100,240,120);getContentPane().setLayout(null);JLabel lblNewLabel = new JLabel("請選擇性別:");lblNewLabel.setBounds(5,5,120,15);getContentPane().add(lblNewLabel);JRadioButton rbtnNormal = new JRadioButton("男");rbtnNormal.setSelected(true);rbtnNormal.setBounds(40,30,75,22);getContentPane().add(rbtnNormal);JRadioButton rbtnPwd = new JRadioButton("女");rbtnPwd.setBounds(120,30,75,22);getContentPane().add(rbtnPwd);ButtonGroup group = new ButtonGroup();group.add(rbtnNormal);group.add(rbtnPwd);}public static void main(String[] args){RadioButtonTest frame = new RadioButtonTest();frame.setVisible(true);}
}

结果 

例题17

package shiba;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class CheckBoxTest extends JFrame{public void CheckBokxTest() {setBounds(100,100,170,110);setDefaultCloseOperation(EXIT_ON_CLOSE);Container c = getContentPane();c.setLayout(new FlowLayout());JCheckBox c1=new JCheckBox("1");JCheckBox c2=new JCheckBox("2");JCheckBox c3=new JCheckBox("3");c.add(c1);c.add(c2);c.add(c3);JButton bth=new JButton("打印");bth.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {//在控制台System.out.println(c1.getText()+"按钮选中状态:"+c1.isSelected());System.out.println(c2.getText()+"按钮选中状态:"+c2.isSelected());System.out.println(c3.getText()+"按钮选中状态:"+c3.isSelected());}});c.add(bth);setVisible(true);}public static void main(String[] args) {new CheckBoxTest();}
}

 结果

例题18

package shiba;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;public class JComboBoxTest extends JFrame{public JComboBoxTest() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setTitle("下拉列表框的使用");setBounds(100,100,317,147);getContentPane().setLayout(null);JLabel lblNewLabel =new JLabel("请使用证件:");lblNewLabel.setBounds(28,14,80,15);getContentPane().add(lblNewLabel);JComboBox<String>comboBox = new JComboBox<String>();comboBox.addItem("抑郁证");comboBox.addItem("学生证");comboBox.addItem("神经证");comboBox.addItem("魔怔");comboBox.setEditable(true);getContentPane().add(comboBox);JLabel lblResult = new JLabel("");lblResult.setBounds(0,57,146,15);getContentPane().add(lblResult);JButton btnNewButton =new JButton("确定");btnNewButton.setBounds(200,10,67,23);getContentPane().add(btnNewButton);btnNewButton.addActionListener((ActionListener) new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {lblResult.setText("您的选择是:"+comboBox.getSelectedItem());}});}public static void main(String[] args) {JComboBoxTest frame = new JComboBoxTest();frame.setVisible(true);}
}

 结果

 例题19

package shiba;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class JListTest extends JFrame {public JListTest(){Container cp = getContentPane();cp.setLayout(null);String[] contents = {"列表1","列表2","列表3","列表4","列表5"};JList<String> jl =new JList<>(contents);JScrollPane js =  new JScrollPane(jl);js.setBounds(10,10,100,189);cp.add(js);JTextArea area = new JTextArea();JScrollPane scrollPane = new JScrollPane(area);scrollPane.setBounds(118,10,73,80);cp.add(scrollPane);JButton btnNewButton = new JButton("确认");btnNewButton.setBounds(120,96,71,23);cp.add(btnNewButton);btnNewButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {java.util.List<String> values = jl.getSelectedValuesList();area.setText("");for(String value:values){area.append(value+"\n");}}});setTitle("在这个窗体中使用了列表框");setSize(217,167);setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);}public static void main(String args[]){new JListTest();}
}

结果

例题20

package shiba;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class JTextFieldTest extends JFrame {public JTextFieldTest() {Container c = getContentPane();c.setLayout(new FlowLayout());JTextField jt = new JTextField("请点击清除按钮");jt.setColumns(20);jt.setFont(new Font("宋体",Font.PLAIN,20));JButton jb = new JButton("清除");jt.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {jt.setText("触发事件");	}});jb.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println(jt.getText());jt.setText("");jt.requestFocus();}});c.add(jt);c.add(jb);setBounds(100,100,250,110);setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);}public static void main(String[] args) {new JTextFieldTest();}}

结果

 

 例题21

import java.awt.Container;import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;public class JTextAreaTest extends JFrame{public JTextAreaTest() {setSize(200,100);setTitle("定义自动换行的文本域");setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);Container cp = getContentPane();	//获取窗体主容器//创建一个文本内容为文本域、行高与列宽均为6的文本域JTextArea jt = new JTextArea("文本域",6,6);jt.setLineWrap(true);		//可以自动换行cp.add(jt);setVisible(true);}public static void main(String[] args) {new JTextAreaTest();}}

结果

例题22

package shiba;import java.awt.BorderLayout;
import java.awt.Component;import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;public class JTableDemo extends JFrame{public static void main(String[] args) {JTableDemo frame =new JTableDemo();frame.setVisible(true);}public JTableDemo() {setTitle("创建可以滚动的表格");setBounds(100,100,240,150);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);String[] columnNames = {"A","B"};//定义表格数据组String[][] tableValues = {{"A1","B1",},{"A2","B2"},{"A3","B3"},{"A4","B4"},{"A5","B5"}};JTable table =new JTable(tableValues,columnNames);JScrollPane scrollPane =new JScrollPane(table);getContentPane().add(scrollPane,BorderLayout.CENTER);}}

结果

例题23

package shiba;import java.awt.BorderLayout;import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;public class SortingTable extends JFrame {private static final long serialVersionUID = 1L;public static void main(String[] args) {SortingTable frame = new SortingTable();frame.setVisible(true);}public SortingTable() {setTitle("表格模型与表格");setBounds(100,100,240,150);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JScrollPane scrollPane = new JScrollPane();getContentPane().add(scrollPane,BorderLayout.CENTER);String [] columnNames = {"A","B"};String[][] tableValues = {{"A1","B1"},{"A2","B2"},{"A3","B3"}};DefaultTableModel tableModel = new DefaultTableModel(tableValues,columnNames);JTable table = new JTable(tableModel);table.setRowSorter(new TableRowSorter<>(tableModel));scrollPane.setViewportView(table);}
}

结果

 

例题24

package 十八;import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;public class AddAndDeleteDemo extends JFrame{private DefaultTableModel tableModel;private JTable table;private JTextField aTextField;private JTextField bTextField;public static void main(String[] args) {AddAndDeleteDemo frame = new AddAndDeleteDemo();frame.setVisible(true);}
public AddAndDeleteDemo() {setTitle("维护表格模型");setBounds(100,100,520,200);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JScrollPane scrollPane = new JScrollPane();getContentPane().add(scrollPane,BorderLayout.CENTER);String[] columnNames = {"A","B"};String[][] tableValues = {{"A1","B1"},{"A2","B2"},{"A3","B3"}};tableModel = new DefaultTableModel(tableValues,columnNames);table = new JTable(tableModel);table.setRowSorter(new TableRowSorter<>(tableModel));table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);table.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {int selectedRow = table.getSelectedRow();Object oa = tableModel.getValueAt(selectedRow, 0);Object ob = tableModel.getValueAt(selectedRow, 1);aTextField.setText(oa.toString());bTextField.setText(ob.toString());}});scrollPane.setViewportView(table);JPanel panel = new JPanel();getContentPane().add(panel,BorderLayout.SOUTH);panel.add(new JLabel("A:"));aTextField = new JTextField("A4",10);panel.add(aTextField);panel.add(new JLabel("B:"));bTextField = new JTextField("B4",10);panel.add(bTextField);JButton addButton = new JButton("添加");addButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {String[] rowValues = {aTextField.getText(),bTextField.getText()};tableModel.addRow(rowValues);int rowCount = table.getRowCount() + 1;aTextField.setText("A" + rowCount);bTextField.setText("B" + rowCount);}});panel.add(addButton);JButton upButton = new JButton("修改");upButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {int selectedRow = table.getSelectedRow();if(selectedRow != -1) {tableModel.setValueAt(aTextField, selectedRow, 0);tableModel.setValueAt(bTextField, selectedRow, 0);}}});panel.add(upButton);JButton delButton = new JButton("删除");delButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {int selectedRow = table.getSelectedRow();if(selectedRow != -1) {tableModel.removeRow(selectedRow);}}});panel.add(delButton);
}
}

结果

 

例题25

package shiba;import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;public class SimpleEvent extends JFrame{private JButton jb=new JButton("我是按钮,点击我");public SimpleEvent() {setLayout(null);setSize(200,100);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);Container cp=getContentPane();cp.add(jb);jb.setBounds(10, 10,150,30);jb.addActionListener((ActionListener) new jbAction());setVisible(true);}class jbAction implements ActionListener{public void actionPerformed(ActionEvent arg0) {jb.setText("我被点击了");}}public static void main(String[] args) {new SimpleEvent();}
}

 结果

结果

更多推荐

18——Swing程序设计

本文发布于:2023-11-15 21:50:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606871.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序设计   Swing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!