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()))