java图形界面初学
JPanel,JFrame ,JButton,JMenu,JMenuBar,JMenuItem,JToolBar,JLabel,
BorderLayout ,GridLayout,
ActionListener,InputEvent
package window;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
public class Mainwindow {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Mainwindow te =new Mainwindow();
//setLookAndFeel();
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int)screensize.getWidth();//1366
int height = (int)screensize.getHeight();//768
JFrame f=new JFrame("文件资源管理器");
f.setSize(width/2, height/2);
f.setLocation(width/4, height/4);
f.setJMenuBar(te.menu());
te.borderlayout(f);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.setVisible(true);
}
/*
* 主窗口
*/
public void borderlayout(JFrame f) {
f.setLayout(new GridLayout(8,1));
JPanel j1=new JPanel();
JPanel j2=new JPanel();
JPanel j3=new JPanel();
j1.setLayout(new GridLayout(1,6));
j2.setLayout(new GridLayout(1,6));
j3.setLayout(new GridLayout(1,6));
this.tool(j1);
this.tool1(j2);
this.tool2(j3);
f.add(j1);
f.add(j2);
f.add(j3);
}
/*
* 菜单栏
*/
public JMenuBar menu() {
JMenuBar jMb=new JMenuBar();
JMenu jm=new JMenu();
String[] s= {"菜单","文件","窗口","账户","设置","帮助"};
for(int i=0;i<s.length;i++) {
jMb.add(jm=new JMenu(s[i]));
if(jm.getText()=="菜单") { /*getText获取JMenu名称*/
menu1(jm);
}
}
return jMb;
}
/*
* 菜单项
*/
public void menu1(JMenu jm) {
JMenuItem m1= new JMenuItem("1");
JMenuItem m2= new JMenuItem("2");
JMenuItem m3= new JMenuItem("3");
JMenuItem m4= new JMenuItem("4");
jm.add(m1);
jm.add(m2);
jm.add(m3);
jm.add(m4);
jm.addSeparator();
jm.add(new JMenuItem("All"));
}
/*
* 工具栏,添加到面板
*/
public void tool(JPanel f) {
JLabel jl=new JLabel("学习软件:");
jl.setHorizontalAlignment(JLabel.CENTER);
JButton jb1=new JButton("HBuilder");
JButton jb2=new JButton("百度网盘");
JButton jb4=new JButton("MATLAB");
JButton jb5=new JButton("ECLIPSE");
JButton jb6=new JButton("搜狗");
/*
* 创建弹出菜单
*/
JPopupMenu jp=new JPopupMenu();
jp.setLayout(new GridLayout(2,1));
jp.add(new JButton("修改链接位置"));
jp.add(new JButton("关闭软件"));
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("D:\\HBuilder.9.0.2.windows\\HBuilder\\HBuilder.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getButton()== MouseEvent.BUTTON3) {
jp.show(f, e.getX(), e.getY());
}
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("D:/BaiduNetdisk/BaiduNetdisk.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("F:/MATLAB/R2018b/bin/matlab.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("F:\\eclipse\\java-oxygen\\eclipse\\eclipse.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("F:/SogouExplorer/SogouExplorer.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
f.add(jl);f.add(jb1);f.add(jb2);f.add(jb4);f.add(jb5);f.add(jb6);
}
public void tool1(JPanel f) {
JLabel jl=new JLabel("娱乐影音:");
jl.setHorizontalAlignment(JLabel.CENTER);
JButton jb1=new JButton("HBuilder");
JButton jb2=new JButton("百度网盘");
JButton jb4=new JButton("MATLAB");
JButton jb5=new JButton("ECLIPSE");
JButton jb6=new JButton("搜狗");
/*
* 创建弹出菜单
*/
JPopupMenu jp=new JPopupMenu();
jp.setLayout(new GridLayout(2,1));
jp.add(new JButton("修改链接位置"));
jp.add(new JButton("关闭软件"));
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("D:\\HBuilder.9.0.2.windows\\HBuilder\\HBuilder.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getButton()== MouseEvent.BUTTON3) {
jp.show(f, e.getX(), e.getY());
}
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("D:/BaiduNetdisk/BaiduNetdisk.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("F:/MATLAB/R2018b/bin/matlab.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("F:\\eclipse\\java-oxygen\\eclipse\\eclipse.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("F:/SogouExplorer/SogouExplorer.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
f.add(jl);f.add(jb1);f.add(jb2);f.add(jb4);f.add(jb5);f.add(jb6);
}
public void tool2(JPanel f) {
JLabel jl=new JLabel("工具:");
jl.setHorizontalAlignment(JLabel.CENTER);
JButton jb1=new JButton("HBuilder");
JButton jb2=new JButton("百度网盘");
JButton jb4=new JButton("MATLAB");
JButton jb5=new JButton("ECLIPSE");
JButton jb6=new JButton("搜狗");
/*
* 创建弹出菜单
*/
JPopupMenu jp=new JPopupMenu();
jp.setLayout(new GridLayout(2,1));
jp.add(new JButton("修改链接位置"));
jp.add(new JButton("关闭软件"));
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("D:\\HBuilder.9.0.2.windows\\HBuilder\\HBuilder.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getButton()== MouseEvent.BUTTON3) {
jp.show(f, e.getX(), e.getY());
}
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("D:/BaiduNetdisk/BaiduNetdisk.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("F:/MATLAB/R2018b/bin/matlab.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("F:\\eclipse\\java-oxygen\\eclipse\\eclipse.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
jb6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("F:/SogouExplorer/SogouExplorer.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
f.add(jl);f.add(jb1);f.add(jb2);f.add(jb4);f.add(jb5);f.add(jb6);
}
/*
* 界面风格
*/
/*private static void setLookAndFeel() {
// TODO 自动生成的方法存根
try {
javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
}catch(Exception e) {
e.printStackTrace();
}
}*/
}