本文主要是介绍java Swing之JTabbedPane用法实例 JTable用法实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一款用JTabbedPane选项卡实现的实例,其功能领域有:笑话、新闻、星座运势、成语、解梦。询问不同领域的问题,自动切换至对应的选项卡来输出答案。用到的技术主要有:javax.swing.JFrame,javax.swing.JTabbedPane,javax.swing.JScrollPane,javax.swing.GroupLayout,javax.swing.JTable,Jsoup。
程序运行效果图见(上一篇)Olami智能语义解析—学习娱乐小能手
该程序可运行jar包见xiaonengshou.jar
代码原文如下:
1、MyJFrame:界面显示。
import java.awt.Color;
import java.awt.Desktop;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;public class MyJFrame extends javax.swing.JFrame {private static final long serialVersionUID = 1L;private JButton jButton_send;private JLabel jLabel1;private JLabel jLabel2;private JPanel jPanel1;private JTextField jTextField1;private JTextArea areaTxt;private JTabbedPane tabbedPanel;private JTable table;private String introduce = "你好,我是娱乐学习小能手。\r\n 我可以讲笑话、查运势、查新闻、查公交线、查成语、还可以解梦呢!\r\n\r\n";private String[] introduces = {"笑话类:\r\n 1、讲笑话。\r\n 2、来个笑话。\r\n 趣图类:\r\n 1、看趣图。\r\n 2、来张趣图。","新闻类:\r\n 1、娱乐头条。\r\n 2、看新闻。","星座类:\r\n 1、摩羯座今日运势。\r\n 2、摩羯座本月运势。 \r\n 3、摩羯座的本周运势","成语类:\r\n 1、日积月累是什么意思。\r\n 2、有龙字的成语 。\r\n 3、带龙马的成语。","解梦类:\r\n 1、我梦到了黄金。\r\n 2、梦到了黄金有什么寓意。"};private AppProcess app;public MyJFrame() { initComponents();app = new AppProcess();} private void initComponents() {jPanel1 = new JPanel();jLabel1 = new JLabel();jLabel2 = new JLabel();jTextField1 = new JTextField();jButton_send = new JButton();tabbedPanel = new JTabbedPane(JTabbedPane.TOP);setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setMinimumSize(new java.awt.Dimension(430, 280));jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("智能问答"));JLabel jl_href = new JLabel("<html><a href=''>相关代码详细说明</a></html>"); jl_href.addMouseListener(new MouseListener(){@Overridepublic void mouseClicked(MouseEvent arg0) {// TODO Auto-generated method stubDesktop desktop = Desktop.getDesktop();try {desktop.browse(new URI("http://blog.csdn.net/u011827504/article/details/75284546"));} catch (IOException | URISyntaxException e1) {e1.printStackTrace();}}@Overridepublic void mouseEntered(MouseEvent arg0) {}@Overridepublic void mouseExited(MouseEvent arg0) {}@Overridepublic void mousePressed(MouseEvent arg0) {}@Overridepublic void mouseReleased(MouseEvent arg0) {}});jLabel1.setText("提问:");jTextField1.setText("娱乐头条");jButton_send.setText("send");jButton_send.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton_sendActionPerformed(evt);}}); GroupLayout jPanel1Layout = new GroupLayout(jPanel1);GroupLayout.SequentialGroup hGroup = jPanel1Layout.createSequentialGroup();hGroup.addGap(15);hGroup.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addComponent(jLabel1));hGroup.addGap(5);hGroup.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addComponent(jTextField1).addComponent(jl_href).addComponent(jLabel2));hGroup.addGap(5);hGroup.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addComponent(jButton_send));hGroup.addGap(15);jPanel1Layout.setHorizontalGroup(hGroup);GroupLayout.SequentialGroup vGroup = jPanel1Layout.createSequentialGroup();vGroup.addGap(8);vGroup.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addComponent(jl_href));vGroup.addGap(8);vGroup.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addComponent(jLabel1).addComponent(jTextField1).addComponent(jButton_send));vGroup.addGap(8);vGroup.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING).addComponent(jLabel2));vGroup.addGap(8);//设置垂直组jPanel1Layout.setVerticalGroup(vGroup);jPanel1.setLayout(jPanel1Layout);//创建面板 JScrollPane panel_joke = new JScrollPane(); JScrollPane panel_news = new JScrollPane(); JScrollPane panel_hor = new JScrollPane(); JScrollPane panel_idiom = new JScrollPane(); JScrollPane panel_oneir = new JScrollPane(); areaTxt = new JTextArea();areaTxt.setColumns(28);areaTxt.setRows(15);areaTxt.setText(introduce + introduces[0]); areaTxt.setEditable(false);areaTxt.setLineWrap(true);areaTxt.setAutoscrolls(true);panel_joke.setViewportView(areaTxt);tabbedPanel.addTab("笑话",panel_joke); tabbedPanel.addTab("新闻",panel_news); tabbedPanel.addTab("星座",panel_hor); tabbedPanel.addTab("成语",panel_idiom); tabbedPanel.addTab("解梦",panel_oneir); tabbedPanel.addChangeListener(new ChangeListener(){ @Overridepublic void stateChanged(ChangeEvent arg0) {// TODO Auto-generated method stubint index = tabbedPanel.getSelectedIndex();areaTxt.setText(introduce + introduces[index]);((JScrollPane)tabbedPanel.getComponent(index)).setViewportView(areaTxt);} }); GroupLayout layout = new GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING).addComponent(tabbedPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addContainerGap()));layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(tabbedPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addContainerGap()));pack();} private void jButton_sendActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:Ma
这篇关于java Swing之JTabbedPane用法实例 JTable用法实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!