JDialog FRAME = new JDialog();//构造一个新的JFrame,作为新窗口。 FRAME.setBounds(new Rectangle((int) jf.getBounds().getX() + 50,(int) jf.getBounds().getY() + 50, (int) jf.getBounds().getWidth(), (int) jf.getBounds().getHeight())); FRAME.add(jsp,BorderLayout.NORTH); try { out = new BufferedWriter(new FileWriter("销售总额.txt",true)); ForJCheckBoxnum(jp9,out,FRAME); } catch (IOException e1) { e1.printStackTrace(); } FRAME.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);// 设置模式类型。 FRAME.setVisible(true); B = true; ((DefaultTableModel)jt.getModel()).getDataVector().clear();// 清空前一人的菜单
时间: 2024-04-18 14:33:24 浏览: 23
这段代码创建了一个新的JDialog作为新窗口,并设置了它的位置和大小。然后在窗口中添加了一个JScrollPane,并通过文件写入流将数据写入到"销售总额.txt"文件中。接着设置了窗口的模式类型为APPLICATION_MODAL,使其成为一个模态对话框。最后设置窗口可见,并清空了一个名为jt的JTable的数据。
相关问题
为什么以下代码构件JButton,JLabel不显示package com.experiment; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * 使用JDialog对话框显示问候语 */ @SuppressWarnings("all") public class demo14 { public static void main(String[] args) { JFrame frame = new JFrame("问候"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(500, 300); frame.setLocationRelativeTo(null); JLabel label = new JLabel("请输入您的姓名:"); JTextField jTextField = new JTextField(15); JPanel panel = new JPanel(); frame.setContentPane(panel); JButton button = new JButton("确定"); panel.add(label); panel.add(jTextField); panel.add(button); } }
这是因为您没有将按钮和标签添加到 JFrame 窗口中。需要使用 frame.add() 方法将它们添加到窗口中。同时,需要将 setVisible() 方法放在最后,因为只有在添加了所有组件后,才应该将窗口显示出来。以下是修改后的代码:
package com.experiment;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 使用JDialog对话框显示问候语
*/
@SuppressWarnings("all")
public class demo14 {
public static void main(String[] args) {
JFrame frame = new JFrame("问候");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
JLabel label = new JLabel("请输入您的姓名:");
JTextField jTextField = new JTextField(15);
JPanel panel = new JPanel();
JButton button = new JButton("确定");
panel.add(label);
panel.add(jTextField);
panel.add(button);
frame.add(panel);
frame.setVisible(true);
}
}
改进以下代码,让错误出现三次弹出冻结信息并关闭窗体。denglu.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int count = 0; String inputzh = zhanghu.getText(); boolean bl = true; try { new Mysql(); bl = Mysql.jujle(inputzh); } catch (ClassNotFoundException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } catch (SQLException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } System.out.println(bl); if (bl) { try { System.out.println(inputzh); new GUI2(inputzh); jf.dispose(); } catch (Exception e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } } else { ImageIcon titleIcon = new ImageIcon("F:\\ATM.png"); JDialog jDialog = new JDialog(jf, "错误", true); jDialog.setSize(400, 200); jDialog.setLocation(575, 400); JTextField jTextField = new JTextField("您的账号和密码输入有误!请重新输入。"); jTextField.setHorizontalAlignment(JTextField.CENTER); jTextField.setEditable(false); jTextField.setSelectedTextColor(Color.black); jDialog.add(jTextField); jDialog.setIconImage(titleIcon.getImage()); jTextField.setVisible(true); jDialog.setVisible(true); count++; if (count == 3) { ImageIcon titleIcon1 = new ImageIcon("F:\\ATM.png"); JDialog jDialog1 = new JDialog(jf, "错误", true); jDialog.setSize(400, 200); jDialog.setLocation(575, 400); JTextField jTextField1 = new JTextField("您的账号已冻结!请联系人工处理。"); jTextField.setHorizontalAlignment(JTextField.CENTER); jTextField.setEditable(false); jTextField.setSelectedTextColor(Color.black); jDialog.add(jTextField1); jDialog.setIconImage(titleIcon1.getImage()); jTextField.setVisible(true); jDialog.setVisible(true); jf.setDefaultCloseOperation(3); System.exit(0); } else { jpl.repaint(); } } } });
改进后的代码如下:
```java
denglu.addActionListener(new ActionListener() {
private int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
String inputzh = zhanghu.getText();
boolean bl = true;
try {
new Mysql();
bl = Mysql.jujle(inputzh);
} catch (ClassNotFoundException | SQLException e1) {
e1.printStackTrace();
}
if (bl) {
try {
new GUI2(inputzh);
jf.dispose();
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
count++;
if (count == 3) {
ImageIcon titleIcon = new ImageIcon("F:\\ATM.png");
JDialog jDialog = new JDialog(jf, "错误", true);
jDialog.setSize(400, 200);
jDialog.setLocation(575, 400);
JTextField jTextField = new JTextField("您的账号已冻结!请联系人工处理。");
jTextField.setHorizontalAlignment(JTextField.CENTER);
jTextField.setEditable(false);
jTextField.setSelectedTextColor(Color.black);
jDialog.add(jTextField);
jDialog.setIconImage(titleIcon.getImage());
jTextField.setVisible(true);
jDialog.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} else {
ImageIcon titleIcon = new ImageIcon("F:\\ATM.png");
JDialog jDialog = new JDialog(jf, "错误", true);
jDialog.setSize(400, 200);
jDialog.setLocation(575, 400);
JTextField jTextField = new JTextField("您的账号和密码输入有误!请重新输入。");
jTextField.setHorizontalAlignment(JTextField.CENTER);
jTextField.setEditable(false);
jTextField.setSelectedTextColor(Color.black);
jDialog.add(jTextField);
jDialog.setIconImage(titleIcon.getImage());
jTextField.setVisible(true);
jDialog.setVisible(true);
jpl.repaint();
}
}
}
});
```
主要的改进点是:
1. 将 `count` 变量声明为 `ActionListener` 的私有成员变量,这样每次点击按钮时都可以访问到它。
2. 在密码输入错误时,先判断 `count` 是否等于 3,如果是则弹出提示信息并关闭窗体;如果不是,则弹出错误信息并让用户重新输入。
3. 在密码输入正确时,直接打开新窗口,无需增加冻结计数器。