gpt4 book ai didi

java - 单击 jbutton 后 Swing 框架不保持或停留

转载 作者:行者123 更新时间:2023-12-02 03:28:38 25 4
gpt4 key购买 nike

我在 swing 的框架中有 2 个文本区域,其间有一个按钮,并且我使用的是 netbeans。

单击该按钮可使用 getText()textArea1 获取 SQL 查询。

使用 SubmitData() 处理输入(即在拆分查询后检查关键字的拼写)。在该方法中,它仅使用 setText() 将输出设置为 textArea2

我的问题是:当我按下按钮后,框架就没有停留或保持。

这是我的代码:

void createUI() throws Exception
{
JFrame frame = new JFrame("JDBC All in One");

// Layout of Main Window
Container c = frame.getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));

textArea1 = new JTextArea(10, 50);
textArea1.setBounds(10, 10, 30, 30);

btnInsert = new JButton("Submit");
btnInsert.setBounds(10, 10, 10, 10);
btnInsert.addActionListener(this);

textArea2 = new JTextArea(10, 50);
textArea2.setBounds(10, 10, 30, 30);

JPanel pnlInput1 = new JPanel();
JPanel pnlInput2 = new JPanel();
JPanel pnlInput3 = new JPanel();

pnlInput1.add(textArea1);
pnlInput3.add(btnInsert);
pnlInput2.add(textArea2);

frame.add(pnlInput1);
frame.add(pnlInput3);
frame.add(pnlInput2);

frame.setSize(400, 500);
frame.pack();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent evt)

{
String cmd = evt.getActionCommand();

if (cmd.equals("Submit")) {
try {
SubmitData();
} catch (Exception e) {}

}
}

public static void SubmitData() throws Exception {

s1 = textArea1.getText();

String[] s2 = s1.split("\\s+");

for (int i = 0; i < s2.length; i++) {
if (s2[i] .equals("elect")|| s2[i] .equals("selct") || s2[i].equals("slect" )|| s2[i].equals("selec")|| s2[i].equals("seect")

{
textArea2.setText("use 'select' instead of " + s2[i]);
System.exit(0);
}
if (s2[i] == "updat" || s2[i] == "updae" || s2[i] == "updte" || s2[i] == "upate") {
textArea2.setText("use 'update' instead of " + s2[i]);
System.exit(0);
}

if (s2[i] == "delet" || s2[i] == "delte" || s2[i] == "elete" || s2[i] == "dlete") {
textArea2.setText("use 'delete' instead of " + s2[i]);
System.exit(0);
}
if (s2[i] == "fro" || s2[i] == "frm" || s2[i] == "fom") {
textArea2.setText("use 'from' instead of " + s2[i]);
System.exit(0);
}
}
}

已编辑-我已将“==”与 .equals() 的字符串比较更改为“==”,但问题似乎并没有消失。

最佳答案

System.exit(0);终止您的 JVM。如果您想保留框架,请将其删除。

编辑第二个问题:在第一个 if 之后,将您的 if 替换为 else if,以便其他 if 在以下情况下不会执行条件为真。

for (int i = 0; i < s2.length; i++) {
if (s2[i] .equals("elect")|| s2[i] .equals("selct") || s2[i].equals("slect" )|| s2[i].equals("selec")|| s2[i].equals("seect")
{
textArea2.setText("use 'select' instead of " + s2[i]);
}
else if (s2[i] == "updat" || s2[i] == "updae" || s2[i] == "updte" || s2[i] == "upate") {
textArea2.setText("use 'update' instead of " + s2[i]);
}

else if (s2[i] == "delet" || s2[i] == "delte" || s2[i] == "elete" || s2[i] == "dlete") {
textArea2.setText("use 'delete' instead of " + s2[i]);
}
else if (s2[i] == "fro" || s2[i] == "frm" || s2[i] == "fom") {
textArea2.setText("use 'from' instead of " + s2[i]);
}
}

关于java - 单击 jbutton 后 Swing 框架不保持或停留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38409850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com