更改文本时标记不清晰的文本

时间:2016-02-25 00:08:15

标签: java swing jtextarea

遗憾的是,当单击按钮时我无法处理txt的更改,我尝试写一个txt和加班,我单击按钮,这个txt值应该改变并且分配似乎是正确的,唯一的问题是打印数字不明显,似乎前一个txt的某些部分仍然存在。

package berGenerator;
import java.awt.EventQueue;
public class sscce {
 private JFrame frame;
 private final Action action = new SwingAction();
 private static     int i = 555;

/**
 * Launch the application.
 */
public static void main(String[] args) {    
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                sscce window = new sscce();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
/**
 * Create the application.
 */
public sscce() {
    initialize();
}
/**
 * Initialize the contents of the frame.
 */
private void initialize() {


    frame = new JFrame();
    frame.setBounds(100, 100, 550, 401);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);


    JButton Next = new JButton("Next");
    Next.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    Next.setAction(action);
    Next.setBounds(167, 290, 198, 64);
    frame.getContentPane().add(Next);



}
private class SwingAction extends AbstractAction {
    public SwingAction() {
        putValue(NAME, "Next Timeslot/scheduler");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }
    public void actionPerformed(ActionEvent e) {
        i = i+1;
        frame.getContentPane().validate();
        frame.getContentPane().repaint();
        String from = String.valueOf(i);
        System.out.println("sw is: "+from);
        JTextArea TextArea11 = new JTextArea("");
        TextArea11.setText(from);
        TextArea11.setForeground(Color.GREEN);
        TextArea11.setBounds(6, 66, 87, 16);
        frame.getContentPane().add(TextArea11);
    }
}
}

1 个答案:

答案 0 :(得分:2)

避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的结束,您将花费越来越多的时间来纠正。

布局管理器是Swing API的基础,您应该花时间学习如何使用它们,它们将解决比您认为的更多问题。

有关详细信息,请参阅Laying Out Components Within a Container

您正在创建JTextArea的多个实例并添加到框架中,但是您没有删除任何实例,您最多会遇到潜在的z排序问题,而最糟糕的情况是主要的资源管理问题

相反,只需创建JTextArea的单个实例,将其添加到框架中(就像您按下按钮一样),并在触发Action时简单地更新它,例如... < / p>

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import static javax.swing.Action.NAME;
import static javax.swing.Action.SHORT_DESCRIPTION;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Test {

    private JFrame frame;
    private final Action action = new SwingAction();
    private static int i = 555;
    private JTextArea textArea;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test window = new Test();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Test() {
        initialize();
    }

    private void initialize() {

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        textArea = new JTextArea(10, 20);
        textArea.setText(String.valueOf(i));
        frame.add(new JScrollPane(textArea));

        JButton next = new JButton("Next");
        next.setAction(action);
        frame.getContentPane().add(next, BorderLayout.SOUTH);

        frame.pack();
        frame.setLocationRelativeTo(null);

    }

    private class SwingAction extends AbstractAction {

        public SwingAction() {
            putValue(NAME, "Next Timeslot/scheduler");
            putValue(SHORT_DESCRIPTION, "Some short description");
        }

        public void actionPerformed(ActionEvent e) {
            i = i + 1;
            String from = String.valueOf(i);
            System.out.println("sw is: " + from);
            textArea.setText(from);
            textArea.setForeground(Color.GREEN);
        }
    }
}