java swing 例子(一些)
生活随笔
收集整理的這篇文章主要介紹了
java swing 例子(一些)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
碼碼;
?
package swing1;import javax.swing.JFrame; import javax.swing.WindowConstants; public class EmptyJFrame extends JFrame{ public static void main(String[] args) { //現在創建了一個對象,不過什么都顯示不出來 EmptyJFrame f = new EmptyJFrame(); //加上這一句就可以顯示一個僅有關閉,最小化,最大化的按鈕的Frame了 f.setVisible(true); //再加上這一句就可以顯示一個在左上角,擁有指定大小的Frame了 f.setSize(300,200); //再加上這一句就可以把Frame放在最中間了 f.setLocationRelativeTo(null); //如果沒有這一句,在點擊關閉Frame的時候程序其實還是在執行狀態中的,加上這一句才算是真正的把資源釋放掉了 f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } }?
package swing1;import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; public class EmptyJFrame2 extends JFrame{ EmptyJFrame2(){ initGUI(); } private void initGUI(){ setVisible(true); setSize(300,200); setLocationRelativeTo(null); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { EmptyJFrame2 f = new EmptyJFrame2(); } }); } }?
?
圖圖;
?
碼碼;
?
package swing1;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; public class AddComponent extends JFrame{ public AddComponent(){ initGUI(); } private void initGUI(){ setVisible(true); setSize(300,200); setLocationRelativeTo(null); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); //創建元件 JButton jButton1 = new JButton("jButton1"); //添加元件 add(jButton1); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { AddComponent f = new AddComponent(); } }); } }?
package swing1;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; public class AddComponent2 extends JFrame{ //定義變量 private JButton jButton1; public AddComponent2(){ initGUI(); addComp(); } private void addComp(){ //初始化 jButton1 = new JButton("jButton1"); //添加元件 add(jButton1); } private void initGUI(){ setVisible(true); setSize(300,400); setLocationRelativeTo(null); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { AddComponent2 f = new AddComponent2(); } }); } }?
?
圖圖;
?
碼碼;
?
package swing1;import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; import java.awt.*; /*** swing基礎實例* @author HZ20232**/ public class Hello{public static void main(String args[])throws Exception{NewFrame frame1 = new NewFrame();frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//一定要設置關閉frame1.setVisible(true);} } class NewFrame extends JFrame{private JLabel label1;private JButton button1;private JTextField text1;private JComboBox box;private JMenuBar menuBar;private JSlider slider;private JSpinner spinner;private JToolBar toolBar;public NewFrame(){super();this.setSize(300,500);this.getContentPane().setLayout(null);//設置布局控制器// this.getContentPane().setLayout(new FlowLayout());//設置布局控制器// this.getContentPane().setLayout(new GridLayout(1,2));//設置布局控制器,需要給出設定的行列數目// this.getContentPane().setLayout(new BorderLayout());//設置布局控制器,以North,South,West,East,來控制控件布局// this.getContentPane().setLayout(new GridBagLayout());//設置布局控制器this.add(this.getTextField(),null);//添加文本框this.add(this.getButton(),null);//添加按鈕this.add(this.getLabel(),null);//添加標簽this.add(this.getBox(),null);//添加下拉列表框this.setJMenuBar(this.getMenu());//添加菜單this.add(this.getSlider(),null);this.add(this.getSpinner(),null);this.add(this.getToolBar(),null);this.setTitle("Hello World!");//設置窗口標題}private JToolBar getToolBar(){if(toolBar==null){toolBar = new JToolBar();toolBar.setBounds(103,260,71,20);toolBar.setFloatable(true);}return toolBar;}private JSpinner getSpinner(){if(spinner==null){spinner = new JSpinner();spinner.setBounds(103,220, 80,20);spinner.setValue(100);}return spinner;}private JSlider getSlider(){if(slider==null){slider = new JSlider();slider.setBounds(103,200,100, 20);slider.setMaximum(100);slider.setMinimum(0);slider.setOrientation(0);slider.setValue(0);}return slider;}/*** 菜單的級別JMenuBar->JMenu->JMenuItem* 三級都是1:n的關系* 最后添加菜單用的SetJMenuBar方法* @return 建立好的菜單*/private JMenuBar getMenu(){if(menuBar==null){menuBar = new JMenuBar();JMenu m1 = new JMenu();m1.setText("文件");JMenu m2 = new JMenu();m2.setText("編輯");JMenu m3 = new JMenu();m3.setText("幫助");JMenuItem item11 = new JMenuItem();item11.setText("打開");JMenuItem item12 = new JMenuItem();item12.setText("保存");JMenuItem item13 = new JMenuItem();item13.setText("退出");JMenuItem item21 = new JMenuItem();item21.setText("復制");JMenuItem item22 = new JMenuItem();item22.setText("拷貝");JMenuItem item23 = new JMenuItem();item23.setText("剪切");JMenuItem item31 = new JMenuItem();item31.setText("歡迎");JMenuItem item32 = new JMenuItem();item32.setText("搜索");JMenuItem item33 = new JMenuItem();item33.setText("版本信息");m1.add(item11);m1.add(item12);m1.add(item13);m2.add(item21);m2.add(item22);m2.add(item23);m3.add(item31);m3.add(item32);m3.add(item33);menuBar.add(m1);menuBar.add(m2);menuBar.add(m3);}return menuBar;}/*** 設置下拉列表框* @return*/private JComboBox getBox(){if(box==null){box = new JComboBox();box.setBounds(103,140,71,27);box.addItem("1");box.addItem("2");box.addActionListener(new comboxListener());//為下拉列表框添加監聽器類}return box;}private class comboxListener implements ActionListener{public void actionPerformed(ActionEvent e){Object o = e.getSource();System.out.println(o.toString());}}/*** 設置標簽* @return 設置好的標簽*/private JLabel getLabel(){if(label1==null){label1 = new JLabel();label1.setBounds(34,49,53,18);label1.setText("Name");label1.setToolTipText("JLabel");}return label1;}/*** 設置按鈕* @return 設置好的按鈕*/private JButton getButton(){if(button1==null){button1 = new JButton();button1.setBounds(103,110,71,27);button1.setText("OK");button1.setToolTipText("OK");button1.addActionListener(new HelloButton());//添加監聽器類,其主要的響應都由監聽器類的方法實現}return button1;}/*** 監聽器類實現ActionListener接口,主要實現actionPerformed方法* @author HZ20232**/private class HelloButton implements ActionListener{public void actionPerformed(ActionEvent e){System.out.println("Hello world!");}}/*** 設定文本域* @return*/private JTextField getTextField(){if(text1==null){text1 = new JTextField();text1.setBounds(96,49,160,20);}return text1;} }?
?
圖圖;
?
碼碼;
?
package swing1;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(); } }
圖圖;
?
?
總結
以上是生活随笔為你收集整理的java swing 例子(一些)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring 源码阅读入门
- 下一篇: 图解观察托管程序线程