最近在学习Java的图形窗口制作,自己写了一个简易的Modbus报文自动生成的程序,但是这个JDialog弹窗一直不对,查资料思考了许久也找不出原因,烦请大家帮忙给指点一下!
JDialog的代码如下:
public static void Creat_JDialog()
{
jd.setTitle("警告");
jd.setLayout(null);
jd.setSize(250,150);
jd.setLocationRelativeTo(jf);
JLabel jl1=new JLabel(parameter_in[2]);
jl1.setBounds(25,20,200,20);
container2.add(jl1);
JButton jb=new JButton("确认");
jb.setBounds(95,60,60,20);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
jl1.setText("");
jf.setEnabled(true);
jd.dispose();
}
});
container2.add(jb);
jd.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
jd.setVisible(true);
}
调用该方法的代码如下:
if(parameter_in[2]=="false")
{
jtf9.setText(parameter_in[0]);
jtf10.setText(parameter_in[1]);
}
else
{
jf.setEnabled(false);
Image.Creat_JDialog();
}
想实现的效果就是当输入的时间没有越限时,自动生成对应的Modbus报文,当输入的时间越限时,弹窗提示越限的部分。
第一次弹窗结果如下:
第二次弹窗结果如下:
修改后的完整代码如下:
class Image {
public static JFrame jf = new JFrame();
public static Container container1 = jf.getContentPane();
public static JDialog jd = new JDialog();
public static Container container2 = jd.getContentPane();
public static String[] parameter_out = new String[8];
public static String[] parameter_in = new String[3];
private static JLabel jl1 = new JLabel();
public Image() {
jd.setTitle("警告");
jd.setLayout(null);
jd.setSize(250, 150);
jd.setLocationRelativeTo(jf);
// JLabel jl1 = new JLabel();
jl1.setBounds(25, 20, 200, 20);
container2.add(jl1);
JButton jb = new JButton("确认");
jb.setBounds(95, 60, 60, 20);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jf.setEnabled(true);
jd.setVisible(false);
}
});
container2.add(jb);
jd.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
}
//新建窗口容器
public static void Creat_JFrame(String title) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = toolkit.getScreenSize();
int screen_width = dimension.width;
int screen_height = dimension.height;
jf.setTitle(title);
int jf_width = 500;
int jf_height = 300;
jf.setLayout(null);
jf.setBounds((screen_width - jf_width) / 2, (screen_height - jf_height) / 2, jf_width, jf_height);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
//新建弹窗容器
public static void Creat_JDialog() {
jl1.setText(parameter_in[2]);
jd.setVisible(true);
}
//画面显示
public void Image_Show() {
Image.Creat_JFrame("报文生成器");
JLabel jl1 = new JLabel("年");
jl1.setBounds(60, 10, 20, 20);
container1.add(jl1);
JTextField jtf1 = new JTextField();
jtf1.setBounds(30, 30, 80, 20);
container1.add(jtf1);
JLabel jl2 = new JLabel("月");
jl2.setBounds(180, 10, 20, 20);
container1.add(jl2);
JTextField jtf2 = new JTextField();
jtf2.setBounds(150, 30, 80, 20);
container1.add(jtf2);
JLabel jl3 = new JLabel("日");
jl3.setBounds(300, 10, 20, 20);
container1.add(jl3);
JTextField jtf3 = new JTextField();
jtf3.setBounds(270, 30, 80, 20);
container1.add(jtf3);
JLabel jl4 = new JLabel("时");
jl4.setBounds(420, 10, 20, 20);
container1.add(jl4);
JTextField jtf4 = new JTextField();
jtf4.setBounds(390, 30, 80, 20);
container1.add(jtf4);
JLabel jl5 = new JLabel("分");
jl5.setBounds(60, 60, 20, 20);
container1.add(jl5);
JTextField jtf5 = new JTextField();
jtf5.setBounds(30, 80, 80, 20);
container1.add(jtf5);
JLabel jl6 = new JLabel("秒");
jl6.setBounds(180, 60, 20, 20);
container1.add(jl6);
JTextField jtf6 = new JTextField();
jtf6.setBounds(150, 80, 80, 20);
container1.add(jtf6);
JLabel jl7 = new JLabel("毫秒");
jl7.setBounds(295, 60, 30, 20);
container1.add(jl7);
JTextField jtf7 = new JTextField();
jtf7.setBounds(270, 80, 80, 20);
container1.add(jtf7);
JLabel jl8 = new JLabel("转出地址");
jl8.setBounds(405, 60, 50, 20);
container1.add(jl8);
JTextField jtf8 = new JTextField();
jtf8.setBounds(390, 80, 80, 20);
container1.add(jtf8);
JLabel jl9 = new JLabel("输入的时间是:");
jl9.setBounds(30, 130, 90, 20);
container1.add(jl9);
JTextField jtf9 = new JTextField();
jtf9.setEditable(false);
jtf9.setBounds(30, 150, 220, 20);
container1.add(jtf9);
JLabel jl10 = new JLabel("对应的报文是:");
jl10.setBounds(30, 180, 90, 20);
container1.add(jl10);
JTextField jtf10 = new JTextField();
jtf10.setEditable(false);
jtf10.setBounds(30, 200, 440, 20);
container1.add(jtf10);
JButton jb = new JButton("生成");
jb.setBounds(400, 110, 60, 20);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
parameter_out[0] = jtf1.getText();
parameter_out[1] = jtf2.getText();
parameter_out[2] = jtf3.getText();
parameter_out[3] = jtf4.getText();
parameter_out[4] = jtf5.getText();
parameter_out[5] = jtf6.getText();
parameter_out[6] = jtf7.getText();
parameter_out[7] = jtf8.getText();
parameter_in = MODBUS_TimeSet.modbus_TimeSet(parameter_out);
if (parameter_in[2] == "false") {
jtf9.setText(parameter_in[0]);
jtf10.setText(parameter_in[1]);
} else {
jf.setEnabled(false);
Image.Creat_JDialog();
}
}
});
container1.add(jb);
jf.setVisible(true);
}
}
class MODBUS_TimeSet {
public static String[] modbus_TimeSet(String[] middle_in) {
int year_dec, month_dec, day_dec, hour_dec, minute_dec, second_dec, millisecond_dec;
StringBuffer month_dec_show = new StringBuffer();
StringBuffer day_dec_show = new StringBuffer();
StringBuffer hour_dec_show = new StringBuffer();
StringBuffer minute_dec_show = new StringBuffer();
StringBuffer second_dec_show = new StringBuffer();
StringBuffer millisecond_dec_show = new StringBuffer();
StringBuffer addr = new StringBuffer();
int[] month_day = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String[] middle_out = new String[10];
year_dec = Integer.parseInt(middle_in[0]);
if ((year_dec <= 0) || (year_dec >= 65536)) {
middle_out[2] = "年份输入越限,请重新输入!";
return middle_out;
} else {
}
StringBuffer year_hex = new StringBuffer(Integer.toHexString(year_dec).toUpperCase());
if (year_hex.length() % 2 != 0) {
year_hex = year_hex.insert(0, "0");
} else {
}
if (year_hex.length() != 4) {
year_hex = year_hex.insert(0, "00");
} else {
}
year_hex = year_hex.insert(2, " ");
year_hex = year_hex.append(" ");
//根据年份确定对应月份的日期,用于判断后面输入的日期是否越限
if (((year_dec % 4 == 0) && (year_dec % 100 != 0)) || (year_dec % 400 == 0)) {
month_day[1] = 29;
} else {
}
month_dec = Integer.parseInt(middle_in[1]);
if ((month_dec <= 0) || (month_dec >= 13)) {
middle_out[2] = "月份输入越限,请重新输入!";
return middle_out;
} else {
}
month_dec_show = month_dec_show.append(month_dec);
if (month_dec_show.length() == 1) {
month_dec_show = month_dec_show.insert(0, "0");
} else {
}
StringBuffer month_hex = new StringBuffer(Integer.toHexString(month_dec).toUpperCase());
month_hex = month_hex.insert(0, "0");
month_hex = month_hex.append(" ");
day_dec = Integer.parseInt(middle_in[2]);
if ((day_dec <= 0) || day_dec > month_day[month_dec - 1]) {
middle_out[2] = "日期输入越限,请重新输入!";
return middle_out;
} else {
}
day_dec_show = day_dec_show.append(day_dec);
if (day_dec_show.length() == 1) {
day_dec_show = day_dec_show.insert(0, "0");
} else {
}
StringBuffer day_hex = new StringBuffer(Integer.toHexString(day_dec).toUpperCase());
if (day_hex.length() != 2) {
day_hex = day_hex.insert(0, "0");
} else {
}
day_hex = day_hex.append(" ");
hour_dec = Integer.parseInt(middle_in[3]);
if ((hour_dec < 0) || (hour_dec >= 24)) {
middle_out[2] = "小时输入越限,请重新输入!";
return middle_out;
} else {
}
hour_dec_show = hour_dec_show.append(hour_dec);
if (hour_dec_show.length() == 1) {
hour_dec_show = hour_dec_show.insert(0, "0");
} else {
}
StringBuffer hour_hex = new StringBuffer(Integer.toHexString(hour_dec).toUpperCase());
if (hour_hex.length() != 2) {
hour_hex = hour_hex.insert(0, "0");
} else {
}
hour_hex = hour_hex.append(" ");
minute_dec = Integer.parseInt(middle_in[4]);
if ((minute_dec < 0) || (minute_dec >= 60)) {
middle_out[2] = "分钟输入越限,请重新输入!";
return middle_out;
} else {
}
minute_dec_show = minute_dec_show.append(minute_dec);
if (minute_dec_show.length() == 1) {
minute_dec_show = minute_dec_show.insert(0, "0");
} else {
}
StringBuffer minute_hex = new StringBuffer(Integer.toHexString(minute_dec).toUpperCase());
if (minute_hex.length() != 2) {
minute_hex = minute_hex.insert(0, "0");
} else {
}
minute_hex = minute_hex.append(" ");
second_dec = Integer.parseInt(middle_in[5]);
if ((second_dec < 0) || (second_dec >= 60)) {
middle_out[2] = "秒输入越限,请重新输入!";
return middle_out;
} else {
}
second_dec_show = second_dec_show.append(second_dec);
if (second_dec_show.length() == 1) {
second_dec_show = second_dec_show.insert(0, "0");
} else {
}
millisecond_dec = Integer.parseInt(middle_in[6]);
if ((millisecond_dec < 0) || (millisecond_dec >= 1000)) {
middle_out[8] = "毫秒输入越限,请重新输入!";
return middle_out;
} else {
}
millisecond_dec_show = millisecond_dec_show.append(millisecond_dec);
if (millisecond_dec_show.length() == 1) {
millisecond_dec_show = millisecond_dec_show.insert(0, "00");
} else if (millisecond_dec_show.length() == 2) {
millisecond_dec_show = millisecond_dec_show.insert(0, "0");
} else {
}
StringBuffer second_hex = new StringBuffer(Integer.toHexString(second_dec * 1000 + millisecond_dec).toUpperCase());
if (second_hex.length() % 2 != 0) {
second_hex = second_hex.insert(0, "0");
} else {
}
if (second_hex.length() != 4) {
second_hex = second_hex.insert(0, "00");
} else {
}
second_hex = second_hex.insert(2, " ");
if ((Integer.parseInt(middle_in[7], 16) <= 0) || (Integer.parseInt(middle_in[7], 16) >= 256)) {
middle_out[9] = "转出地址输入越限,请重新输入!";
return middle_out;
} else {
}
addr = addr.append(middle_in[7].toUpperCase());
if (addr.length() != 2) {
addr = addr.insert(0, "0");
} else {
}
addr = addr.append(" ");
String middle_out_dec = new String(year_dec + "-" + month_dec_show + "-" + day_dec_show + "\t" + hour_dec_show + ":" + minute_dec_show + ":" + second_dec_show + ":" + millisecond_dec_show);
String middle_out_hex = new String("00 C5 00 00 00 0F " + addr + "10 1F 40 00 04 08 " + year_hex + month_hex + day_hex + hour_hex + minute_hex + second_hex);
middle_out[0] = middle_out_dec;
middle_out[1] = middle_out_hex;
middle_out[2] = "false";
return middle_out;
}
}
public class Main {
public static void main(String[] args) {
Image image = new Image();
image.Image_Show();
}
}
调试了很久,期待额外加个鸡腿
完整代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Image
{
public static JFrame jf=new JFrame();
public static Container container1=jf.getContentPane();
public static JDialog jd=new JDialog();
public static Container container2=jd.getContentPane();
public static String[] parameter_out=new String[8];
public static String[] parameter_in=new String[3];
//新建窗口容器
public static void Creat_JFrame(String title)
{
Toolkit toolkit=Toolkit.getDefaultToolkit();
Dimension dimension=toolkit.getScreenSize();
int screen_width=dimension.width;
int screen_height=dimension.height;
jf.setTitle(title);
int jf_width=500;
int jf_height=300;
jf.setLayout(null);
jf.setBounds((screen_width-jf_width)/2,(screen_height-jf_height)/2,jf_width,jf_height);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
//新建弹窗容器
public static void Creat_JDialog()
{
jd.setTitle("警告");
jd.setLayout(null);
jd.setSize(250,150);
jd.setLocationRelativeTo(jf);
JLabel jl1=new JLabel(parameter_in[2]);
jl1.setBounds(25,20,200,20);
container2.add(jl1);
JButton jb=new JButton("确认");
jb.setBounds(95,60,60,20);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
jl1.setText("");
jf.setEnabled(true);
jd.dispose();
}
});
container2.add(jb);
jd.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
jd.setVisible(true);
}
//画面显示
public void Image_Show()
{
Image.Creat_JFrame("报文生成器");
JLabel jl1=new JLabel("年");
jl1.setBounds(60,10,20,20);
container1.add(jl1);
JTextField jtf1=new JTextField();
jtf1.setBounds(30,30,80,20);
container1.add(jtf1);
JLabel jl2=new JLabel("月");
jl2.setBounds(180,10,20,20);
container1.add(jl2);
JTextField jtf2=new JTextField();
jtf2.setBounds(150,30,80,20);
container1.add(jtf2);
JLabel jl3=new JLabel("日");
jl3.setBounds(300,10,20,20);
container1.add(jl3);
JTextField jtf3=new JTextField();
jtf3.setBounds(270,30,80,20);
container1.add(jtf3);
JLabel jl4=new JLabel("时");
jl4.setBounds(420,10,20,20);
container1.add(jl4);
JTextField jtf4=new JTextField();
jtf4.setBounds(390,30,80,20);
container1.add(jtf4);
JLabel jl5=new JLabel("分");
jl5.setBounds(60,60,20,20);
container1.add(jl5);
JTextField jtf5=new JTextField();
jtf5.setBounds(30,80,80,20);
container1.add(jtf5);
JLabel jl6=new JLabel("秒");
jl6.setBounds(180,60,20,20);
container1.add(jl6);
JTextField jtf6=new JTextField();
jtf6.setBounds(150,80,80,20);
container1.add(jtf6);
JLabel jl7=new JLabel("毫秒");
jl7.setBounds(295,60,30,20);
container1.add(jl7);
JTextField jtf7=new JTextField();
jtf7.setBounds(270,80,80,20);
container1.add(jtf7);
JLabel jl8=new JLabel("转出地址");
jl8.setBounds(405,60,50,20);
container1.add(jl8);
JTextField jtf8=new JTextField();
jtf8.setBounds(390,80,80,20);
container1.add(jtf8);
JLabel jl9=new JLabel("输入的时间是:");
jl9.setBounds(30,130,90,20);
container1.add(jl9);
JTextField jtf9=new JTextField();
jtf9.setEditable(false);
jtf9.setBounds(30,150,220,20);
container1.add(jtf9);
JLabel jl10=new JLabel("对应的报文是:");
jl10.setBounds(30,180,90,20);
container1.add(jl10);
JTextField jtf10=new JTextField();
jtf10.setEditable(false);
jtf10.setBounds(30,200,440,20);
container1.add(jtf10);
JButton jb=new JButton("生成");
jb.setBounds(400,110,60,20);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
parameter_out[0]=jtf1.getText();
parameter_out[1]=jtf2.getText();
parameter_out[2]=jtf3.getText();
parameter_out[3]=jtf4.getText();
parameter_out[4]=jtf5.getText();
parameter_out[5]=jtf6.getText();
parameter_out[6]=jtf7.getText();
parameter_out[7]=jtf8.getText();
parameter_in=MODBUS_TimeSet.modbus_TimeSet(parameter_out);
if(parameter_in[2]=="false")
{
jtf9.setText(parameter_in[0]);
jtf10.setText(parameter_in[1]);
}
else
{
jf.setEnabled(false);
Image.Creat_JDialog();
}
}
});
container1.add(jb);
jf.setVisible(true);
}
}
class MODBUS_TimeSet
{
public static String[] modbus_TimeSet(String[] middle_in)
{
int year_dec,month_dec,day_dec,hour_dec,minute_dec,second_dec,millisecond_dec;
StringBuffer month_dec_show=new StringBuffer();
StringBuffer day_dec_show=new StringBuffer();
StringBuffer hour_dec_show=new StringBuffer();
StringBuffer minute_dec_show=new StringBuffer();
StringBuffer second_dec_show=new StringBuffer();
StringBuffer millisecond_dec_show=new StringBuffer();
StringBuffer addr=new StringBuffer();
int[] month_day= {31,28,31,30,31,30,31,31,30,31,30,31};
String[] middle_out=new String[10];
year_dec=Integer.parseInt(middle_in[0]);
if((year_dec<=0)||(year_dec>=65536))
{
middle_out[2]="年份输入越限,请重新输入!";
return middle_out;
}
else
{}
StringBuffer year_hex=new StringBuffer(Integer.toHexString(year_dec).toUpperCase());
if(year_hex.length()%2!=0)
{
year_hex=year_hex.insert(0,"0");
}
else
{}
if(year_hex.length()!=4)
{
year_hex=year_hex.insert(0,"00");
}
else
{}
year_hex=year_hex.insert(2," ");
year_hex=year_hex.append(" ");
//根据年份确定对应月份的日期,用于判断后面输入的日期是否越限
if(((year_dec%4==0)&&(year_dec%100!=0))||(year_dec%400==0))
{
month_day[1]=29;
}
else
{}
month_dec=Integer.parseInt(middle_in[1]);
if((month_dec<=0)||(month_dec>=13))
{
middle_out[2]="月份输入越限,请重新输入!";
return middle_out;
}
else
{}
month_dec_show=month_dec_show.append(month_dec);
if(month_dec_show.length()==1)
{
month_dec_show=month_dec_show.insert(0,"0");
}
else
{}
StringBuffer month_hex=new StringBuffer(Integer.toHexString(month_dec).toUpperCase());
month_hex=month_hex.insert(0,"0");
month_hex=month_hex.append(" ");
day_dec=Integer.parseInt(middle_in[2]);
if((day_dec<=0)||day_dec>month_day[month_dec-1])
{
middle_out[2]="日期输入越限,请重新输入!";
return middle_out;
}
else
{}
day_dec_show=day_dec_show.append(day_dec);
if(day_dec_show.length()==1)
{
day_dec_show=day_dec_show.insert(0,"0");
}
else
{}
StringBuffer day_hex=new StringBuffer(Integer.toHexString(day_dec).toUpperCase());
if(day_hex.length()!=2)
{
day_hex=day_hex.insert(0,"0");
}
else
{}
day_hex=day_hex.append(" ");
hour_dec=Integer.parseInt(middle_in[3]);
if((hour_dec<0)||(hour_dec>=24))
{
middle_out[2]="小时输入越限,请重新输入!";
return middle_out;
}
else
{}
hour_dec_show=hour_dec_show.append(hour_dec);
if(hour_dec_show.length()==1)
{
hour_dec_show=hour_dec_show.insert(0,"0");
}
else
{}
StringBuffer hour_hex=new StringBuffer(Integer.toHexString(hour_dec).toUpperCase());
if(hour_hex.length()!=2)
{
hour_hex=hour_hex.insert(0,"0");
}
else
{}
hour_hex=hour_hex.append(" ");
minute_dec=Integer.parseInt(middle_in[4]);
if((minute_dec<0)||(minute_dec>=60))
{
middle_out[2]="分钟输入越限,请重新输入!";
return middle_out;
}
else
{}
minute_dec_show=minute_dec_show.append(minute_dec);
if(minute_dec_show.length()==1)
{
minute_dec_show=minute_dec_show.insert(0,"0");
}
else
{}
StringBuffer minute_hex=new StringBuffer(Integer.toHexString(minute_dec).toUpperCase());
if(minute_hex.length()!=2)
{
minute_hex=minute_hex.insert(0,"0");
}
else
{}
minute_hex=minute_hex.append(" ");
second_dec=Integer.parseInt(middle_in[5]);
if((second_dec<0)||(second_dec>=60))
{
middle_out[2]="秒输入越限,请重新输入!";
return middle_out;
}
else
{}
second_dec_show=second_dec_show.append(second_dec);
if(second_dec_show.length()==1)
{
second_dec_show=second_dec_show.insert(0,"0");
}
else
{}
millisecond_dec=Integer.parseInt(middle_in[6]);
if((millisecond_dec<0)||(millisecond_dec>=1000))
{
middle_out[8]="毫秒输入越限,请重新输入!";
return middle_out;
}
else
{}
millisecond_dec_show=millisecond_dec_show.append(millisecond_dec);
if(millisecond_dec_show.length()==1)
{
millisecond_dec_show=millisecond_dec_show.insert(0,"00");
}
else if(millisecond_dec_show.length()==2)
{
millisecond_dec_show=millisecond_dec_show.insert(0,"0");
}
else
{}
StringBuffer second_hex=new StringBuffer(Integer.toHexString(second_dec*1000+millisecond_dec).toUpperCase());
if(second_hex.length()%2!=0)
{
second_hex=second_hex.insert(0,"0");
}
else
{}
if(second_hex.length()!=4)
{
second_hex=second_hex.insert(0,"00");
}
else
{}
second_hex=second_hex.insert(2," ");
if((Integer.parseInt(middle_in[7],16)<=0)||(Integer.parseInt(middle_in[7],16)>=256))
{
middle_out[9]="转出地址输入越限,请重新输入!";
return middle_out;
}
else
{}
addr=addr.append(middle_in[7].toUpperCase());
if(addr.length()!=2)
{
addr=addr.insert(0,"0");
}
else
{}
addr=addr.append(" ");
String middle_out_dec=new String(year_dec+"-"+month_dec_show+"-"+day_dec_show+"\t"+hour_dec_show+":"+minute_dec_show+":"+second_dec_show+":"+millisecond_dec_show);
String middle_out_hex=new String("00 C5 00 00 00 0F "+addr+"10 1F 40 00 04 08 "+year_hex+month_hex+day_hex+hour_hex+minute_hex+second_hex);
middle_out[0]=middle_out_dec;
middle_out[1]=middle_out_hex;
middle_out[2]="false";
return middle_out;
}
}
public class Main
{
public static void main(String[] args)
{
Image image = new Image();
image.Image_Show();
}
}
```