【发布时间】:2016-02-29 12:01:50
【问题描述】:
您好,我正在开发一个程序,该程序将通过程序列表运行并打开它们。 为了让它看起来不错,我需要更改标签中的文本以匹配程序名称。当我调用 ChnageTitle 和 Refresh 函数时,它们似乎不起作用。你能帮忙指出我哪里出错了吗?
这是主要的自动生成的窗口构建器代码,我对其稍作编辑并添加了
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);
}
}
}
【问题讨论】:
-
变量名不应以大写字符开头。方法名称不应以大写字符开头。关注Java conventions。不要使用空布局和 setBounds(,,,)。 Swing 旨在与layout managers 一起使用。
-
代码由window builder自动生成。所以我不确定如何在不搞砸的情况下改变它。我一直喜欢在每个单词中使用大写字母,这只是我个人的喜好。
-
问题是您在论坛上发布代码寻求帮助。您的命名约定会导致论坛突出显示问题,这使我们难以阅读您的代码。要么摆脱 IDE(最好自己编写 GUI 代码),要么学习如何配置 IDE 以遵循 Java 约定。
-
啊,好吧,对不起,我不知道该网站会遇到困难,下次我发帖时请记住这一点。通过代码,我自己的 gui 意味着不使用窗口构建器界面,而是在按钮等直接?我对 java 中的 gui 编码很陌生,我习惯于 Visual Basic。
-
从 Swing 教程中的工作演示代码开始。例如How to Use Labels。那里的代码将向您展示如何构造一个类,并且代码不依赖于从 IDE 生成的代码,这意味着您的代码在您从 IDE 移动到 IDE 时将是可移植的。只需使用IDE格式化代码、调试等即可。
标签: java eclipse jframe refresh windowbuilder