嗨,我正在做一个程序,它将运行一个程序列表并打开它们。为了使这个看起来好看,我需要更改标签中的文本,以匹配程序名称。然而,当我调用ChnageTitle和刷新功能时,它们似乎不起作用。你能帮我指出我哪里出错了吗?
下面是自动生成的主要窗口生成器代码,我稍微编辑了一下,并添加了
package WindowBuilder;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import Logic.ButtonPresses;
import javax.swing.JTextField;
import javax.swing.JLabel;
public class WindowBuilder {
public JLabel Title;
private JFrame frame;
/**
* Launch the application.
*/
public void Launch() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WindowBuilder window = new WindowBuilder();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public WindowBuilder() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void Refresh(){
frame.revalidate();
frame.repaint();
}
public void ChangeTitle(){
Title.setText("Test");
}
public void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnNext = new JButton("Next");
btnNext.setBounds(365, 226, 79, 45);
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ButtonPresses.Next();
}
});
frame.getContentPane().setLayout(null);
frame.getContentPane().add(btnNext);
JButton btnBack = new JButton("Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonPresses.Back();
}
});
btnBack.setBounds(0, 226, 79, 45);
frame.getContentPane().add(btnBack);
Title = new JLabel("Program Name");
Title.setBounds(9, 11, 89, 33);
frame.getContentPane().add(Title);
JButton btnInstall = new JButton("Install/Run");
btnInstall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnInstall.setBounds(173, 226, 116, 45);
frame.getContentPane().add(btnInstall);
}
}这里是我称之为函数的地方。
package Main;
import Logic.ProgramAdder;
import Logic.Programs;
import WindowBuilder.WindowBuilder;
public class Main {
public static void main(String[] args) {
Main start = new Main();
start.Start();
}
public void Start(){
WindowBuilder wb = new WindowBuilder();
wb.Launch();
ProgramAdder.ProgramList();
DisplayPrograms();
wb.ChangeTitle();
wb.Refresh();
}
public static void DisplayPrograms(){
for (Programs p : ProgramAdder.programs) {
System.out.print(p.ProgramName);
System.out.println(p.ProgramPath);
}
}
}发布于 2015-11-26 12:14:13
我最近在Swing中开发了一个简单的GUI来从它运行python脚本。如果出现了同样的问题(在JFrame上,标签应该在执行python脚本时告诉用户),请尝试:
要显示标签:
Label.setVisible(true);
Label.paintImmediately(x, y, width, height);要隐藏标签:
Label.setVisible(false);更新:
使用此选项更改文本,而标签是不可见的。
Label.setText(String text);https://stackoverflow.com/questions/33937824
复制相似问题