【java Swing】开发控件,看这就够了

0、JFrame

      JFrame frame = new JFrame("标题");
		
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setLocationByPlatform(true);
        //让窗体可见
        frame.setVisible(true);
        //重置窗体大小
        frame.setResizable(false);
        frame.setSize(1024,768);
        // 设置窗体居中显示
        frame.setLocationRelativeTo(frame.getOwner());
        frame.setLayout(null);  
		
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

1、按钮JButton

JButton login = new JButton("登录");
        frame.add(login);  
        login.setBounds(left, 10, 100, 50);

//点击事件监听
 login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
    //TODO
    }
}

2、JLable

static JLabel type1_Label = new JLabel("地址:",JLabel.CENTER);

 type1_Label.setName("1");
        type1_Label.setFont(new java.awt.Font("宋体", 1, 20));
        type1_Label.setBounds(10, 10, 230, 50);
        frame.add(type1_Label);

3、输入框

static JTextField type1_t = new JTextField();

 type1_t.setFont(new java.awt.Font("宋体", 1, 20));
        type1_t.setBounds(10, 10, 230, 50);
        frame.add(type1_t);

4、单选

        final JRadioButton  pmje_1 =new JRadioButton ("10以下");
        final JRadioButton  pmje_2 =new JRadioButton ("10-50");
        final JRadioButton  pmje_3 =new JRadioButton ("50-100");
        final JRadioButton  pmje_4 =new JRadioButton ("100以上");
        
        final ButtonGroup group=new ButtonGroup();
		group.add(pmje_1);
		group.add(pmje_2);
		group.add(pmje_3);
		group.add(pmje_4);

        Integer width=100;
        Integer left=150;

        pmje_1.setBounds(left, 10, width+20, 50);
        pmje_2.setBounds(left+jg+10, 10, width+30, 50);
        pmje_3.setBounds(left+jg*2+30, 10, width+30, 50);
        pmje_4.setBounds(left+jg*3+60, 10, width+30, 50);

        frame.add(pmje_1);
        frame.add(pmje_2);
        frame.add(pmje_3);
        frame.add(pmje_4);

//是否选中,用pmje_1.isSelected()
//默认选中,用pmje_1.setSelected(true);
      

5、多选JCheckBox

        final  JCheckBox syys_01 =new JCheckBox("1");
        final  JCheckBox syys_02 =new JCheckBox("2");
        final  JCheckBox syys_03 =new JCheckBox("3");
        final  JCheckBox syys_04 =new JCheckBox("4");
        Integer left=150; 
        Integer jg=170;
        Integer width =50;
        syys_01.setBounds(left, 10+60*1, width, 50);
        syys_02.setBounds(left+jg, 10+60*1, width, 50);
        syys_03.setBounds(left+jg*2, 10+60*1, width, 50);
        syys_04.setBounds(left+jg*3, 10+60*1, width, 50);

        frame.add(syys_01);
        frame.add(syys_02);
        frame.add(syys_03);
        frame.add(syys_04);    
//默认选中 syys_01.setSelected(true);  是否选中xcqk_0.isSelected()

6、多行输入框JtextArea

static JTextArea log_textArea= new JTextArea();

 JScrollPane  sp=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		sp.setViewportView(log_textArea);
		sp.setBounds(100, 70, 800, 600);
		frame.add(sp);

7、列表

 final JTable table;
			  //定义一维数据作为列标题
	    Object[] columnTitle = {"列1" , "列2" , "列3"};
	    //以二维数组和一维数组来创建一个JTable对象
	 
		Integer j = 5;
	    Object[][] tableData = new Object[j][3];
		for(int i=0;i<j;i++){
			tableData[i]=new Object[]{i,i+1,i+2};
		}
		
	    table = new JTable(tableData , columnTitle){
	    	public boolean isCellEditable(int row, int column) {	// 表格不可编辑
	    		return false;
	    	}
	    };
		    
	    table.getColumnModel().getColumn(0).setPreferredWidth(80);

	    table.getColumnModel().getColumn(1).setPreferredWidth(240);
	    
frame.setContentPane(table);

8、TabbedPane

class TabbedPaneFrame extends JFrame {
    private JTabbedPane tabbedPane;

    private int count = 0;

    JFrame j = new JFrame();

    public TabbedPaneFrame() {
       // 添加选项卡
       tabbedPane = new JTabbedPane();

       tabbedPane.addTab("tab1", null);

       tabbedPane.addTab("tab2", null);

       tabbedPane.addTab("tab3", null);

       tabbedPane.addTab("tab4", null);

       // 添加选项卡面板

       add(tabbedPane, "Center");

       // 添加监听器

       tabbedPane.addChangeListener(new ChangeListener() {

           public void stateChanged(ChangeEvent e) {

              int n = tabbedPane.getSelectedIndex();
              loadTab(n);
           }

       });

       loadTab(0);
    }

    private void loadTab(int n) {
	try {
		
		List<Record> records = null;
		if(0==n){
			 //todo
		}else if(1==n){
			//todo
		}else if(2==n){
			//todo
		}else if(3==n){
			//todo
		}
		
		    
	    tabbedPane.setComponentAt(n, XXXX);
	} catch (Exception e) {
		e.printStackTrace();
	}
       
    }

}


//调用


 TabbedPaneFrame frame = new TabbedPaneFrame();
       frame.setTitle("TabbedPane");
       frame.setSize(1200, 700);
       frame.setVisible(true);
       Point point = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
       frame.setBounds(point.x - 1200 / 2, point.y - 768 / 2+200, 1200, 700);

9、引入浏览器,需引入的jar包:djnativeswing.jar,djnativeswing-swt.jar

