Java 从其他类Swingworker访问JLabel
我有两门课。一个类用于gui,另一个类用于编写人员。 第二类包括Swingworker。它搜索一些日志文件并从中提取一些句子。在gui中还有一个标签,用于写入Java 从其他类Swingworker访问JLabel,java,swing,user-interface,jlabel,swingworker,Java,Swing,User Interface,Jlabel,Swingworker,我有两门课。一个类用于gui,另一个类用于编写人员。 第二类包括Swingworker。它搜索一些日志文件并从中提取一些句子。在gui中还有一个标签,用于写入search。。请稍候。当第二个类完成工作时,应将其更改为搜索完成。 此标签名为searchLabel,并在第一类中定义。它是私有变量 我的目的是:在第二节课中有done方法。在这个方法中,我想做searchLabel.setText(“blabla”) 我该怎么做?我无法访问。我认为,做公共JLabel也不是一个解决方案。 您可以很容易地
search。。请稍候。
当第二个类完成工作时,应将其更改为搜索完成。
此标签名为searchLabel
,并在第一类中定义。它是私有变量
我的目的是:在第二节课中有done
方法。在这个方法中,我想做searchLabel.setText(“blabla”)代码>
我该怎么做?我无法访问。我认为,做公共JLabel也不是一个解决方案。
您可以很容易地在代码中找到searcing/*问题在这里*/
这个字符串中
这是代码
这是我的gui类:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
public class mainGui extends JFrame {
private JPanel contentPane;
private JTextField userNametextField;
public static JLabel searchLabel,userNameWarningLabel,pathWarningLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainGui frame = new mainGui();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = frame.getSize().width;
int h = frame.getSize().height;
int x = (dim.width-w)/4;
int y = (dim.height-h)/2;
frame.setLocation(x, y);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public mainGui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 550, 455);
contentPane = new JPanel();
getContentPane().setLayout(null);
setTitle("Role Finding Script");
// Border border;
JLabel lblUsername = new JLabel(" Username :");
lblUsername.setFont(new Font("LucidaSans", Font.BOLD, 13));
lblUsername.setBounds(10, 53, 113, 30);
//Border border = BorderFactory.createRaisedSoftBevelBorder();
// border = BorderFactory.createEtchedBorder();
// lblUsername.setBorder(border);
getContentPane().add(lblUsername);
userNametextField = new JTextField();
userNametextField.setBounds(146, 53, 250, 30);
userNametextField.setFont(new Font("LucidaSans", Font.PLAIN, 13));
getContentPane().add(userNametextField);
userNametextField.setColumns(20);
JLabel lblRole = new JLabel(" Roles :");
lblRole.setFont(new Font("LucidaSans", Font.BOLD, 13));
lblRole.setBounds(10, 124, 113, 30);
// border = BorderFactory.createEtchedBorder();
// lblRole.setBorder(border);
getContentPane().add(lblRole);
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.setBounds(146, 124, 250, 30);
comboBox.addItem("VR_ANALYST1");
comboBox.addItem("VR_ANALYST2");
comboBox.addItem("VR_ANALYST3");
comboBox.addItem("VR_ANALYST4");
comboBox.addItem("VR_ANALYST5");
comboBox.addItem("VR_ANALYST6");
comboBox.addItem("VR_ANALYST7");
comboBox.addItem("VR_ANALYST8");
comboBox.addItem("VR_ANALYST9");
comboBox.addItem("VR_ANALYST10");
comboBox.addItem("VR_ANALYST11");
comboBox.addItem("VR_ANALYST12");
comboBox.setMaximumRowCount(6);
getContentPane().add(comboBox);
this.searchLabel = new JLabel("Searching.. Please wait..");
searchLabel.setFont(new Font("LucidaSans", Font.BOLD, 13));
searchLabel.setBounds(169, 325, 195, 30);
searchLabel.setVisible(false);
getContentPane().add(searchLabel);
JButton btnNewButton = new JButton("Show Me ");
btnNewButton.setFont(new Font("LucidaSans", Font.BOLD, 13));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(userNametextField.getText() == null || userNametextField.getText().equals("")){
userNameWarningLabel.setText("Please filled in the Username part.");
}else{
searchLabel.setVisible(true);
VolvoMain task = new VolvoMain();
task.execute();
}
}
});
btnNewButton.setBounds(188, 271, 126, 30);
getContentPane().add(btnNewButton);
JLabel lblPath = new JLabel(" Path :");
lblPath.setFont(new Font("Dialog", Font.BOLD, 13));
lblPath.setBounds(10, 195, 113, 30);
getContentPane().add(lblPath);
userNameWarningLabel = new JLabel("");
userNameWarningLabel.setBounds(156, 89, 227, 14);
userNameWarningLabel.setFont(new Font("Dialog", Font.ITALIC, 10));
userNameWarningLabel.setForeground(Color.red);
getContentPane().add(userNameWarningLabel);
JButton btnNewButton_1 = new JButton("...");
btnNewButton_1.setBounds(412, 195, 30, 30);
getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("+");
btnNewButton_2.setBounds(460, 195, 44, 30);
getContentPane().add(btnNewButton_2);
JLabel headerLabel = new JLabel("Find the Role");
headerLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
headerLabel.setHorizontalAlignment(SwingConstants.CENTER);
headerLabel.setBounds(94, 11, 358, 30);
headerLabel.setForeground(Color.red);
getContentPane().add(headerLabel);
pathWarningLabel = new JLabel("");
pathWarningLabel.setForeground(Color.RED);
pathWarningLabel.setFont(new Font("Dialog", Font.ITALIC, 10));
pathWarningLabel.setBounds(156, 236, 227, 14);
getContentPane().add(pathWarningLabel);
}
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Font;
导入java.awt.Toolkit;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
导入javax.swing.UIManager;
导入javax.swing.UIManager.LookAndFeelInfo;
导入javax.swing.JScrollPane;
导入javax.swing.JTable;
导入javax.swing.SwingConstants;
公共类mainGui扩展JFrame{
私有JPanel内容窗格;
私有JTextField用户名textfield;
公共静态JLabel searchLabel、userNameWarningLabel、pathWarningLabel;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
试一试{
for(LookAndFeelInfo:UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
UIManager.setLookAndFeel(info.getClassName());
打破
}
}
}捕获(例外e){
//如果Nimbus不可用,您可以将GUI设置为其他外观。
}
invokeLater(新的Runnable(){
公开募捐{
试一试{
mainGui frame=newmaingui();
维度dim=Toolkit.getDefaultToolkit().getScreenSize();
int w=frame.getSize().width;
int h=frame.getSize()高度;
INTX=(尺寸宽度-w)/4;
int y=(尺寸高度-h)/2;
帧设置位置(x,y);
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公共mainGui(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100550455);
contentPane=newjpanel();
getContentPane().setLayout(null);
setTitle(“角色查找脚本”);
//边界;
JLabel lblUsername=新的JLabel(“用户名:”);
setFont(新字体(“LucidaSans”,Font.BOLD,13));
lblUsername.挫折(10,53,113,30);
//Border Border=BorderFactory.createRaisedSoftBevelOrder();
//border=BorderFactory.create蚀刻边界();
//lblUsername.订单(边界);
getContentPane().add(lblUsername);
userNametextField=newjtextfield();
userNametextField.setBounds(146,53250,30);
setFont(新字体(“LucidaSans”,Font.PLAIN,13));
getContentPane().add(userNametextField);
userNametextField.setColumns(20);
JLabel lblRole=新的JLabel(“角色:”);
setFont(新字体(“LucidaSans”,Font.BOLD,13));
lblRole.立根(10,124,113,30);
//border=BorderFactory.create蚀刻边界();
//lblRole.setboorder(边框);
getContentPane().add(lblRole);
JComboBox comboBox=新的JComboBox();
组合框.立根(146、124、250、30);
comboBox.addItem(“VR_分析仪1”);
comboBox.addItem(“VR_分析仪2”);
comboBox.addItem(“VR_分析仪3”);
comboBox.addItem(“VR_分析仪4”);
comboBox.addItem(“VR_分析仪5”);
comboBox.addItem(“VR_分析仪6”);
comboBox.addItem(“VR_分析仪7”);
comboBox.addItem(“VR_分析仪8”);
comboBox.addItem(“VR_分析仪9”);
comboBox.addItem(“VR_分析仪10”);
comboBox.addItem(“VR_分析仪11”);
comboBox.addItem(“VR_分析仪12”);
comboBox.setMaximumRowCount(6);
getContentPane().add(组合框);
this.searchLabel=new JLabel(“正在搜索..请稍候..”);
searchLabel.setFont(新字体(“LucidaSans”,Font.BOLD,13));
searchLabel.setBounds(16932519530);
searchLabel.setVisible(false);
getContentPane().add(searchLabel);
JButton btnNewButton=新JButton(“向我展示”);
setFont(新字体(“LucidaSans”,Font.BOLD,13));
addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
if(userNametextField.getText()==null | | userNametextField.getText().equals(“”){
userNameWarningLabel.setText(“请填写用户名部分”);
}否则{
searchLabel.setVisible(true);
VolvoMain任务=新的VolvoMain();
task.execute();
}
}
});
btnNewButton.setBounds(188271126,30);
getContentPane().add(btnNewButton);
JLabel lblPath=新的JLabel(“路径:”);
lblPath.setFont(新字体(“Dialog”,Font.BOLD,13));
lblPath.立根(10195113,30);
getContentPane().add(lblPath);
userNameWarningLabel=新的JLabel(“”);
userNameWarningLabel.setBounds(156,89,227,14);
userNameWarningLabel.setFont(新字体(“对话框”,Font.ITALIC,10));
userNameWarningLabel.setForeground(颜色:红色);
getContentPane().add(userNameWarningLabel);
JButton btnNewButton_1=新JButton(“…”);
btnNewButton_1.立根(412195,30,30);
getContentPane().add(btnNewButton_1);
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javax.swing.SwingWorker;
public class VolvoMain extends SwingWorker{
private FileInputStream fstream;
private BufferedReader br;
private String username = "HALAA";
private String role = "VR_ANALYST";
private String firstLine = "";
private Pattern regex;
private List<String> stringList = new ArrayList<String>();
private File dir;
private mainGui mg = new mainGui();
//String reg = "\\t'AUR +(" + username + ") .*? /ROLE=\"(" + role + ")\".*$";
@SuppressWarnings("unchecked")
@Override
protected Object doInBackground() throws Exception {
String fmt = "\\t'AUR +(%s) .*? /ROLE=\"(%s)\".*$";
String reg = String.format(fmt, username, role);
regex = Pattern.compile(reg);
dir = new File(
"C:"+File.separator+"Documents and Settings"+File.separator+"sbx1807"+File.separator+"Desktop"+File.separator
+"Batu"+File.separator+"Deneme"+File.separator
);
File[] dirs = dir.listFiles();
String[] txtArray = new String[dirs.length];
int z=0;
for (File file : dirs) {
if (file.isDirectory()) {
}else {
if(file.getAbsolutePath().endsWith(".log")){
txtArray[z] = file.getAbsolutePath();
System.out.println(file.getAbsolutePath());
z++;
}
}
int j = 0;
for(int i=0; i<txtArray.length; i++){
if(txtArray[i] != null && !txtArray[i].equals("")){
try{
fstream = new FileInputStream(txtArray[i]);
br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
/* parse strLine to obtain what you want */
if((j%2)== 0){
firstLine = strLine;
}
if(((j%2) != 0) && regex.matcher(strLine).matches()){
stringList.add(firstLine);
stringList.add(strLine);
}
publish(stringList.size());
j++;
}
publish(stringList.size());
br.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
for(int k=0; k<stringList.size(); k++){
System.out.println(stringList.get(k));
}
}
return null;
}
@Override
public void done() {
System.out.println("bitti");
// getSearchJLabel().setText("Searching is done..");
// mainGui m = new mainGui();
// m.searchLabel.setText("done");
}
}
public class VolvoMain extends SwingWorker{
// ...
private final JLabel labelToUpdate;
public VolvoMain(JLabel labelToUpdate) {
this.labelToUpdate = labelToUpdate;
}
// ...
@Override
public void done() {
// Update labelToUpdate here
}