扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
java做窗口的话,需要用swing技术,之后创建JFrame 等组件,即可完成窗口创建工作。
创新互联公司服务项目包括遂川网站建设、遂川网站制作、遂川网页制作以及遂川网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,遂川网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到遂川省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
package inter.frame;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;public class MenuTest { /**
* @param args
*/
JFrame frame; //定义一个窗口架构
JMenuBar mb;//定义窗口的菜单工具栏
JMenu m; //定义菜单
JMenuItem mi1;//定义菜单的内容
JMenuItem mi2; //定义菜单的内容
public MenuTest() {
initFrame();
initAction();
}
public void initFrame() {
frame = new JFrame();
mb = new JMenuBar();
m = new JMenu("学生查询");
mi1 = new JMenuItem("确认");
mi2 = new JMenuItem("取消"); m.add(mi1);
m.add(mi2);
mb.add(m);
frame.add(mb, BorderLayout.NORTH);
frame.setSize(300, 300); //设置窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置退出时关闭窗口
frame.setVisible(true);//设置窗口可见
} public void initAction() {
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具体实现代码根据实际要求填写
System.out.println("click");
JOptionPane.showMessageDialog(null, "你点击了确定按钮");
}
});
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具体实现代码根据实际要求填写
JOptionPane.showMessageDialog(null, "你点击了取消按钮");
}
});
} public static void main(String[] args) {
new MenuTest();//执行菜单创建
}}
我这有个联系swing的界面大全,里面包含了常用的主件,可以直接用
package demo.synth;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.synth.SynthLookAndFeel;
import javax.swing.table.*;
public class SwingTest extends JFrame {
/**
* 主模块,初始化所有子模块,并设置主框架的相关属性
*/
public SwingTest() {
// 初始化所有模块
MenuTest menuTest = new MenuTest();
LeftPanel leftPanel = new LeftPanel();
RightPanel rightPanel = new RightPanel();
BottomPanel bottomPanel = new BottomPanel();
CenterPanel centerPanel = new CenterPanel();
// 设置主框架的布局
Container c = this.getContentPane();
// c.setLayout(new BorderLayout());
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(800, 600);
setTitle("* All Groupware Of Swing *");
// 隐藏frame的标题栏,此功暂时关闭,以方便使用window事件
// setUndecorated(true);
setLocation(200, 150);
this.setVisible(true);
}
// //////////////////////////////////////////////////////////////////////////
/**
* 菜单栏处理模块 JMenuBar --+ --JMenu--+ --JMenuItem --ActionListener
*
*/
class MenuTest extends JMenuBar {
private JDialog aboutDialog;
/**
* 菜单初始化操作
*/
public MenuTest() {
JMenu fileMenu = new JMenu("File");
JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_E);
JMenuItem aboutMenuItem = new JMenuItem("About...", 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.setVisible(true);
}
});
}
/**
* 返回关于对话框
*/
public JDialog getAboutDialog() {
return aboutDialog;
}
/**
* 设置"关于"对话框的外观及响应事件,操作和JFrame一样都是在内容 框架上进行的
*/
public void initAboutDialog() {
aboutDialog.setTitle("About");
Container con = aboutDialog.getContentPane();
// Swing 中使用html语句
Icon icon = new ImageIcon("smile.gif");
JLabel aboutLabel = new JLabel("htmlbfont size=5"
+ "centerAll Groupware Of Swing !" + "brKasam.Huang",
icon, JLabel.CENTER);
// JLabel aboutLabel = new JLabel("Swing
// 组件大全简体版!",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();
}
});
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 最左边模块,继承JPanel,初始化内容为JTree JPanel--+ --JTree
*/
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) {
// 内隐类不能直接引用外部类tree,1.外部变量可申明为final 2.新建外部类的对象
JTree tree = (JTree) e.getSource();
DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
i++;
selectNode.add(new DefaultMutableTreeNode("" + i));
// Object s = tree.getLastSelectedPathComponent();//
// getSelectedValue();
// JOptionPane.showMessageDialog(null, s + " is selected",
// "Message !", JOptionPane.YES_OPTION);
}
});
tree.setPreferredSize(new Dimension(100, 300));
// tree.setEnabled(true);
JScrollPane scrollPane = new JScrollPane(tree);
// scrollPane.setSize(100,350);
this.add(scrollPane);
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 最下面层模块,继承JPanel,初始化内容为进度条,并由定时器控制 JPanel--+ --JProcessBar --Timer
*/
class BottomPanel extends JPanel {
private JProgressBar pb;
public BottomPanel() {
pb = new JProgressBar();
pb.setPreferredSize(new Dimension(550, 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);
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 最右边模块,继承JPanel,初始化各种按钮 JPanel--+ --JButton --JToggleButton -- JList --
* JCombox --JCheckBox ....
*/
class RightPanel extends JPanel {
public RightPanel() {
this.setLayout(new GridLayout(8, 1));
// 初始化各种按钮
JCheckBox checkBox = new JCheckBox("CheckButton");
JButton button = new JButton("OpenFile");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser file = new JFileChooser();
int result = file.showOpenDialog(new JPanel());
if (result == file.APPROVE_OPTION) {
String fileName = file.getSelectedFile().getName();
String dir = file.getCurrentDirectory().toString();
JOptionPane.showConfirmDialog(null,
dir + "" + fileName, "SelectFile",
JOptionPane.YES_OPTION);
}
}
});
JToggleButton toggleButton = new JToggleButton("RadioButton");
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton radioButton1 = new JRadioButton("RadioButton 1", false);
JRadioButton radioButton2 = new JRadioButton("RadioButton 2", false);
// 组合框的处理
JComboBox comboBox = new JComboBox();
comboBox.setToolTipText("ComboBox");
comboBox.addItem("程序员");
comboBox.addItem("分析员");
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox comboBox1 = (JComboBox) e.getSource();
comboBox1.addItem("程序员");
comboBox1.addItem("分析员");
}
});
// 列表框的处理
DefaultListModel litem = new DefaultListModel();
litem.addElement("SAT");
litem.addElement("SUN");
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, "Message !",
JOptionPane.YES_OPTION);
}
});
// 增加按钮组
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
// 增加各种按钮到JPanel中显示
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));
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 中间层模块,继承JPanel,初始化页签,并在页签中设置文本区,表格, 文本区上下用分隔条分隔 JPanel--+ -JTabbedPane--+
* --Draw --JTable -JTextAreas -JText --JPopupMenu
*/
class CenterPanel extends JPanel {
public CenterPanel() {
JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP);
JTextField textField = new JTextField(
"Text Area,Click Text Button Will select file !");
textField.setActionCommand("textField");
JTextPane textPane = new JTextPane();
textPane.setCursor(new Cursor(Cursor.TEXT_CURSOR));
textPane.setText("Editor,Click the TextArea .");
// textPane.setForeground(new ImageIcon(""));
textPane.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
JTextPane textPane = (JTextPane) e.getSource();
textPane.setText("Editor click command success!");
// textField.setText(""+textPane.getText());
}
});
/*
* UpperCaseDocument doc = new Document();
* textField.setDocumentsetDocument(doc);
* doc.addDocumentListener(new DocumentListener() { public void
* changedUpdate(DocumentEvent e){} public void
* removeUpdate(DocumentEvent e){} public void
* insertUpdate(DocumentEvent e) { Document text =
* (Document)e.getDocument(); text.setText("Copy Success !"); } });
*/
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
textField, textPane);
JTable table = new JTable(10, 10);
// UIManager.get("Table.focusCellForeground");
// table.showHorizontalLines(true);
// table.showVerticalLines(true);
// table.setGridColor(Color.blue);
JPanel pane = new JPanel();
pane.add(table.getTableHeader(), BorderLayout.NORTH);
pane.add(table);
tab.addTab("Text Action", splitPane);
// tab.addTab(table.getTableHeader());
tab.addTab("Table Action", pane);
tab.setPreferredSize(new Dimension(500, 600));
this.add(tab);
this.setEnabled(true);
}
}
public static void main(String args[]){
new SwingTest();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Login extends JFrame{
nbsp;public Login() {
nbsp;nbsp;setLayout(new GridLayout(1,2));//网格布局,1行2列,放置左面板和右面板
nbsp;nbsp;setTitle("发表iPhone说说");//设置窗口标题
nbsp;nbsp;setSize(550,300);//设置大小
nbsp;nbsp;setLocationRelativeTo(null);//设置窗口位置
nbsp;nbsp;setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭按钮动作
nbsp;nbsp;//左面板
nbsp;nbsp;JPanel leftPanel = new JPanel();
nbsp;nbsp;leftPanel.setBorder(BorderFactory.createTitledBorder("登录手机腾讯网"));//给左面板加上边框,并添加文字
nbsp;nbsp;leftPanel.setLayout(new BorderLayout());
nbsp;nbsp;JPanel leftPanelTop = new JPanel();
nbsp;nbsp;JPanel leftPanelBottom = new JPanel();
nbsp;nbsp;leftPanel.add(leftPanelTop, BorderLayout.CENTER);
nbsp;nbsp;leftPanel.add(leftPanelBottom, BorderLayout.SOUTH);
nbsp;nbsp;leftPanelTop.setLayout(new GridLayout(3,2));//网格布局,3行2列
nbsp;nbsp;nbsp;nbsp;nbsp;
nbsp;nbsp;//========================请在下面写上你的代码
nbsp;nbsp;JLabel qq = new JLabel("QQ号码");
nbsp;nbsp;JTextField qqNum = new JTextField();
nbsp;nbsp;JLabel pwd = new JLabel("QQ密码");
nbsp;nbsp;JPasswordFieldnbsp; qqPwd = new JPasswordField();
nbsp;nbsp;JLabel verify = new JLabel("验证码");
nbsp;nbsp;JTextField qqVer = new JTextField();
nbsp;nbsp;leftPanelTop.add(qq);
nbsp;nbsp;leftPanelTop.add(qqNum);
nbsp;nbsp;leftPanelTop.add(pwd);
nbsp;nbsp;leftPanelTop.add(qqPwd);
nbsp;nbsp;leftPanelTop.add(verify);
nbsp;nbsp;leftPanelTop.add(qqVer);
nbsp;nbsp;leftPanelBottom.setLayout(new GridLayout(1,2));
nbsp;nbsp;JLabel i_vc = new JLabel(new ImageIcon("images/VerificationCode.jpg"));//这是显示验证码的标签,帮你写好了,后面直接调用即可。
nbsp;nbsp;JButton bt1 = new JButton("帐号密码登陆");
nbsp;nbsp;JButton bt2 = new JButton("带验证码登陆");
nbsp;nbsp;JPanel btPanel = new JPanel();
nbsp;nbsp;btPanel.setLayout(new GridLayout(2,1));
nbsp;nbsp;btPanel.add(bt1);
nbsp;nbsp;btPanel.add(bt2);
nbsp;nbsp;leftPanelBottom.add(i_vc);
nbsp;nbsp;leftPanelBottom.add(btPanel);
nbsp;nbsp;//============================================
nbsp;nbsp;add(leftPanel);//将左面板放入窗体中
nbsp;nbsp;//右面板
nbsp;nbsp;JPanel rightPanel = new JPanel();
nbsp;nbsp;rightPanel.setBorder(BorderFactory.createTitledBorder("发表iPhone说说"));//给右面板加上边框,并添加文字
nbsp;nbsp;//========================请在下面写上你的代码
nbsp;nbsp;JTextArea msg = new JTextArea();
nbsp;nbsp;JButton submit = new JButton("马上发表说说");
nbsp;nbsp;rightPanel.setLayout(new BorderLayout());
nbsp;nbsp;rightPanel.add(msg, BorderLayout.CENTER);
nbsp;nbsp;rightPanel.add(submit, BorderLayout.SOUTH);
nbsp;nbsp;//============================================
nbsp;nbsp;add(rightPanel);//将右面板放入窗体中
nbsp;nbsp;setVisible(true);//使窗体可见
nbsp;}
nbsp;public static void main(String[] args){
nbsp;nbsp;new Login();
nbsp;}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流