import java.awt.BorderLayout;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserCommandEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserListener;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserNavigationEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserWindowOpeningEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserWindowWillOpenEvent;

public class TestLiulanqi extends JPanel {
    
    public TestLiulanqi(String url) {
        super(new BorderLayout());
        final JPanel webBrowserPanel = new JPanel(new BorderLayout());
        final JWebBrowser webBrowser = new JWebBrowser();
        webBrowser.navigate(url);
        webBrowser.setButtonBarVisible(false);
        webBrowser.setMenuBarVisible(false);
        webBrowser.setBarsVisible(false);
        webBrowser.setStatusBarVisible(false);
        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
        add(webBrowserPanel, BorderLayout.CENTER);
        //执行Js代码
       
        webBrowser.addWebBrowserListener(new WebBrowserListener() {
			
			public void windowWillOpen(WebBrowserWindowWillOpenEvent arg0) {
				System.out.println("windowWillOpen");
				
			}
			
			public void windowOpening(WebBrowserWindowOpeningEvent arg0) {
				System.out.println("windowOpening");
				
			}
			
			public void windowClosing(WebBrowserEvent arg0) {
				System.out.println("windowClosing");
				
			}
			
			public void titleChanged(WebBrowserEvent arg0) {
				System.out.println("titleChanged");
				
			}
			
			public void statusChanged(WebBrowserEvent arg0) {
				System.out.println(arg0.getWebBrowser().getStatusText());
				
			}
			
			public void locationChanging(WebBrowserNavigationEvent arg0) {
				System.out.println("locationChanging");
			}
			
			public void locationChanged(WebBrowserNavigationEvent arg0) {
				
			}
			
			public void locationChangeCanceled(WebBrowserNavigationEvent arg0) {
				System.out.println("locationChangeCanceled");
				
			}
			
			public void loadingProgressChanged(WebBrowserEvent arg0) {
				System.out.println("loadingProgressChanged");
				
			}
			
			public void commandReceived(WebBrowserCommandEvent arg0) {
				System.out.println("commandReceived");
				
			}
		});
    }
 
 
    /**
     * 在swing里内嵌浏览器
     * @param url  要访问的url
     * @param title    窗体的标题
     */
    public  static void  openForm(final String url,final String title){
        UIUtils.setPreferredLookAndFeel();
        NativeInterface.open();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	 JFrame frame = new JFrame("登录");
                //设置窗体关闭的时候不关闭应用程序
            	frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.getContentPane().add(new TestLiulanqi(url), BorderLayout.CENTER);
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                frame.setLocationByPlatform(true);
                //让窗体可见
                frame.setVisible(true);
                //重置窗体大小
                frame.setResizable(true);
                // 设置窗体的宽度、高度
                frame.setSize(1200, 900);
                // 设置窗体居中显示
                frame.setLocationRelativeTo(frame.getOwner());
            }
        });
        NativeInterface.runEventPump();
    }
 
    public static void main(String[] args) {
        openForm("https://www.baidu.com","百度");
    }
}

10、日历控件,日期选择,需引入的jar包:DatePicker.jar

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.eltima.components.ui.DatePicker;

public class Datepick {
	 static  String DefaultFormat = "yyyy.MM.dd";
     static SimpleDateFormat sdf = new SimpleDateFormat(DefaultFormat);
    public static void main(String[] args) {

        final JFrame f = new JFrame("LoL");
        f.setSize(400, 300);
        f.setLocation(500, 500);
        f.setLayout(null);

        final DatePicker datepick;
        datepick = getDatePicker("2019.12.12");
        f.add(datepick);
        
        datepick.setLocation(10, 10);
        datepick.setSize(200,50);
        

        JButton b = new JButton("获取时间");
        b.setBounds(137, 183, 100, 30);
        f.add(b);

        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	Date date = (Date)datepick.getValue();
            	System.out.println(sdf.format(date));
                JOptionPane.showMessageDialog(f, "获取控件中的日期:" + datepick.getValue());
                System.out.println(datepick.getValue());//这是一个java.util.Date对象
            }
        });

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setVisible(true);
    }

    public static DatePicker getDatePicker(String strDate) {
        final DatePicker datepick;
        // 当前时间
        Date date;
		try {
			date = sdf.parse(strDate);
		} catch (Exception e) {
			date=new Date();
		}
       
        // 字体
        Font font = new Font("Times New Roman", Font.BOLD, 14);

        Dimension dimension = new Dimension(177, 24);

        int[] hilightDays = { 1, 3, 5, 7 };

        int[] disabledDays = { 4, 6, 5, 9 };
    //构造方法(初始时间,时间显示格式,字体,控件大小)
        datepick = new DatePicker(date, DefaultFormat, font, dimension);
        datepick.setSize(90,40);
//        datepick.setLocation(137, 83);//设置起始位置
        
        /*
        //也可用setBounds()直接设置大小与位置
        datepick.setBounds(137, 83, 177, 24);
        */
        // 设置一个月份中需要高亮显示的日子
//        datepick.setHightlightdays(hilightDays, Color.red);
//        // 设置一个月份中不需要的日子,呈灰色显示
//        datepick.setDisableddays(disabledDays);
        // 设置国家
        datepick.setLocale(Locale.CHINA);
        // 设置时钟面板可见
        datepick.setTimePanleVisible(false);
        return datepick;
    }
    
    static String getValue(Object obj){
    	return sdf.format((Date)obj);
    }
}





static DatePicker type1_datepick1s = null;
type1_datepick1s = Datepick.getDatePicker(“2019-04-30”);
type1_datepick1s.setLocation(10, 10+50);
frame.add(type1_datepick1s);

//获取值  Datepick.getValue(type1_datepick1s.getValue()))



 

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值