java gui菜鸟笔记

来源:互联网 发布:人工智能机器人玩具 编辑:程序博客网 时间:2024/06/11 19:51

第一次用gui弄了一个窗口,一个背景,一个按钮,一个文本域

package JFrameTest;


import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;


import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.alibaba.fastjson.JSONObject;
import com.yld.util.YCHttpClient;


/**
 * ok的
 * @author 龙
 *
 */
public class JFrameTest implements ActionListener {
// private static Logger logger = LoggerFactory.getLogger(JFrameTest.class);
private JFrame jframe;
private JButton jbutton;
private JPanel jpanel;
private JTextArea jTextArea;
private GridBagLayout gridbag;
private GridBagConstraints constraints;
private final int width = 600;
private final int height = 400;


public JFrameTest() {
jframe = new JFrame();
jbutton = new JButton();
jTextArea = new JTextArea(4, 30);
gridbag = new GridBagLayout();
init();
}


private void init() {
jpanel = new JPanel() {
/**

*/
private static final long serialVersionUID = 1L;


@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon img = new ImageIcon("39.png");
img.setImage(img.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
img.paintIcon(this, g, 0, 0);
}
};
// 更新产品按钮显示
constraints = getGridBagConstraints(1, 2, 1, 1, 0, 0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(10, 0, 10, 0), 0, 0);


gridbag.setConstraints(jbutton, constraints);
// 文本域显示
constraints = getGridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(10, 0, 10, 0), 100, 0);


gridbag.setConstraints(jTextArea, constraints);


jpanel.setLayout(gridbag);
// 设置button按钮
jbutton.setText("更新产品");
jbutton.addActionListener(this);
// 设置文本域


jpanel.add(jbutton);
jpanel.add(jTextArea);
jpanel.setOpaque(true);
jframe.add(jpanel);


}


private static GridBagConstraints getGridBagConstraints(int gridx, int gridy, int gridwidth,
int gridheight, double weightx, double weighty, int anchor, int fill, Insets insets, int ipadx,
int ipady) {


return new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill,
insets, ipadx, ipady);
}


public void showMe() {
jframe.setSize(width, height);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
// 屏幕中间显示窗口
jframe.setLocationRelativeTo(null);
}


public static void main(String[] args) {
new JFrameTest().showMe();
}


@SuppressWarnings("unchecked")
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jbutton) {
// logger.info("button程序开始执行");
StringBuffer buffer = new StringBuffer();
buffer.append("测试程序");
// 开始发送请求给商务平台
String requesturl = "http://www.elianda.cn/openapi/api/productquery.do";
Map<String, String> responseMap = YCHttpClient.sendPostToServer(requesturl,
new HashMap<String, String>());
String responseCode = responseMap.get("responseCode");
String responseBody = responseMap.get("responseBody");
// logger.info("【与商务平台通讯,接收到的响应码:" + responseCode + "】");
// logger.info("【与商务平台通讯,接收到的响应报文是:" + responseBody + "】");
if (!responseCode.equals("200")) {
buffer.append("【与商务平台通讯失败】");
}else{
JSONObject responsebody = JSONObject.parseObject(responseBody);
String responsesuccess = responsebody.getString("success");
if (responsesuccess.equals("true")) {
// 发送微信通知成功
// logger.info("【商务平台程序处理成功】");
Map<String, Integer> map = (Map<String, Integer>) responsebody.get("o");
// logger.info("【map:" + map.toString() + "】");
buffer.append("此次一共有" + map.get("productnum") + "个产品,其中新增产品" + map.get("addproductnum")
+ "个.\r\n");
buffer.append("此次一共有" + map.get("topupnum") + "个充值产品,其中新增充值产品" + map.get("addstopupnum")
+ "个.\r\n");
buffer.append("此次一共有" + map.get("citynum") + "个城市代码,其中新增城市" + map.get("addcitynum") + "个.");
} else {
buffer.append("【商务平台程序处理失败】");
}
}

jTextArea.setText(buffer.toString());


}
}
}

0 0
原创粉丝点击