【问题标题】:Eclipse WindowBuilder / Jframe - Update labels textEclipse WindowBuilder / Jframe - 更新标签文本
【发布时间】: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


【解决方案1】:

我最近在 Swing 中开发了一个简单的 GUI 来从中运行 python 脚本。 有同样的问题(在 JFrame 上,一个标签应该告诉用户何时执行 python 脚本),尝试:

显示标签:

    Label.setVisible(true);
    Label.paintImmediately(x, y, width, height);

隐藏标签:

    Label.setVisible(false);

更新:

在标签不可见时使用此更改文本。

    Label.setText(String text);

【讨论】:

  • 您好,感谢您的回复,我今天实际上要离开商店,但明天我会试一试
  • 您好,我刚才有机会对其进行测试,但它似乎仍然没有更改文本。这可能与我从单独的课程中调用它有关吗?
  • 当然这不会改变文本。这是为了使标签可见或不可见。 (当标签不可见时,您可以使用“Label.setText(String text);”更改文本。
  • 所以你只能在标签不可见时更改文本?
  • 我现在无法确认,但肯定是最简单的方法,只需使其不可见,更改它,然后再次使其可见。这样可以避免可能的对齐(或任何其他类型的)错误
猜你喜欢
  • 1970-01-01
  • 2016-08-07
  • 2015-01-10
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 2023-03-09
  • 2011-08-06
相关资源
最近更新 更多