有没有可能运行gui 然后还能生成txt文本保存数据呢 有没有可能运行gui 然后还能生成txt文本保存数据呢 就是程序是做的gui,也就是gui在运行呢,也进行着操作,然后gui上的数据还能保存到本地呢有人知道吗?给讲讲呗 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 。呵呵你想什么操作保存数据呢?如果再按钮按下就看下按钮的事件,如果是鼠标的就看鼠标的事件,在相应的事件中取得对应的数据,然后用java.io下的包写入txt文件就可以了其实自己google甚至百度 都能找到你需要的东西自己琢磨下吧 你自己加进去不就知道可不可以了嘛!这里要看的是不是可不可以,可不可以自己去试,这里欢迎的是为什么这样不可以?没道理java在运行的时候不让用io,所以肯定可以操作 肯定是可以的啦看了下你发给我的程序用的是BufferedWriter输出的时候注意最后要flush()close() import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.Timer;import java.util.TimerTask;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;public class kaku extends JFrame { private static final long serialVersionUID = 1L; /** Simulation Basic Data **/ private int maximumVESSELS = 16; private int simFIN = 10000;// int simFIN = 11521 ; /** 8 Days **/ private double dtMIN = 1.0; /** minute **/ private double pi = 3.14159265358979323846; private double degToRAD = pi / 180.0; private ShipCondition[] shipSET; private Timer timer; private TimerTask task; private int clock; private MyPanel pnlMain; private JButton btnInterrupt; private JTextField textField1; private JButton pressok; private boolean movable = false; private String out; private PrintWriter output = null; public kaku() { super("操船シミュレータ"); initialize(); } private void initialize() { out = "out.txt"; try { output = new PrintWriter(new BufferedWriter(new FileWriter(out))); } catch (IOException e1) { e1.printStackTrace(); } clock = 0; shipSET = new ShipCondition[maximumVESSELS]; for (int i = 0; i < maximumVESSELS; i++) { shipSET[i] = new ShipCondition(); } for (int is = 0; is < maximumVESSELS; is++) { shipSET[is].vesselID = is; shipSET[is].metaWeightGOAL = 10.0; shipSET[is].goalSigA = 5.0; shipSET[is].goalSigB = 1.0; shipSET[is].metaWeightCONTRACT = 1.0; shipSET[is].contractSigA = 10.0; shipSET[is].contractSigB = 1.0; shipSET[is].metaWeightEXPAND = 1.0; shipSET[is].expandSigA = -10.0; // -10 shipSET[is].expandSigB = 1.0; // 1 shipSET[is].metaWeightClusterAVOID = 100.0; shipSET[is].clusterAvoidSigA = -10.0; // -10 shipSET[is].clusterAvoidSigB = 3.0; // 3 shipSET[is].decisionMD = 6.0; // 6 shipSET[is].dcpaJUDGE = 0.75; shipSET[is].headOnDEG = 50.0; shipSET[is].courseRadOLD = pi / 2; /* Dummy */ shipSET[is].speedKtOLD = 8.0; shipSET[is].courseRadNOW = pi / 2; /* Dummy */ shipSET[is].speedKtNOW = 8.0; shipSET[is].courseNaviRAD = pi / 2; /* Dummy */ shipSET[is].speedNaviKT = 8.0; shipSET[is].goalRad = pi / 2; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { shipSET[i * 3 + j + 1].xMileOLD = j * 2 + i; shipSET[i * 3 + j + 1].yMileOLD = i * 1.7320508076; shipSET[i * 3 + j + 1].xMileNOW = shipSET[i * 3 + j + 1].xMileOLD; shipSET[i * 3 + j + 1].yMileNOW = shipSET[i * 3 + j + 1].yMileOLD; } } for (int i = 3; i < 5; i++) { for (int j = 0; j < 3; j++) { shipSET[i * 3 + j + 1].xMileOLD = j * 2 + (4 - i); shipSET[i * 3 + j + 1].yMileOLD = i * 1.7320508076; shipSET[i * 3 + j + 1].xMileNOW = shipSET[i * 3 + j + 1].xMileOLD; shipSET[i * 3 + j + 1].yMileNOW = shipSET[i * 3 + j + 1].yMileOLD; } } } this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("操船シミュレータ"); this.setSize(800, 300); Container cont = this.getContentPane(); cont.setLayout(new BorderLayout()); pnlMain = new MyPanel(); pnlMain.setBackground(Color.BLACK); pnlMain.add(new JLabel("shiprad")); textField1 = new JTextField(10); pnlMain.add(textField1); pressok = new JButton("preeok"); pressok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { shipSET[8].goalRad = (180 - (Integer.parseInt(textField1 .getText().trim()))) / 180.0 * pi;// outputPos();在你想输出的地方使用这个函数 pnlMain.requestFocus(); } }); pressok.setLayout(new FlowLayout()); pressok.setBackground(Color.WHITE); cont.add(pressok, BorderLayout.PAGE_END); cont.add(pnlMain, BorderLayout.CENTER); outputPos(); } //将每个点的坐标输出到文件 private void outputPos() { for (int i = 1; i < maximumVESSELS; i++) { output.append(shipSET[i].xMileNOW + "," + shipSET[i].yMileNOW); output.println(); } output.flush(); } public void transform() { timer = new Timer(); task = new TimerTask() { public void run() { if (clock >= simFIN) { timer.cancel(); output.close(); return; } for (int givenSHIP = 0; givenSHIP < maximumVESSELS; givenSHIP++) { /*** Navigation -> Navigation (status = 0 --> 0) ***/ /********************************************************/ /***** Ship Cluster Behavior Model Calculation *****/ /********************************************************/ if (movable && givenSHIP == 0) { continue; } DecisionMakingReturn dmCoSp = DecisionMakingBody .agentDecisionMaking(shipSET, givenSHIP, dtMIN); /*** Quick Response ***/ shipSET[givenSHIP].xMileNEW = shipSET[givenSHIP].xMileNOW + dmCoSp.dmSpeedKt * dtMIN / 60.0 * Math.sin(dmCoSp.dmCourseRad); shipSET[givenSHIP].yMileNEW = shipSET[givenSHIP].yMileNOW + dmCoSp.dmSpeedKt * dtMIN / 60.0 * Math.cos(dmCoSp.dmCourseRad); shipSET[givenSHIP].courseRadNEW = dmCoSp.dmCourseRad; shipSET[givenSHIP].speedKtNEW = dmCoSp.dmSpeedKt; pnlMain.repaint((int) Math.round(shipSET[givenSHIP].xMileOLD * 10), (int) Math.round(shipSET[givenSHIP].yMileOLD * 10) + 50, 3, 3); pnlMain.repaint((int) Math.round(shipSET[givenSHIP].xMileNEW * 10), (int) Math.round(shipSET[givenSHIP].yMileNEW * 10) + 50, 3, 3); } double collisionJudgeDIST = 0.1; for (int i = 0; i < maximumVESSELS; i++) { int flagCC = 0; if (shipSET[i].vesselSTATUS == 0) { for (int j = i + 1; j < maximumVESSELS; j++) { if (shipSET[j].vesselSTATUS == 0) { double x1 = shipSET[j].xMileNEW; double y1 = shipSET[j].yMileNEW; double x2 = shipSET[j].xMileNEW; double y2 = shipSET[j].yMileNEW; Point2D p1, p2; p1 = new Point2D(x1, y1); p2 = new Point2D(x2, y2); double dist = p1.distance2D(p2); if (dist < collisionJudgeDIST) flagCC = 1; } } } } /** State Transition **/ for (int i = 0; i < maximumVESSELS; i++) { if (shipSET[i].vesselSTATUS == 0) { shipSET[i].xMileOLD = shipSET[i].xMileNOW; shipSET[i].yMileOLD = shipSET[i].yMileNOW; shipSET[i].courseRadOLD = shipSET[i].courseRadNOW; shipSET[i].speedKtOLD = shipSET[i].speedKtNOW; shipSET[i].xMileNOW = shipSET[i].xMileNEW; shipSET[i].yMileNOW = shipSET[i].yMileNEW; shipSET[i].courseRadNOW = shipSET[i].courseRadNEW; shipSET[i].speedKtNOW = shipSET[i].speedKtNEW; } } clock++; outputPos(); } }; timer.schedule(task, 0, 100); } public static void main(String[] args) { kaku ka = new kaku(); ka.setVisible(true); ka.transform(); } class MyPanel extends JPanel { private static final long serialVersionUID = 1L; public MyPanel() { addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (movable) { int code = e.getKeyCode(); switch (code) { case KeyEvent.VK_UP: // shipSET[0].yMileOLD = shipSET[0].yMileNEW; shipSET[8].yMileNEW -= 0.1; repaint((int) Math.round(shipSET[0].xMileNEW * 10), (int) Math.round(shipSET[0].yMileNEW * 10) + 50, 3, 4); break; case KeyEvent.VK_DOWN: // shipSET[0].yMileOLD = shipSET[0].yMileNEW; shipSET[8].yMileNEW += 0.1; repaint((int) Math.round(shipSET[0].xMileNEW * 10), (int) Math.round(shipSET[0].yMileNEW * 10) + 49, 3, 4); break; case KeyEvent.VK_LEFT: // shipSET[0].xMileOLD = shipSET[0].xMileNEW; shipSET[8].xMileNEW -= 0.1; repaint((int) Math.round(shipSET[0].xMileNEW * 10), (int) Math.round(shipSET[0].yMileNEW * 10) + 50, 4, 3); break; case KeyEvent.VK_RIGHT: // shipSET[0].xMileOLD = shipSET[0].xMileNEW; shipSET[8].xMileNEW += 0.1; repaint((int) Math.round(shipSET[0].xMileNEW * 10) - 1, (int) Math.round(shipSET[0].yMileNEW * 10) + 50, 4, 3); break; } } } }); } protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.GREEN); for (ShipCondition point : shipSET) { g.fillRect((int) Math.round(point.xMileNEW * 10), (int) Math .round(point.yMileNEW * 10) + 50, 3, 3); } g.setColor(Color.RED); g.fillRect((int) Math.round(shipSET[8].xMileNEW * 10), (int) Math .round(shipSET[8].yMileNEW * 10) + 50, 3, 3); } }}改成这样了,把输出的部分加到Timer中就可以了不过数据量蛮大哦我刚运行了一会儿,有快1MB的数据 请问怎样给对象动态命名啊 java的GUI设计,怎么样获得具有焦点的控件,比如说文本框!text.isFocusControl怎么不行?~~~~~~~~~~ java中的char占几个字节 有谁知道哪有比较好的java开源新闻发布系统吗? 用easyRecovery 恢复数据后不能读取,各位达人来看了!! 线程中-客户机与服务器 关于对象引用类型转换的问题。 如何获得当前活动frame? 菜鸟弱问题:如何把屏幕显示输出全生成到txt文件里去? java byte转char 中文乱码~不是byte[] 为什么用dos可以调试,用Eclipse不行呢? 关于文件的读取··求教
。呵呵
你想什么操作保存数据呢?如果再按钮按下就看下按钮的事件,如果是鼠标的就看鼠标的事件,在相应的事件中取得对应的数据,然后用java.io下的包写入txt文件就可以了其实自己google甚至百度 都能找到你需要的东西
自己琢磨下吧
没道理java在运行的时候不让用io,所以肯定可以操作
用的是BufferedWriter
输出的时候注意最后要
flush()
close()
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Timer;
import java.util.TimerTask;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;public class kaku extends JFrame {
private static final long serialVersionUID = 1L;
/** Simulation Basic Data **/
private int maximumVESSELS = 16;
private int simFIN = 10000;// int simFIN = 11521 ; /** 8 Days **/
private double dtMIN = 1.0;
/** minute **/
private double pi = 3.14159265358979323846;
private double degToRAD = pi / 180.0;
private ShipCondition[] shipSET;
private Timer timer;
private TimerTask task;
private int clock;
private MyPanel pnlMain;
private JButton btnInterrupt;
private JTextField textField1;
private JButton pressok;
private boolean movable = false;
private String out;
private PrintWriter output = null; public kaku() {
super("操船シミュレータ");
initialize();
} private void initialize() {
out = "out.txt";
try {
output = new PrintWriter(new BufferedWriter(new FileWriter(out)));
} catch (IOException e1) {
e1.printStackTrace();
}
clock = 0;
shipSET = new ShipCondition[maximumVESSELS];
for (int i = 0; i < maximumVESSELS; i++) {
shipSET[i] = new ShipCondition();
}
for (int is = 0; is < maximumVESSELS; is++) {
shipSET[is].vesselID = is;
shipSET[is].metaWeightGOAL = 10.0;
shipSET[is].goalSigA = 5.0;
shipSET[is].goalSigB = 1.0;
shipSET[is].metaWeightCONTRACT = 1.0;
shipSET[is].contractSigA = 10.0;
shipSET[is].contractSigB = 1.0;
shipSET[is].metaWeightEXPAND = 1.0;
shipSET[is].expandSigA = -10.0; // -10
shipSET[is].expandSigB = 1.0; // 1
shipSET[is].metaWeightClusterAVOID = 100.0;
shipSET[is].clusterAvoidSigA = -10.0; // -10
shipSET[is].clusterAvoidSigB = 3.0; // 3
shipSET[is].decisionMD = 6.0; // 6
shipSET[is].dcpaJUDGE = 0.75;
shipSET[is].headOnDEG = 50.0;
shipSET[is].courseRadOLD = pi / 2; /* Dummy */
shipSET[is].speedKtOLD = 8.0;
shipSET[is].courseRadNOW = pi / 2; /* Dummy */
shipSET[is].speedKtNOW = 8.0;
shipSET[is].courseNaviRAD = pi / 2; /* Dummy */
shipSET[is].speedNaviKT = 8.0;
shipSET[is].goalRad = pi / 2; for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
shipSET[i * 3 + j + 1].xMileOLD = j * 2 + i;
shipSET[i * 3 + j + 1].yMileOLD = i * 1.7320508076;
shipSET[i * 3 + j + 1].xMileNOW = shipSET[i * 3 + j + 1].xMileOLD;
shipSET[i * 3 + j + 1].yMileNOW = shipSET[i * 3 + j + 1].yMileOLD;
}
}
for (int i = 3; i < 5; i++) {
for (int j = 0; j < 3; j++) {
shipSET[i * 3 + j + 1].xMileOLD = j * 2 + (4 - i);
shipSET[i * 3 + j + 1].yMileOLD = i * 1.7320508076;
shipSET[i * 3 + j + 1].xMileNOW = shipSET[i * 3 + j + 1].xMileOLD;
shipSET[i * 3 + j + 1].yMileNOW = shipSET[i * 3 + j + 1].yMileOLD;
}
}
} this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("操船シミュレータ");
this.setSize(800, 300);
Container cont = this.getContentPane();
cont.setLayout(new BorderLayout());
pnlMain = new MyPanel();
pnlMain.setBackground(Color.BLACK);
pnlMain.add(new JLabel("shiprad"));
textField1 = new JTextField(10);
pnlMain.add(textField1);
pressok = new JButton("preeok");
pressok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shipSET[8].goalRad = (180 - (Integer.parseInt(textField1
.getText().trim()))) / 180.0 * pi;
// outputPos();在你想输出的地方使用这个函数
pnlMain.requestFocus();
}
}); pressok.setLayout(new FlowLayout());
pressok.setBackground(Color.WHITE);
cont.add(pressok, BorderLayout.PAGE_END);
cont.add(pnlMain, BorderLayout.CENTER);
outputPos();
} //将每个点的坐标输出到文件
private void outputPos() {
for (int i = 1; i < maximumVESSELS; i++) {
output.append(shipSET[i].xMileNOW + "," + shipSET[i].yMileNOW);
output.println();
}
output.flush();
} public void transform() {
timer = new Timer();
task = new TimerTask() {
public void run() {
if (clock >= simFIN) {
timer.cancel();
output.close();
return;
}
for (int givenSHIP = 0; givenSHIP < maximumVESSELS; givenSHIP++) {
/*** Navigation -> Navigation (status = 0 --> 0) ***/
/********************************************************/
/***** Ship Cluster Behavior Model Calculation *****/
/********************************************************/
if (movable && givenSHIP == 0) {
continue;
}
DecisionMakingReturn dmCoSp = DecisionMakingBody
.agentDecisionMaking(shipSET, givenSHIP, dtMIN); /*** Quick Response ***/
shipSET[givenSHIP].xMileNEW = shipSET[givenSHIP].xMileNOW
+ dmCoSp.dmSpeedKt * dtMIN / 60.0
* Math.sin(dmCoSp.dmCourseRad);
shipSET[givenSHIP].yMileNEW = shipSET[givenSHIP].yMileNOW
+ dmCoSp.dmSpeedKt * dtMIN / 60.0
* Math.cos(dmCoSp.dmCourseRad);
shipSET[givenSHIP].courseRadNEW = dmCoSp.dmCourseRad;
shipSET[givenSHIP].speedKtNEW = dmCoSp.dmSpeedKt;
pnlMain.repaint((int) Math.round(shipSET[givenSHIP].xMileOLD * 10),
(int) Math.round(shipSET[givenSHIP].yMileOLD * 10) + 50,
3, 3);
pnlMain.repaint((int) Math.round(shipSET[givenSHIP].xMileNEW * 10),
(int) Math.round(shipSET[givenSHIP].yMileNEW * 10) + 50,
3, 3);
}
double collisionJudgeDIST = 0.1;
for (int i = 0; i < maximumVESSELS; i++) {
int flagCC = 0;
if (shipSET[i].vesselSTATUS == 0) {
for (int j = i + 1; j < maximumVESSELS; j++) {
if (shipSET[j].vesselSTATUS == 0) {
double x1 = shipSET[j].xMileNEW;
double y1 = shipSET[j].yMileNEW;
double x2 = shipSET[j].xMileNEW;
double y2 = shipSET[j].yMileNEW;
Point2D p1, p2;
p1 = new Point2D(x1, y1);
p2 = new Point2D(x2, y2);
double dist = p1.distance2D(p2);
if (dist < collisionJudgeDIST)
flagCC = 1;
}
}
}
}
/** State Transition **/
for (int i = 0; i < maximumVESSELS; i++) {
if (shipSET[i].vesselSTATUS == 0) {
shipSET[i].xMileOLD = shipSET[i].xMileNOW;
shipSET[i].yMileOLD = shipSET[i].yMileNOW;
shipSET[i].courseRadOLD = shipSET[i].courseRadNOW;
shipSET[i].speedKtOLD = shipSET[i].speedKtNOW;
shipSET[i].xMileNOW = shipSET[i].xMileNEW;
shipSET[i].yMileNOW = shipSET[i].yMileNEW;
shipSET[i].courseRadNOW = shipSET[i].courseRadNEW;
shipSET[i].speedKtNOW = shipSET[i].speedKtNEW;
}
}
clock++;
outputPos();
}
};
timer.schedule(task, 0, 100);
} public static void main(String[] args) { kaku ka = new kaku();
ka.setVisible(true);
ka.transform();
} class MyPanel extends JPanel {
private static final long serialVersionUID = 1L; public MyPanel() {
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (movable) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_UP:
// shipSET[0].yMileOLD = shipSET[0].yMileNEW;
shipSET[8].yMileNEW -= 0.1;
repaint((int) Math.round(shipSET[0].xMileNEW * 10),
(int) Math.round(shipSET[0].yMileNEW * 10) + 50,
3, 4);
break;
case KeyEvent.VK_DOWN:
// shipSET[0].yMileOLD = shipSET[0].yMileNEW;
shipSET[8].yMileNEW += 0.1;
repaint((int) Math.round(shipSET[0].xMileNEW * 10),
(int) Math.round(shipSET[0].yMileNEW * 10) + 49,
3, 4);
break;
case KeyEvent.VK_LEFT:
// shipSET[0].xMileOLD = shipSET[0].xMileNEW;
shipSET[8].xMileNEW -= 0.1;
repaint((int) Math.round(shipSET[0].xMileNEW * 10),
(int) Math.round(shipSET[0].yMileNEW * 10) + 50,
4, 3);
break;
case KeyEvent.VK_RIGHT:
// shipSET[0].xMileOLD = shipSET[0].xMileNEW;
shipSET[8].xMileNEW += 0.1;
repaint((int) Math.round(shipSET[0].xMileNEW * 10) - 1,
(int) Math.round(shipSET[0].yMileNEW * 10) + 50,
4, 3);
break;
}
}
}
});
} protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
for (ShipCondition point : shipSET) {
g.fillRect((int) Math.round(point.xMileNEW * 10), (int) Math
.round(point.yMileNEW * 10) + 50, 3, 3);
}
g.setColor(Color.RED);
g.fillRect((int) Math.round(shipSET[8].xMileNEW * 10), (int) Math
.round(shipSET[8].yMileNEW * 10) + 50, 3, 3);
}
}
}改成这样了,把输出的部分加到Timer中就可以了
不过数据量蛮大哦
我刚运行了一会儿,有快1MB的数据