Java swing 代码例子
package com;
import java.awt.Button;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
/*
* 连接设备的JPanel类
*/
public class Monkey_run {
MonkeySql monkeySql = new MonkeySql();
public Monkey_run() {
JFrame jFrame= new JFrame("monkey可视化操作工具");
Container container = jFrame.getContentPane();
container.setLayout(null);
try {
container.add(new P1().P1_Connect_the_device());
container.add(new P2_log_Detail_level().P2_log_Detail_level());
container.add(new P3_delay().P3_delay());
container.add(new P4_Constraint_package().P4_Constraint_package());
container.add(new P5_Run_times().P5_Run_times());
container.add(new P6_log_save().P6_log_save());
container.add(new P7_Start_and_finish().P7_Start_and_finish());
container.add(P9_Chart_switch());
container.add(new P10_getPackage().P10_getPackage());
jFrame.setVisible(true);
jFrame.setSize(800, 800);
jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}catch(Exception e) {
e.printStackTrace();
}
}
public JPanel P9_Chart_switch() throws InterruptedException {
JPanel p9 = new JPanel();
p9.setBorder(BorderFactory.createTitledBorder("图表开关"));
p9.setBounds(15, 700, 200, 50);
JButton jButton = new JButton("log");
jButton.setBounds(15, 30, 50, 50);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
new P8("ID").setVisible(true);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
p9.add(jButton);
return p9;
}
public static void main(String [] agrs) {
new Monkey_run();
}
}
package com;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class P1 extends MonkeySql{
MonkeySql monkey_data = new MonkeySql();
public JPanel P1_Connect_the_device(){
StringBuffer stringBuffer = new StringBuffer();
JPanel p1 = new JPanel();
p1.setLayout(null);
p1.setBorder(BorderFactory.createTitledBorder("连接设备"));
p1.setBounds(15, 1, 750, 70);
JTextField jTextField = new JTextField(1);
jTextField.setBounds(30, 25, 300, 30);
JRadioButton Write = new JRadioButton("写入");
Write.setBounds(350, 30, 70, 25);
Write.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String aString = jTextField.getText();
monkey_data.setmonkey_array("-s "+aString, 1);
}
});
JRadioButton Clear = new JRadioButton("清除");
Clear.setBounds(420, 30, 70, 25);
Clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jTextField.setText("");
jTextField.requestFocus();
monkey_data.setmonkey_array("", 1);
}
});
Button Device_ID = new Button("Device_ID");
JTextArea jTextArea = new JTextArea();
jTextArea.setLineWrap(true);
Device_ID.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
Process process=Runtime.getRuntime().exec("cmd /c adb devices");
InputStream pInputStream =process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(pInputStream));
StringBuffer sBuffer = new StringBuffer();
String ID ="";
while((ID = br.readLine()) != null ) {
if (ID.endsWith("device")) {
sBuffer.append(ID);
int size=sBuffer.lastIndexOf("ice");
sBuffer.insert(size+3, "\n");
sBuffer.replace(size-3, size+3, "");
jTextArea.setText(sBuffer.toString());
}
}
process.waitFor();
br.close();
pInputStream.close();
}catch(IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
Device_ID.setBounds(480, 15, 70, 50);
jTextArea.setBounds(560, 15, 180, 50);
p1.add(jTextArea);
p1.add(Device_ID);
p1.add(Write);
p1.add(Clear);
p1.add(jTextField);
ButtonGroup group = new ButtonGroup();
group.add(Write);
group.add(Clear);
return p1;
}
}
package com;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class P2_log_Detail_level {
MonkeySql monkeySql = new MonkeySql();
public JPanel P2_log_Detail_level() {
JPanel p2 = new JPanel();
p2.setBorder(BorderFactory.createTitledBorder("log详细级别"));
p2.setBounds(15, 72, 350, 70);
p2.setLayout(null);
String value [] = {"空","一级","二级","三级"};
JComboBox<String> group = new JComboBox<>(value);
group.setBounds(50, 25, 200, 35);
group.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(group.getSelectedItem().equals("空")) {
monkeySql.setmonkey_array("", 5);
}else if(group.getSelectedItem().equals("一级")){
System.out.println("一级");
monkeySql.setmonkey_array("-v", 5);
}else if (group.getSelectedItem().equals("二级")) {
System.out.println("二级");
monkeySql.setmonkey_array("-v -v", 5);
}else if(group.getSelectedItem().equals("三级")) {
System.out.println("三级");
monkeySql.setmonkey_array("-v -v -v", 5);
}
}
});
/*JRadioButton v1 = new JRadioButton("1级");
JRadioButton v2 = new JRadioButton("2级");
JRadioButton v3 = new JRadioButton("3级");
JRadioButton cancel = new JRadioButton("取消");
v1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
monkeySql.setmonkey_array("-v", 5);
}
});
v2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
monkeySql.setmonkey_array("-v -v", 5);
}
});
v3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
monkeySql.setmonkey_array("-v -v -v", 5);
}
});
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
monkeySql.setmonkey_array("", 3);
}
});
ButtonGroup group = new ButtonGroup();
group.add(v1);
group.add(v2);
group.add(v3);
group.add(cancel);
*/
//p2.add(v1);
//p2.add(v2);
//p2.add(v3);
//p2.add(cancel);
p2.add(group);
return p2;
}
}
package com;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class P3_delay {
MonkeySql monkeySql = new MonkeySql();
public JPanel P3_delay() {
JPanel p3 = new JPanel();
p3.setLayout(null);
p3.setBorder(BorderFactory.createTitledBorder("执行间隔时间"));
p3.setBounds(380, 72, 385, 70);
JTextField jTextField = new JTextField(1);
jTextField.setBounds(10, 25, 150, 30);
JRadioButton Write = new JRadioButton("写入");
Write.setBounds(165, 15, 55, 50);
JRadioButton Clear = new JRadioButton("清除");
Clear.setBounds(216, 15, 55, 50);
Write.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String Value = jTextField.getText();
monkeySql.setmonkey_array(" --throttle "+Value, 6);
}
});
Clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jTextField.setText("");
monkeySql.setmonkey_array("", 6);
}
});
ButtonGroup group = new ButtonGroup();
group.add(Write);
group.add(Clear);
p3.add(Write);
p3.add(Clear);
p3.add(jTextField);
return p3;
}
}
package com;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.net.ssl.SSLException;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.xml.xpath.XPath;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.DataUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
public class P8 extends JPanel{
private static Thread thread;
public P8(String AA) throws InterruptedException {
JFrame jFrame = new JFrame(AA);
Container container = jFrame.getContentPane();
container.add(p8_log());
jFrame.setVisible(true);
jFrame.setSize(1000, 600);
jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public ChartPanel p8_log() throws InterruptedException {
ChartPanel framel;
JFreeChart jfreechart = ChartFactory.createLineChart("ID",
"Type",
"Value",
createDataset(),
PlotOrientation.VERTICAL,
true,
true,
false);
framel= new ChartPanel(jfreechart,true);
framel.setBounds(15, 15, 750, 300);;
jfreechart.setBackgroundPaint(Color.white);
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setRangeGridlinePaint(Color.white);
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
//获得renderer 注意这里是下嗍造型到lineandshaperenderer!!
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
lineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {6F, 6F}, 0.0F)); //定义series为”Second”的(即series2)点之间的连线
lineandshaperenderer.setSeriesStroke(2, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {2.0F, 6F}, 0.0F)); //定义series为”Third”的(即series3)点之间的连线
return framel;
}
public static CategoryDataset createDataset() throws InterruptedException {
DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();
thread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
try {
Process process=Runtime.getRuntime().exec("cmd /c adb shell dumpsys cpuinfo");
InputStream pInputStream =process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(pInputStream));
StringBuffer sBuffer = new StringBuffer();
String ID ="";
while((ID = br.readLine()) != null ) {
if (ID.endsWith("TOTAL")) {
sBuffer.append(ID);
int size=sBuffer.lastIndexOf("ice");
sBuffer.insert(size+3, "\n");
sBuffer.replace(size-3, size+3, "");
}
}
}catch(IOException e1) {
e1.printStackTrace();
}
Date date = new Date();
String hour =String.format("%tH", date);
String minute =String.format("%tM", date);
String second =String.format("%tS", date);
String time1 = hour+minute+second;
sleep(2000);
Random random = new Random();
int randNum = random.nextInt(10);
defaultCategoryDataset.addValue(randNum, "A",time1);
sleep(1000);
int randNum2 = random.nextInt(10);
defaultCategoryDataset.addValue(randNum2, "B",time1);
sleep(1000);
int randNum3 = random.nextInt(10);
defaultCategoryDataset.addValue(randNum3, "C",time1);
}
}
});
thread.start();
return defaultCategoryDataset;
}
public static void sleep(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}