我正在为项目创建GUI。我有一个小问题。如何实现重置选项?我在底部创建了一个按钮。当我点击按钮时,我应该能够提升代码。我尝试使用标签。但我猜主代码中使用的标签不能在重置按钮的actionPerformed()方法中使用。还有其他选择吗?
编辑 - 我还在研究代码。但这是代码的一部分,我遇到了麻烦。我已经将标签提到了--LABEL -
import java.sql.*;
import javax.swing.*;
import java.awt.Component;
import java.awt.event.*;
public class Console
{
public static String v1;
public static String v2;
Connection conn;
Statement stmt;
ResultSet rs;
JFrame f = new JFrame("Automation Testing Tool");
JPanel p1 = new JPanel();
JLabel l1 = new JLabel("Username");
JTextField t1 = new JTextField(10);
JButton b1 = new JButton("Login");
JLabel val = new JLabel();
JPanel p2 = new JPanel();
JLabel l2 = new JLabel("Schema");
final JComboBox c1 = new JComboBox();
JButton b2 = new JButton("OK");
JPanel p3 = new JPanel();
JLabel l3 = new JLabel("Query");
final JComboBox c2 = new JComboBox();
JButton b3 = new JButton("OK");
JButton reset = new JButton("Reset");
JLabel desc = new JLabel();
JButton b4 = new JButton("Submit");
public Console()
{
connect();
frame();
}
public void connect()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:luser/lpass@localhost");
stmt = conn.createStatement();
}
catch(Exception e1)
{
System.out.println("Exception caught - "+e1);
}
}
public void frame()
{
f.setLayout(null);
c1.addItem("--Select--");
c1.addItem("ADMIN");
c1.addItem("FINANCE");
c1.addItem("VSS");
c1.setEditable(true);
c2.addItem("--Select--");
p1.setBounds(0,10,580,160);
p1.add(l1);
p1.add(t1);
p1.add(b1);
p1.add(val);
f.add(p1);
for(Component c:p1.getComponents())
c.setEnabled(true);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String user = t1.getText().trim();
String sql = "SELECT usr from data WHERE usr='"+user+"'";
rs = stmt.executeQuery(sql);
int count=0;
while(rs.next())
{
count++;
}
if(count==1)
{
val.setText("Valid user!");
for(Component c:p2.getComponents())
c.setEnabled(true);
}
else
{
val.setText("Invalid User!");
}
}
catch(Exception e2)
{
System.out.println("Exception caught -"+e2);
}
}
});
p2.setBounds(0,180,580,100);
p2.add(l2);
p2.add(c1);
p2.add(b2);
f.add(p2);
for(Component c:p2.getComponents())
c.setEnabled(false);
//--LABEL
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
v1 = (String) c1.getSelectedItem();
if(v1.equals("ADMIN"))
{
c2.addItem("Query 3");
c2.addItem("Query 4");
c2.addItem("Query 5");
c2.addItem("Query 6");
c2.addItem("Query 7");
c2.addItem("Query 8");
c2.addItem("Query 9");
}
else if(v1.equals("FINANCE"))
{
c2.addItem("Query 1");
c2.addItem("Query 2");
c2.addItem("Query 5");
c2.addItem("Query 10");
}
else if(v1.equals("VSS"))
{
c2.addItem("Query 3");
c2.addItem("Query 4");
c2.addItem("Query 6");
c2.addItem("Query 9");
}
for(Component c:p2.getComponents())
c.setEnabled(false);
for(Component c:p3.getComponents())
c.setEnabled(true);
}
catch(Exception e3)
{
System.out.println("Exception caught -"+e3);
}
}
});
p3.setBounds(0,290,580,300);
p3.add(l3);
p3.add(c2);
p3.add(b3);
f.add(p3);
p3.add(desc);
p3.add(reset);
p3.add(b4);
for(Component c:p3.getComponents())
c.setEnabled(false);
b4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
//continue execution
}
catch(Exception e5)
{
System.out.println("Exception caught -"+e5);
}
}
});
reset.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//--LABEL--
}
});
f.setSize(600,600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
答案 0 :(得分:1)
也许您应该创建一个首先执行的函数my_fn()
,并包含所有其他方法调用,初始化等。
当您点击重置按钮时,您可以调用此方法。